RGB To CMYK
This algorithm converts RGB color model to CMYK color model.
public struct CMYK
{
private double _c;
private double _m;
private double _y;
private double _k;
public CMYK(double c, double m, double y, double k)
{
this._c = c;
this._m = m;
this._y = y;
this._k = k;
}
public double C
{
get { return this._c; }
set { this._c = value; }
}
public double M
{
get { return this._m; }
set { this._m = value; }
}
public double Y
{
get { return this._y; }
set { this._y = value; }
}
public double K
{
get { return this._k; }
set { this._k = value; }
}
public bool Equals(CMYK cmyk)
{
return (this.C == cmyk.C) && (this.M == cmyk.M) && (this.Y == cmyk.Y) && (this.K == cmyk.K);
}
}
public struct RGB
{
private byte _r;
private byte _g;
private byte _b;
public RGB(byte r, byte g, byte b)
{
this._r = r;
this._g = g;
this._b = b;
}
public byte R
{
get { return this._r; }
set { this._r = value; }
}
public byte G
{
get { return this._g; }
set { this._g = value; }
}
public byte B
{
get { return this._b; }
set { this._b = value; }
}
public bool Equals(RGB rgb)
{
return (this.R == rgb.R) && (this.G == rgb.G) && (this.B == rgb.B);
}
}
public static CMYK RGBToCMYK(RGB rgb)
{
double dr = (double)rgb.R / 255;
double dg = (double)rgb.G / 255;
double db = (double)rgb.B / 255;
double k = 1 - Math.Max(Math.Max(dr, dg), db);
double c = (1 - dr - k) / (1 - k);
double m = (1 - dg - k) / (1 - k);
double y = (1 - db - k) / (1 - k);
return new CMYK(c, m, y, k);
}
Example
RGB data = new RGB(125, 29, 107);
CMYK value = RGBToCMYK(data);
Output
C: 0
M: 0.7679999999999999
Y: 0.14399999999999993
K: 0.50980392156862742