Click here to Skip to main content
Click here to Skip to main content

XCmyk - CMYK to RGB Calculator with source code

By , 5 Jul 2003
 

Introduction

A color space is a way of representing colors and how they relate to one another. Among the color spaces currently in use, the RGB and CMYK spaces are encountered very frequently: RGB, because it is used in color monitors; and CMYK, because it is used for printing. There are different reasons for using different color spaces, and any google search will produce many results for further study.

RGB

In the computer industry, RGB has become the de facto standard for both desktop applications and web-based systems. RGB is an additive color space - that is, its three primary colors (red, green, and blue) combine additively to produce any desired colors. Each of the component colors is represented by a number from 0 to 255. This representation lends itself to easy manipulation by computer systems in a hex notation. It also means that 255 x 255 x 255, or 16,777,216 different colors may be represented in the RGB color space. The RGB color space may be shown as:

        screenshot

This shows that when you have an RGB value of (255,255,255), you will get white.

CMYK

CMYK by contrast is a subtractive color space. It is the complement of the RGB color space - cyan, magenta, and yellow are the complements of red, green, and blue, respectively. CMYK is called subtractive because its primary colors are subtracted from white light to produce the resulting color: cyan absorbs red, magenta absorbs green, and yellow absorbs blue.

        screenshot

So you could increase the red either by increasing the magenta and yellow, or by decreasing the cyan. In the CMYK color space, the values for each of the colors are typically shown as percentages, from 0 to 100. So up to 100 x 100 x 100, or 1,000,000 different colors may be represented in the CMYK color space (black is used to lighten or darken the color).

The fourth color (black) was added to the CMYK color space because in printing it gives a purer form of black than the other three colors. Black ink is also cheaper than the other three inks, so that printers will adjust the colors until the lowest-cost combination is achieved. Black was added to the CMYK color space as "K" to avoid confusing a "B" with blue or brown.

RGB to CMYK

Often a company will want to match its web site layout and colors to its corporate logo, and to its use of colors in printed material. One standard equation that is used to convert RGB to CMYK is:

    Black   = minimum(1-Red,1-Green,1-Blue)
    Cyan    = (1-Red-Black)/(1-Black)
    Magenta = (1-Green-Black)/(1-Black)
    Yellow  = (1-Blue-Black)/(1-Black) 

Note that this equation assumes that the RGB values have been normalized - i.e., that they are between 0 and 1.0.

With this equation, you are able to approximate the RGB color you see on your screen with what the color will look like when it is printed. I say "approximate" because there are several factors to consider when going from screen to printed material. The first is that computer monitors usually have poor color calibration. Imaging professionals use very high-end monitors and a variety of calibration tools to try to minimize this factor.

Another major factor: colors will simply look different when printed, often due to the type of paper being used - matte, semi-gloss, high gloss, etc. Also, color inks are not opaque, so if the paper is not white, the color will be tinted by the paper. Textured paper will also affect the appearance of the color. Imaging professionals must deal with all these things, and compensate for them.

One tool you will see imaging professionals use quite often is something called a Process Color Guide. This is simply a small booklet - usually fan-fold - with a large number of colors and their CMYK values printed on white paper. A Process Color Guide usually consists of several of these booklets, for matte paper, semi-gloss paper, and so on.

Demo App

The XCmykTest.exe demo shows how to use the conversion routines in XCmyk.cpp:

        screenshot

Note that as you turn one of the RGB or CMY knobs in one direction, the corresponding knob in the other color space will turn in the opposite direction.

Run-time Requirements

CKnobControlST uses Gdiplus.dll, which is included with Windows XP. GDI+ is available as a redistributable for Windows NT 4.0SP6, Windows 2000, Windows 98, and Windows Me. To download the latest redistributable, please go here. To learn more about GDI+, please go here.

Acknowledgements

Revision History

Version 1.0 - 2003 July 6

  • Initial public release.

Usage

This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.



