Click here to Skip to main content
12,695,044 members (31,177 online)
Click here to Skip to main content

Stats

252.6K views
5.2K downloads
52 bookmarked
Posted

XCmyk - CMYK to RGB Calculator with source code

, 5 Jul 2003 CPOL
XCmyk implements an RGB to CMYK calculator
// XCmyk.cpp  Version 1.0
//
// Author:  Hans Dietrich
//          hdietrich2@hotmail.com
//
// Description:
//     XCmyk.cpp implements functions to convert RGB to CMYK and CMYK to RGB.
//
// History
//     Version 1.0 - 2003 July 5
//     - Initial public release
//
// This software is released into the public domain.  You are free to use it 
// in any way you like.
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XCmyk.h"


///////////////////////////////////////////////////////////////////////////////
//
// The algorithms for these routines were taken from:
//     http://www.neuro.sfc.keio.ac.jp/~aly/polygon/info/color-space-faq.html
//
// RGB --> CMYK                              CMYK --> RGB
// ---------------------------------------   --------------------------------------------
// Black   = minimum(1-Red,1-Green,1-Blue)   Red   = 1-minimum(1,Cyan*(1-Black)+Black)
// Cyan    = (1-Red-Black)/(1-Black)         Green = 1-minimum(1,Magenta*(1-Black)+Black)
// Magenta = (1-Green-Black)/(1-Black)       Blue  = 1-minimum(1,Yellow*(1-Black)+Black)
// Yellow  = (1-Blue-Black)/(1-Black)
//


///////////////////////////////////////////////////////////////////////////////
// RGB2CMYK
void RGB2CMYK(BYTE r, BYTE g, BYTE b, BYTE& c, BYTE& m, BYTE& y, BYTE& k)
{
	double R, G, B;
	R = (double) r;
	G = (double) g;
	B = (double) b;

	R = 1.0 - (R / 255.0);
	G = 1.0 - (G / 255.0);
	B = 1.0 - (B / 255.0);

	double C, M, Y, K;
	if (R < G)
		K = R;
	else
		K = G;
	if (B < K)
		K = B;

	C = (R - K)/(1.0 - K);
	M = (G - K)/(1.0 - K);
	Y = (B - K)/(1.0 - K);

	C = (C * 100) + 0.5;
	M = (M * 100) + 0.5;
	Y = (Y * 100) + 0.5;
	K = (K * 100) + 0.5;

	c = (BYTE) C;
	m = (BYTE) M;
	y = (BYTE) Y;
	k = (BYTE) K;
}

///////////////////////////////////////////////////////////////////////////////
// CMYK2RGB
COLORREF CMYK2RGB(BYTE c, BYTE m, BYTE y, BYTE k)
{
	BYTE r, g, b;
	COLORREF rgb;

	double R, G, B;
	double C, M, Y, K;

	C = (double) c;
	M = (double) m;
	Y = (double) y;
	K = (double) k;

	C = C / 255.0;
	M = M / 255.0;
	Y = Y / 255.0;
	K = K / 255.0;

	R = C * (1.0 - K) + K;
	G = M * (1.0 - K) + K;
	B = Y * (1.0 - K) + K;

	R = (1.0 - R) * 255.0 + 0.5;
	G = (1.0 - G) * 255.0 + 0.5;
	B = (1.0 - B) * 255.0 + 0.5;

	r = (BYTE) R;
	g = (BYTE) G;
	b = (BYTE) B;

	rgb = RGB(r,g,b);

	return rgb;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

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 www.hdsoft.org.






You may also be interested in...

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