How can we convert CMYK to RGB in illustrator script?

2.5k views Asked by At

I fetch cmyk value for color and convert it into RGB and this RGB color is displayed in an html extension. But Color picker shows different values for RGB color due to which we get difference in color shades.i use normal java script method to convert CMYK to RGB.

Following formula that i used to convert CMYK to RGB--

CMYK to CMY

C = 1 - ( R / 255 )

M = 1 - ( G / 255 )

Y = 1 - ( B / 255 )

and then CMY TO RGB

R = ( 1 - C ) * 255

G = ( 1 - M ) * 255

B = ( 1 - Y ) * 255

Color picker shows different color as calculated from normal functions.

For eg : Color is Red.

In general

RGB : 255,0,0

CMYK : 0%,100%,100%,0%

HEXCODE : #FF0000

but in illustrator on double click of color picker it shows as

RGB : 237,28,36

CMYK : 0%,100%,100%,0%

HEXCODE : #ED1C24

So, is there any method or formula which can get the same values as calculated from the color picker .

Please Help ,thanks in advance.

please find attached image link for reference.

http://screencast.com/t/sYa1Y301Qa2

4

There are 4 answers

1
Magicprog.fr On

CMYK to RGB conversion:

The R,G,B values are given in the range of 0-255.

The red (R) color is calculated from the cyan (C) and black (K) colors:

R = 255 × (1-C) × (1-K)

The green color (G) is calculated from the magenta (M) and black (K) colors:

G = 255 × (1-M) × (1-K)

The blue color (B) is calculated from the yellow (Y) and black (K) colors:

B = 255 × (1-Y) × (1-K)

0
Mehdi On

There is no simple or a general conversion formula for RGB to CMYK. Basically, RGB is about present of optical color, while CMYK is about present of ink color. When you increase R, G and B you get brighter color, while in general CMYK acts opposite. I think these two articles can help you understand them in depth: Adobe RGB and Color space

The problem is that if you want to have exact values as softwares like illustrator or photoshop, you need to use ICC profile which is not accessible inside javascript (see this question: Is there any client-side technology able to convert sRGB to CMYK through an ICC color profile?).

However there are some mathematical conversions like just you did, or this python version, which converts the CMYK to CMY then to RGB (they are not standard ICC colors):

C = C * (1.0 - K) + K
M = M * (1.0 - K) + K
Y = Y * (1.0 - K) + K

R = 255 * (1.0 - C)
G = 255 * (1.0 - M)
B = 255 * (1.0 - Y)
2
Mohammed Abd El Aty On

PLEASE EXCUSE ME IN MY BAD ENGLISH !!

I THINK THAT I FIGURED IT OUT

THE PROBLEM IS THAT CMYK CYAN RETURNS TO LIGHT-CYAN IN RGB

AND ALSO CMYK PINK RETURNS TO REAL-BLUE IN RGB

SO I ADDED SOME VALUES :

IF CYAN > MAGENTA .. WE ADD 25% OF CYAN VALUE TO MAGENTA VALUE

ELSE IF CYAN < MAGENTA .. WE CUT 50% OF CYAN VALUE TO SHOW REAL-PINK

IT WORKED FOR ME AND HERE IS THE EXTENDSCRIPT CODE :

function CMYKTORGB1(_c, _m, _y, _k) {
    var C = _c / 100;   // THE PROBLEM THAT IT RETURNS LIGHT-CYAN IN RGB
    var M = _m / 100;
    var Y = _y / 100;
    var K = _k / 100;
    var _mc = 0;

    if (_c > 0) {
        if (_m >= _c) {
            _mc = _c / 2;
            C = (_c - _mc) / 100;   // THIS WILL FIX BLUE-PINK VALUE
        }
        else {
            _mc = _c / 4;
            M = (_m + _mc) / 100;   // THIS WILL FIX LIGHT-CYAN VALUE
        }
    }

    var R = 255 * (1 - C) * (1 - K);
    var G = 255 * (1 - M) * (1 - K);
    var B = 255 * (1 - Y) * (1 - K);

    if (R < 0) { R = 0; }
    if (G < 0) { G = 0; }
    if (B < 0) { B = 0; }

    return [R, G, B];
}

OR THIS FUNTION IT WORKING TOO :

function CMYKTORGB2(_c, _m, _y, _k) {
    var C = _c / 100;               // THE PROBLEM THAT IT RETURNS LIGHT-CYAN IN RGB
    var M = _m / 100;
    var Y = _y / 100;
    var K = _k / 100;
    var _mc = 0;

    if (_c > 0) {
        if (_m >= _c) {
            _mc = _c / 2;
            C = (_c - _mc) / 100;       // THIS WILL FIX BLUE-PINK VALUE
        }
        else {
            _mc = _c / 4;
            M = (_m + _mc) / 100;       // THIS WILL FIX LIGHT-CYAN VALUE
        }
    }

    var _C = C * (1.0 - K) + K
    var _M = M * (1.0 - K) + K
    var _Y = Y * (1.0 - K) + K

    var R = 255 * (1.0 - C)
    var G = 255 * (1.0 - M)
    var B = 255 * (1.0 - Y)

    if (R < 0) { R = 0; }
    if (G < 0) { G = 0; }
    if (B < 0) { B = 0; }

    return [R, G, B];
}
3
Vasily Hall On

In Illustrator scripting you will find use in the app.convertSampleColor() function.

This function takes in the source and destination space names as well as an array of color values and returns an illustrator-accurate converted color array in the destination space.