java – RGB到CMYK和返回算法

前端之家收集整理的这篇文章主要介绍了java – RGB到CMYK和返回算法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图实现一个解决方案来计算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,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,B};
        }
    }

解决方法

正如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();
        }
    }
}
原文链接:https://www.f2er.com/java/125309.html

猜你在找的Java相关文章