ram1
ram1

Reputation: 6470

What is the formula to convert a color in CMYK to hex?

What is the formula for converting a color encoded in CMYK to its hexadecimal equivalent?

Upvotes: 4

Views: 3204

Answers (2)

user160357
user160357

Reputation: 1506

It can be done with 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(k); // fixed a typo
    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;
    }
}

Upvotes: 2

Julian
Julian

Reputation: 4366

I have bad news for you: there is no simple formula. Cyan, magenta and yellow are complex ink colours and the translation to RGB depends on colour profiles. To make things worse, the CMYK colour space is smaller than the RGB colour space. Some pointers on colour theory:

Upvotes: 5

Related Questions