Skip to main content
Inspiring
November 23, 2010
Question

[CS5 - JS] Convert InDesign colors to Hex

  • November 23, 2010
  • 1 reply
  • 4884 views

Hi,

I assembled the following functions in order to convert InDesign colors to theyr hex value.

I tested it with some colors but RGB conversion is not working as expected.

function colorToHex(color, tint){

    switch(color.space){

        case ColorSpace.CMYK:

        return cmykToHex((color.colorValue[0] * tint) / 100, (color.colorValue[1] * tint) / 100, (color.colorValue[2] * tint) / 100, (color.colorValue[3] * tint) / 100);

            break;

        case ColorSpace.RGB:

        return cp_RgbToHex((color.colorValue[0] * tint) / 100, (color.colorValue[1] * tint) / 100, (color.colorValue[2] * tint) / 100);

            break;

        default:

        return "";

            break;

    }

}

function cmykToHex(c, m, y, k){

    c = c/100;

    m = m/100;

    y = y/100;

    k = k/100;

    var R = (1 - (c * (1 - k) + k)) * 255;

    var G = (1 - (m * (1 - k) + k)) * 255;

    var B = (1 - (y * (1 - k) + k)) * 255;

    return rgbToHex(R, G, B);

}

function rgbToHex(R, G, B){

    return "#" + nToHex(R) + nToHex(G) + nToHex(B);

}

function nToHex(N){

    if (N == 0 || isNaN(N)){

        return "00";

    }

    N = Math.max(0,N);

    N = Math.min(N,255);

    N = Math.round(N);

    return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);

}

Do you see something wrong?

Thanks!

This topic has been closed for replies.

1 reply

Harbs.
Legend
November 23, 2010

What's not working? I just tested and it seems to work fine.

The only thing I see wrong is your function name (cp_RgbToHex instead of rgbToHex).

Harbs

Inspiring
November 23, 2010

Apart from that function name problem, the fact is that conversion do not gives me the right HEX value.

I make a CMYK color in Fireworks: 75 - 5 - 100 - 0.

RGB values are 50 - 151- 55.

Hex value is #329737.

If I try to convert the same CMYK color from InDesign with my functions I get this HEX: #40F200 (RGB values are: 64 - 242 - 0).

Jongware
Community Expert
Community Expert
November 23, 2010

Well, that's actually a different issue.

Your CMYK-to-RGB conversion is extremely basic. There is no straightforward conversion; both InDesign and Fireworks use advanced look-up tables to get an accurate-as-possible value; it also depends on your actual document Color Profile (etc.) settings.

If you want to get the 'good' RGB value, let InDesign convert the color from CMYK to RGB for you by changing the ColorSpace property of the swatch.