License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
Member
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.






Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questiontanks tanksmemberClaudio Basile12 Feb '12 - 10:57 
Tanks so much Mr. Hans , you solve me a big problem in my job.
GeneralTHANK YOU VERY MUCHmembernallamvamsi13 May '10 - 2:39 
Thank you very much .. it came at my rescue
GeneralThank you!memberSlash-X18 Nov '08 - 11:28 
The only usefull article on CMYK to RGB conversion found on the net. Thanks a lot! Wink | ;)
General[Message Removed]membernompel6 Oct '08 - 9:00 
Spam message removed
QuestionPrinting Blak without the blak ink!memberMSONBOL3 Dec '06 - 6:41 
I'm developing an application using HP inkjet printer , it's required to print the colors without black,I'm using C# an d the method of bruch is using the RGB , i make the conversion as per the eaquation mentioned , i got the colors: CMY to give black , its corresponding value in RGB:
R=54 g=54 b=57 but when i print , the result has black ink over the paper ?
Any body knows how to handle this?
 
Mohamed Sonbol-Cairo
QuestionHow to Create a CMYK image from a pixel array?sussforgi20 Mar '05 - 4:36 
I would like to create a CMYK (4x8bit 1 layer) image from pixel array (BYTE* pixels). I can not encode/create/save a valid CMYK image, just a file which contains TIF-like data, biut it is not valid (no viewer can open it, all the properties are invalid) ... i tryed almost anything. Any hints??
QuestionHow to Create a CMYK image from a pixel array?memberforgi20 Mar '05 - 4:36 
I would like to create a CMYK (4x8bit 1 layer) image from pixel array (BYTE* pixels). I can not encode/create/save a valid CMYK image, just a file which contains TIF-like data, biut it is not valid (no viewer can open it, all the properties are invalid) ... i tryed almost anything. Any hints??
QuestionHow write cmy_model in Display with c#?sussjamalN17 Mar '05 - 1:04 
Hello ,
how can i write CMY_model(Color_Struc) in Display(in bitmap) in C#???
for exemple by RGB we have setPixil(i,j,n)with 0
QuestionHow write cmy_model in Display with c#?memberjamiaa17 Mar '05 - 0:54 
Laugh | :laugh: hello ,
how can i write CMY_model(Color_model) in Display(in bitmap) in C#???
for exemple by RGB we have setPixil(i,j,n)with 0
GeneralDon't forget zero!memberAnonymous7 Jun '04 - 11:56 
"It also means that 255 x 255 x 255, or 16,777,216..."
 
0->255 = 256
 
256 x 256 x 256 = 16777216
 
nice article - thanks.
GeneralnerdssussAnonymous19 Feb '04 - 10:06 
you guys are nerds
GeneralRe: nerdsmemberChristian Graus19 Feb '04 - 10:21 
No, nerds are a type of candy. We are geeks.

 
Christian
 
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
GeneralC, M, Y, K, R, G, and B have a range of [0;1].sussAnonymous4 Aug '03 - 23:54 
This formulae assumes that C, M, Y, K, R, G, and B have a range of [0;1]. For range of [0;255] or [0;100%] you must use additional calculation.
GeneralRe: C, M, Y, K, R, G, and B have a range of [0;1].memberChristian Graus19 Feb '04 - 10:21 
Yes, the article says as much, and it's not exactly hard to do.
 
Moron.

 
Christian
 
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
QuestionHow can I get a pixel's C,M,Y,K Value of a CMYK .TIF picture?memberjohnsuna17 Jul '03 - 0:17 
I have a picture of CMYK Color space of *.TIFF(300DPI),how can I get the C,M,Y,K value of one pixel?
Thank you.
 
Best Wishes!
 
Johnsun
GeneralCool article, but...memberJim Crafton7 Jul '03 - 6:39 
There's always a but! Don't you hate that! Smile | :)
Well running your calc, with
C: 0, M: 0, Y: 100, K: 0 I would expect to get pure yellow
What I get, in RGB (according to your calc) is
R: 255, G: 255, B:155, and a rather washed out yellow
 
In Photoshop, the same CMYK values produce a pure yellow
and RGB values of:
R: 249 G: 244 B: 0 which is closer to what I'd expect
 
