I’m trying to create java code for converting RGB to CMYK, CMYK to RGB and CMYK to CMYK values based on input and output icc profiles.
Converting a given value from RGB to CMYK and using the outcome for a vice versa conversion does not return the first given value. I know that this is the case with icc conversion, because RGB values are converted using a RGB to CIEXYZ table. The corresponding CIEXYZ values are looked up in another table that contains CIEXYZ to CMYK values. It is possible that there are no exact values so an approximate value is chosen (based on the rendering intent like perceptual or relative).
What I don’t understand is the fact that an RGB gray with value 128,128,128 results in CMYK 42, 34, 31, 20 and this seems correct although in real life prepress one would use lighter CMY values and a higher K value. But, the problem is when converting CMYK 42, 34, 31, 20 to RGB, the result is 41, 39, 32 and this is nowhere near the original RGB values 128, 128, 128. This way off of what would be expected.
I’ve tried different RGB icc profiles and all results are somewhat the same.
Taking a closer look at the values I suspected a wrong conversion of the CIEXYZ values because the first 3 input values look like the output values:
CMYK input 42, 34, 31, 20.
RGB output 41, 39, 32.
I’ve read that there is a problem with the used sRGB implementation in ava.awt.color.ICC_Profile, as can be read here:
/questions/29707253/icc-based-image-conversion-using-java/29895944#29895944. Maybe this is the case here.
This also confuses me in thinking one could not use any other input profile then sRGB as an RGB-based input profile.
Maybe it has something to do with the way RGB and CMYK values are converted to CIEXYZ values.
Here is the code I pieced together. Take notice of the fact I planned on using a lot of icc profiles, so copied those to another place in my IDE. So if trying this for yourself, use the correct path and file names as they are on your system.
package ConvertColor;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import java.io.IOException;
import java.util.Arrays;
public class ColorConv {
private static final String path = "src/iccprofiles/"; // path depends on your config
/* COLOR PROFILES (depending on your machine and copyrights of the profile itself)
"AdobeRGB1998.icc", // 0
"sRGB Profile.icc", // 1
"sRGB2014.icc", // 2
"ColorMatchRGB.icc", // 3
"AppleRGB.icc", // 4
"PAL_SECAM.icc", // 5
"SMPTE-C.icc", // 6
"VideoHD.icc", // 7
"VideoNTSC.icc", // 8
"VideoPAL.icc", // 9
"Coated_Fogra39L_VIGC_260.icc", // 10
"CoatedFOGRA27.icc", // 11
"CoatedFOGRA39.icc", // 12
"CoatedGRACoL2006.icc", // 13
"EuroscaleCoated.icc", // 14
"EuroscaleUncoated.icc", // 15
"JapanColor2001Coated.icc", // 16
"JapanColor2001Uncoated.icc", // 17
"JapanColor2002Newspaper.icc", // 18
"JapanColor2003WebCoated.icc", // 19
"JapanWebCoated.icc", // 20
"PSOcoated_v3.icc", // 21
"PSOuncoated_v3_FOGRA52.icc", // 22
"Uncoated_Fogra47L_VIGC_260.icc", // 23
"UncoatedFOGRA29.icc", // 24
"USWebCoatedSWOP.icc", // 25
"USWebUncoated.icc", // 26
"WebCoatedFOGRA28.icc", // 27
"WebCoatedSWOP2006Grade3.icc", // 28
"WebCoatedSWOP2006Grade5.icc", // 29
"PSO_Coated_v3_Matte_laminate.icc", // 30
"PSO_Coated_v3_Glossy_laminate.icc", // 31
"Metal-Printing_MPC1_FOGRA60.icc", // 32
"ISOcoated_v2_to_PSOcoated_v3_DeviceLink.icc", // 33 <-- devicelink doesn't work in java.awt.color.ICC_Profile
"PSOcoated_v3_to_ISOcoated_v2_DeviceLink.icc"}; // 34 <-- devicelink doesn't work in java.awt.color.ICC_Profile
*/
public static void main(String... args) {
try {
color2color("PSOcoated_v3.icc", "sRGB Profile.icc", 23, 21, 52);
color2color("sRGB Profile.icc", "PSOcoated_v3.icc", 100, 100, 0, 0);
color2color("PSOcoated_v3.icc", "sRGB Profile.icc", 128, 128, 128);
color2color("sRGB Profile.icc", "PSOcoated_v3.icc", 42, 34, 31, 20);
color2color("PSOcoated_v3.icc", "AdobeRGB1998.icc", 23, 21, 52);
color2color("AdobeRGB1998.icc", "PSOcoated_v3.icc", 100, 100, 0, 0);
color2color("PSOcoated_v3.icc", "AdobeRGB1998.icc", 128, 128, 128);
color2color("AdobeRGB1998.icc", "PSOcoated_v3.icc", 42, 34, 31, 20);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void color2color(String iccOutput, String iccInput, float... inputColor) throws IOException {
StringBuilder sbColNmsIn = new StringBuilder();
StringBuilder sbColValIn = new StringBuilder();
StringBuilder sbColNmsOut = new StringBuilder();
StringBuilder sbColValOut = new StringBuilder();
ColorSpace fromCS = new ICC_ColorSpace(ICC_Profile.getInstance(path + iccInput));
ColorSpace toCS = new ICC_ColorSpace(ICC_Profile.getInstance(path + iccOutput));
for (int i = 0; i < inputColor.length; i++) {
sbColNmsIn.append(fromCS.getName(i).charAt(0));
sbColValIn.append(Math.round(inputColor[i]));
if (i < inputColor.length - 1) {
sbColValIn.append(", ");
}
}
if (inputColor.length == 4 & fromCS.getNumComponents() == 4) {
if ((Math.max(Math.max(Math.max(inputColor[0], inputColor[1]), inputColor[2]), inputColor[3]) <= 100f)
& (Math.min(Math.min(Math.min(inputColor[0], inputColor[1]), inputColor[2]), inputColor[3]) >= 0f)) {
float[] outputColor = toCS.fromCIEXYZ(fromCS.toCIEXYZ(inputColor));
// conversion correction for CMYK to CIEXYZ values
for(int i = 0; i < inputColor.length; i++) {
inputColor[i] = inputColor[i]/100f;
}
for (int i = 0; i < outputColor.length; i++) {
sbColNmsOut.append(toCS.getName(i).charAt(0));
sbColValOut.append(Math.round( (outputColor[i]) * 255f));
if (i < outputColor.length - 1) {
sbColValOut.append(", ");
}
}
System.out.println(iccInput + " ---> " + iccOutput);
System.out.println("input " + sbColNmsIn.replace(3, 4, "K") + " : " + sbColValIn);
System.out.println("output " + sbColNmsOut + " : " + sbColValOut + 'n');
} else {
System.out.println("ERROR: One or more values are either too low or to high!n");
}
} else if (inputColor.length == 3 & fromCS.getNumComponents() == 3) {
if ((Math.max(Math.max(inputColor[0], inputColor[1]), inputColor[2]) <= 255f)
& (Math.min(Math.min(inputColor[0], inputColor[1]), inputColor[2]) >= 0f)) {
// conversion correction for RGB to CIEXYZ values
for(int i = 0; i < inputColor.length; i++) {
inputColor[i] = inputColor[i]/255f;
}
float[] outputColor = toCS.fromCIEXYZ(fromCS.toCIEXYZ(inputColor));
for (int i = 0; i < outputColor.length; i++) {
sbColNmsOut.append(toCS.getName(i).charAt(0));
sbColValOut.append(Math.round((outputColor[i]) * 100f));
if (i < outputColor.length - 1) {
// sbColNmsOut.append(", ");
sbColValOut.append(", ");
}
}
System.out.println(iccInput + " ---> " + iccOutput);
System.out.println("input " + sbColNmsIn + " : " + sbColValIn);
System.out.println("output " + sbColNmsOut.replace(3, 4, "K") + " : " + sbColValOut + 'n');
} else {
System.out.println("ERROR: One or more values are either too low or to high!n");
}
} else {
System.out.println("ERROR: The amount of input values doesn't correspond with chosen input icc profile!n");
}
}
}
user24876900 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.