Click here to Skip to main content
15,358,239 members
Articles / Desktop Programming / MFC
Posted 5 Jul 2003


53 bookmarked

XCmyk - CMYK to RGB Calculator with source code

Rate me:
Please Sign up or sign in to vote.
4.80/5 (22 votes)
5 Jul 2003CPOL4 min read
XCmyk implements an RGB to CMYK calculator


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.


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:


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


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.


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.


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:


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.


Revision History

Version 1.0 - 2003 July 6

  • Initial public release.


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.


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
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

Comments and Discussions

QuestionIsn't this an RGB To CMYK conversion? Pin
Member 1347612422-May-18 15:03
MemberMember 1347612422-May-18 15:03 
Questiontanks tanks Pin
Claudio Basile12-Feb-12 10:57
MemberClaudio Basile12-Feb-12 10:57 
nallamvamsi13-May-10 2:39
Membernallamvamsi13-May-10 2:39 
GeneralThank you! Pin
Slash-X18-Nov-08 11:28
MemberSlash-X18-Nov-08 11:28 
General[Message Removed] Pin
nompel6-Oct-08 9:00
Membernompel6-Oct-08 9:00 
QuestionPrinting Blak without the blak ink! Pin
MSONBOL3-Dec-06 6:41
MemberMSONBOL3-Dec-06 6:41 
QuestionHow to Create a CMYK image from a pixel array? Pin
forgi20-Mar-05 4:36
Memberforgi20-Mar-05 4:36 
QuestionHow to Create a CMYK image from a pixel array? Pin
forgi20-Mar-05 4:36
Memberforgi20-Mar-05 4:36 
QuestionHow write cmy_model in Display with c#? Pin
jamiaa17-Mar-05 1:04
Memberjamiaa17-Mar-05 1:04 
QuestionHow write cmy_model in Display with c#? Pin
jamiaa17-Mar-05 0:54
Memberjamiaa17-Mar-05 0:54 
GeneralDon't forget zero! Pin
Member 7184907-Jun-04 11:56
MemberMember 7184907-Jun-04 11:56 
Generalnerds Pin
Anonymous19-Feb-04 10:06
MemberAnonymous19-Feb-04 10:06 
GeneralRe: nerds Pin
Christian Graus19-Feb-04 10:21
mveChristian Graus19-Feb-04 10:21 
GeneralC, M, Y, K, R, G, and B have a range of [0;1]. Pin
Anonymous4-Aug-03 23:54
MemberAnonymous4-Aug-03 23:54 
GeneralRe: C, M, Y, K, R, G, and B have a range of [0;1]. Pin
Christian Graus19-Feb-04 10:21
mveChristian Graus19-Feb-04 10:21 
QuestionHow can I get a pixel's C,M,Y,K Value of a CMYK .TIF picture? Pin
johnsuna17-Jul-03 0:17
Memberjohnsuna17-Jul-03 0:17 
GeneralCool article, but... Pin
Jim Crafton7-Jul-03 6:39
MemberJim Crafton7-Jul-03 6:39 
GeneralRe: Cool article, but... Pin
Hans Dietrich7-Jul-03 9:14
mentorHans Dietrich7-Jul-03 9:14 
GeneralRe: Cool article, but... Pin
DVoracek2-Jun-04 6:43
professionalDVoracek2-Jun-04 6:43 
QuestionRe: Cool article, but... Pin
Jan Bacca17-Jun-08 5:36
MemberJan Bacca17-Jun-08 5:36 
AnswerRe: Cool article, but... Pin
Hans Dietrich17-Jun-08 7:48
mentorHans Dietrich17-Jun-08 7:48 
GeneralThis is not "RGB to CMYK" PinPopular
Jürgen Eidt6-Jul-03 18:06
MemberJürgen Eidt6-Jul-03 18:06 
GeneralRe: This is not "RGB to CMYK" Pin
Blake Coverett7-Jul-03 0:31
MemberBlake Coverett7-Jul-03 0:31 
GeneralVery nice !! Pin
WREY6-Jul-03 8:42
MemberWREY6-Jul-03 8:42 
GeneralRe: Very nice !! Pin
Hans Dietrich6-Jul-03 9:44
mentorHans Dietrich6-Jul-03 9:44 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.