So who's "right" and how would you account for the discrepancy?
 
Cheers Smile | :)
 
¡El diablo está en mis pantalones! ¡Mire, mire!
 
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
GeneralRe: Cool article, but...memberHans Dietrich7 Jul '03 - 9:14 
Jim Crafton wrote:
Well running your calc, with C: 0, M: 0, Y: 100, K: 0 I would expect to get pure yellow What I get, in RGB (according to your calc) is R: 255, G: 255, B:155, and a rather washed out yellow
 
There is a bug in CMYK2RGB. Replace these lines:
    C = C / 255.0;
    M = M / 255.0;
    Y = Y / 255.0;
    K = K / 255.0; 
with these:
    C = C / 100.0;
    M = M / 100.0;
    Y = Y / 100.0;
    K = K / 100.0; 
 
Jim Crafton wrote:
In Photoshop, the same CMYK values produce a pure yellow and RGB values of: R: 249 G: 244 B: 0 which is closer to what I'd expect
 
Concerning the Photoshop values, there are many discussions about the values that Photoshop produces in the news groups. You will have to decide for yourself who is right.
 
If you want to look into this more deeply, I suggest getting a copy of Pantone's "Web-Safe Guide" (ISBN 1-576164-60-8). They give RGB and CMYK values for many common web colors - and yes, they give a value of CMYK(0,0,100,0) for RGB(255,255,0).
 
Best wishes,
Hans
 

 

 


GeneralRe: Cool article, but...memberDVoracek2 Jun '04 - 6:43 
Could you update your code with this fix? I tried to change it, but my compiler (VC++ 6.0) can't find gdiplus.h.
 
Thanks.

QuestionRe: Cool article, but...memberJan Bacca17 Jun '08 - 5:36 
I had the same problem as the previous poster. I installed the latest SDK and compile the program again. It finally found gdiplus.h but now it can't find sal.h Where can I find that file?
AnswerRe: Cool article, but...mvpHans Dietrich17 Jun '08 - 7:48 
Not sure why you need that - it's not referenced directly in any of this project's code. I found it in ...\VC\include\sal.h (installed with VS2005).
 

GeneralThis is not "RGB to CMYK"memberJürgen Eidt6 Jul '03 - 18:06 
Colorspace conversions must be done with a CMS via profiles. This formula you are using just gives you an idea about the picture, but it's not accurate in any way. Someone published it on the web and now everyone is thinking to use this to separate to CMYK.
 
I think you article should focussed more on the knob controls than on the complicated subject of color space conversion.

 
Jürgen Eidt
www.cpicture.de

GeneralRe: This is not "RGB to CMYK"memberBlake Coverett7 Jul '03 - 0:31 
This post hits the nail right on the head. The knobs are nicely done, but CMYK is by it's very definition a device specific colorspace. If you want to convert to or from it you need to know _which_ CMYK you are talking about and an ICM profile profile is the only standard way to do that under Windows. Color science is a subtle, complicated field.
 
--
-Blake (com/bcdev/blake)
GeneralVery nice !!memberWREY6 Jul '03 - 8:42 
Very nice indeed!!
 
Because you are using knobs (dials, etc.), you don't quite get an exact calibration of the color you want, which means if under your RGB and CMYK groups, you were to provide the user with the ability to enter the precise value of the color desired (thereby, achieving calibration), this would solve that issue.
 
Cool | :cool:
 
William
 
Fortes in fide et opere!
GeneralRe: Very nice !!memberHans Dietrich6 Jul '03 - 9:44 
WREY wrote:
you were to provide the user with the ability to enter the precise value
 
Actually, it already has this, because you can use the up/down arrows to dial in exact values - first, click on the knob to give it focus, then use arrow keys.
 
Best wishes,
Hans

GeneralRe: Very nice !!memberWREY6 Jul '03 - 10:18 
Cool!!
 
Cool | :cool:
 
William
 
Fortes in fide et opere!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 6 Jul 2003
Article Copyright 2003 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid