I am trying to implement a solution for calculating the conversion between RGB and CMYK and vice versa. Here is what I have so far:
public static int[] rgbToCmyk(int red, int green, int blue)
{
int black = Math.min(Math.min(255 - red, 255 - green), 255 - blue);
if (black!=255) {
int cyan = (255-red-black)/(255-black);
int magenta = (255-green-black)/(255-black);
int yellow = (255-blue-black)/(255-black);
return new int[] {cyan,magenta,yellow,black};
} else {
int cyan = 255 - red;
int magenta = 255 - green;
int yellow = 255 - blue;
return new int[] {cyan,magenta,yellow,black};
}
}
public static int[] cmykToRgb(int cyan, int magenta, int yellow, int black)
{
if (black!=255) {
int R = ((255-cyan) * (255-black)) / 255;
int G = ((255-magenta) * (255-black)) / 255;
int B = ((255-yellow) * (255-black)) / 255;
return new int[] {R,G,B};
} else {
int R = 255 - cyan;
int G = 255 - magenta;
int B = 255 - yellow;
return new int[] {R,G,B};
}
}