我正在尝试实现计算RGB和CMYK之间转换的解决方案,反之亦然。以下是我到目前为止的情况:
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};
}
}
答案 0 :(得分:7)
正如Lea Verou所说,你应该使用色彩空间信息,因为没有算法可以从RGB映射到CMYK。 Adobe有一些ICC颜色配置文件可供下载1,但我不确定它们是如何获得许可的。
一旦你有了颜色配置文件,就会发生以下情况:
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 {
final static String pathToCMYKProfile = "C:\\UncoatedFOGRA29.icc";
public static float[] rgbToCmyk(float... rgb) throws IOException {
if (rgb.length != 3) {
throw new IllegalArgumentException();
}
ColorSpace instance = new ICC_ColorSpace(ICC_Profile.getInstance(pathToCMYKProfile));
float[] fromRGB = instance.fromRGB(rgb);
return fromRGB;
}
public static float[] cmykToRgb(float... cmyk) throws IOException {
if (cmyk.length != 4) {
throw new IllegalArgumentException();
}
ColorSpace instance = new ICC_ColorSpace(ICC_Profile.getInstance(pathToCMYKProfile));
float[] fromRGB = instance.toRGB(cmyk);
return fromRGB;
}
public static void main(String... args) {
try {
float[] rgbToCmyk = rgbToCmyk(1.0f, 1.0f, 1.0f);
System.out.println(Arrays.toString(rgbToCmyk));
System.out.println(Arrays.toString(cmykToRgb(rgbToCmyk[0], rgbToCmyk[1], rgbToCmyk[2], rgbToCmyk[3])));
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:6)
要准确地将值从RGB转换为CMYK,反之亦然,就像Photoshop一样,您需要使用ICC颜色配置文件。您将在互联网中找到的所有简单算法解决方案(如上面发布的那些)都是不准确的,并产生超出CMYK色域的颜色(例如,它们将CMYK(100,0,0,0)转换为rgb(0 ,255,255)这显然是错误的,因为rgb(0,255,255)不能用CMYK再现)。 查看使用ICC颜色配置文件转换颜色的java.awt.color.ICC_ColorSpace和java.awt.color.ICC_Profile类。 至于颜色配置文件本身,Adobe会免费分发它们。
答案 2 :(得分:3)
更好的方法:
try {
// The "from" CMYK colorspace
ColorSpace cmykColorspace = new ICC_ColorSpace(ICC_Profile.getInstance("icc/CoatedFOGRA27.icc"));
// The "to" RGB colorspace
ColorSpace rgbColorspace = new ICC_ColorSpace(ICC_Profile.getInstance("icc/AdobeRGB1998.icc"));
// Bring in to CIEXYZ colorspace (refer to Java documentation: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/color/ColorSpace.html)
float[] ciexyz = cmykColorspace.toCIEXYZ(cmyk);
float[] thisColorspace = rgbColorspace.fromCIEXYZ(ciexyz);
float[] rgb = thisColorspace;
Color c = new Color(rgb[0], rgb[1], rgb[2]);
// Format RGB as Hex and return
return String.format("#%06x", c.getRGB() & 0xFFFFFF);
} catch (IOException e) { e.printStackTrace(); }
答案 3 :(得分:0)
为了正确显示,CMYK图像应包含color space information作为ICC配置文件。因此,最好的方法是使用可以使用Sanselan轻松提取的ICC配置文件:
ICC_Profile iccProfile = Sanselan.getICCProfile(new File("filename.jpg"));
ColorSpace cs = new ICC_ColorSpace(iccProfile);
如果图像没有附加ICC配置文件,我会默认使用Adobe profiles。
现在的问题是你不能只使用ImageIO加载带有自定义颜色空间的JPEG文件,因为它会失败抛出异常,抱怨它不支持某些颜色空间或类似的sthing。 Hense你必须使用栅格:
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new ByteArrayInputStream(data));
Raster srcRaster = decoder.decodeAsRaster();
BufferedImage result = new BufferedImage(srcRaster.getWidth(), srcRaster.getHeight(), BufferedImage.TYPE_INT_RGB);
WritableRaster resultRaster = result.getRaster();
ColorConvertOp cmykToRgb = new ColorConvertOp(cs, result.getColorModel().getColorSpace(), null);
cmykToRgb.filter(srcRaster, resultRaster);
然后,您可以在任何需要的位置使用result
,并且它会转换颜色。
在实践中,然而我遇到了一些图像(用相机拍摄并用Photoshop处理过),这些图像以某种方式反转了颜色值,因此生成的图像总是反转的,即使再次反转它们也太亮了。虽然我仍然不知道如何确定何时使用它(当我需要反转像素值时),我有一个算法可以校正这些值并逐个像素地转换颜色:
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new ByteArrayInputStream(data));
Raster srcRaster = decoder.decodeAsRaster();
BufferedImage ret = new BufferedImage(srcRaster.getWidth(), srcRaster.getHeight(), BufferedImage.TYPE_INT_RGB);
WritableRaster resultRaster = ret.getRaster();
for (int x = srcRaster.getMinX(); x < srcRaster.getWidth(); ++x)
for (int y = srcRaster.getMinY(); y < srcRaster.getHeight(); ++y) {
float[] p = srcRaster.getPixel(x, y, (float[])null);
for (int i = 0; i < p.length; ++i)
p[i] = 1 - p[i] / 255f;
p = cs.toRGB(p);
for (int i = 0; i < p.length; ++i)
p[i] = p[i] * 255f;
resultRaster.setPixel(x, y, p);
}
我很确定RasterOp或ColorConvertOp可以用来提高对话效率,但这对我来说已经足够了。
说真的,没有必要使用这些简化的CMYK到RGB转换算法,因为您可以使用嵌入到图像中或可从Adobe免费获得的ICC配置文件。如果不完美(使用嵌入的配置文件),所得到的图像看起来会更好。
答案 4 :(得分:0)
public static String makeCMYKString(int color) {
double red = Color.red(color);
double green = Color.green(color);
double blue = Color.blue(color);
double red1 = red / 255;
double green1 = green / 255;
double blue1 = blue / 255;
double max = (Math.max(Math.max(red1, green1), blue1));
double K = 1 - max;
double C = (1 - red1 - K) / (1 - K);
double M = (1 - green1 - K) / (1 - K);
double Y = (1 - blue1 - K) / (1 - K);
double CMYK[] = {C, M, Y, K};
String cmyk = "CMYK = (" + Math.round(C * 100) + " , " + Math.round(M * 100) + " , " + Math.round(Y * 100) + " , " + Math.round(K * 100) + ")";
return cmyk;
}
答案 5 :(得分:-1)
Here与您的问题完全相同
以下是该页面的副本/面食:
/** CMYK to RGB conversion */
/* Adobe PhotoShop algorithm */
cyan = Math.min(255, cyan + black); //black is from K
magenta = Math.min(255, magenta + black);
yellow = Math.min(255, yellow + black);
rgb[0] = 255 - cyan;
rgb[1] = 255 - magenta;
rgb[2] = 255 - yellow;
/* GNU Ghostscript algorithm -- this is better*/
int colors = 255 - black;
rgb[0] = colors * (255 - cyan)/255;
rgb[1] = colors * (255 - magenta)/255;
rgb[2] = colors * (255 - yellow)/255;
答案 6 :(得分:-1)
这是我的方式。请记住,我从原始颜色中重新转换了RGB颜色。
public static String getCMYK(int c){
float computedC = 0;
float computedM = 0;
float computedY = 0;
float computedK = 0;
int r = (c >> 16) & 0xFF;
int g = (c >> 8) & 0xFF;
int b = (c >> 0) & 0xFF;
// BLACK
if (r==0 && g==0 && b==0) {
computedK = 1;
return "0 0 0 100";
}
computedC = 1 - (r/255f);
computedM = 1 - (g/255f);
computedY = 1 - (b/255f);
float minCMY = Math.min(computedC,Math.min(computedM,computedY));
if (1 - minCMY != 0){
computedC = (computedC - minCMY) / (1 - minCMY) ;
computedM = (computedM - minCMY) / (1 - minCMY) ;
computedY = (computedY - minCMY) / (1 - minCMY) ;
}
computedK = minCMY;
return (int)(computedC*100f) + " " + (int)(computedM*100f) + " " + (int)(computedY*100f) + " " + (int)(computedK*100f);
}