4

将 CMYK 编码的颜色转换为其十六进制等效值的公式是什么?

4

2 回答 2

5

我要告诉你一个坏消息:没有简单的公式。青色、品红色和黄色是复杂的墨水颜色,转换为 RGB 取决于颜色配置文件。更糟糕的是,CMYK 颜色空间小于 RGB 颜色空间。关于色彩理论的一些提示:

于 2012-04-26T00:11:08.573 回答
2

它可以用javascript完成:

//converts cmyk to hex
function cmykToHex(c,m,y,k) {
    var hex,
        rgb;
    //convert cmyk to rgb first
    rgb = cmykToRgb(c,m,y,k);
    //then convert rgb to hex
    hex = rgbToHex(rgb.r, rgb.g, rgb.b);
    //return hex color format
    return hex;
}
//converts cmyk color to rgb
function cmykToRgb(c, m, y, k) {
var rgb_r,
    rgb_g,
    rgb_b,
    cyan = 100 *Number(c),
    magenta = 100 * Number(m),
    yellow = 100 * Number(y),
    black = 100 * Number(m);
    0 < cyan ? cyan /= 100 : 0 < magenta ? magenta /= 100 : 0 < yellow ? yellow /= 100 : 0 < black && (black /= 100);
    rgb_r = 1 - Math.min(1, cyan * (1 - black) + black);
    rgb_g = 1 - Math.min(1, magenta * (1 - black) + black);
    rgb_b = 1 - Math.min(1, yellow * (1 - black) + black);
    rgb_r = Math.round(255 * rgb_r);
    rgb_g = Math.round(255 * rgb_g);
    rgb_b = Math.round(255 * rgb_b);
    return {r: rgb_r, g: rgb_g, b: rgb_b};
}
//converts rgb to hex
function rgbToHex(r, g, b) {
    return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
    function componentToHex(c) {
        var hex = c.toString(16);
        return hex.length === 1 ? "0" + hex : hex;
    }
}
于 2020-01-29T10:39:56.903 回答