RGB To CMYK

This algorithm converts RGB color model to CMYK color model.



									struct RGB
{
	unsigned char R;
	unsigned char G;
	unsigned char B;
};

struct CMYK
{
	double C;
	double M;
	double Y;
	double K;
};

double Max(double a, double b) {
	return a >= b ? a : b;
}

struct CMYK RGBToCMYK(struct RGB rgb) {
	struct CMYK cmyk;

	double dr = (double)rgb.R / 255;
	double dg = (double)rgb.G / 255;
	double db = (double)rgb.B / 255;
	cmyk.K = 1 - Max(Max(dr, dg), db);
	cmyk.C = (1 - dr - cmyk.K) / (1 - cmyk.K);
	cmyk.M = (1 - dg - cmyk.K) / (1 - cmyk.K);
	cmyk.Y = (1 - db - cmyk.K) / (1 - cmyk.K);

	return cmyk;
}
								


Example

									struct RGB data = { 125, 29, 107 };
struct CMYK value = RGBToCMYK(data);
								


Output

									C: 0
M: 0.7679999999999999
Y: 0.14399999999999993
K: 0.50980392156862742