-->

How to convert CMYK to RGB programmatically in ind

2020-04-19 08:54发布

问题:

I have a CMYK colorspace in indesign, i want to convert that as RGB color space, I got some codes, but I am getting incorrect data.

Some of the codes which I tried are given below

    double cyan = 35.0;
    double magenta = 29.0;
    double yellow = 0.0;
    double black = 16.0;

    cyan = Math.min(255, cyan + black); //black is from K
    magenta = Math.min(255, magenta + black);
    yellow = Math.min(255, yellow + black);
    l_res[0] = 255 - cyan;
    l_res[1] = 255 - magenta;
    l_res[2] = 255 - yellow;

@Override
public float[] toRGB(float[] p_colorvalue) {
    float[] l_res = {0,0,0};
    if (p_colorvalue.length >= 4)
    {
        float l_black = (float)1.0 - p_colorvalue[3];
        l_res[0] = l_black * ((float)1.0 - p_colorvalue[0]);
        l_res[1] = l_black * ((float)1.0 - p_colorvalue[1]);
        l_res[2] = l_black * ((float)1.0 - p_colorvalue[2]);
    }
    return (l_res);
}

The values are C=35, M = 29, Y = 0, K = 16 in CMYK color space and the correct RGB values are R = 142, G = 148, B = 186.

In adobe indesign, using swatches we can change the mode to CMYK or to RGB.

But I want to do that programmatically, Can I get any algorithm to convert CMYK to RGB which will give the correct RGB values.

And one more question, if the alpha value for RGB is 1 then what will be the alpha value for CMYK?

Can anyone help me to solve these issues... Thanks in advance.

回答1:

To answer your last question:

If the alpha is 1 in RGB colour space it will be 1 in CMYK colour space to. Both spaces are just specifying the colours, not the transparency.

On your CMYK to RGB conversion problem, you should note that

There is no exact conversion between the CMYK and RGB models - both color spaces cover different color gamuts

Source

There's also a discussion on this Wikipedia page



回答2:

Color is subjective.

When converting CMYK to RGB there are direct conversion formulas, as you have found out and seem to have code for. They are, however, too precise.

When converting from CMYK to RGB you need to use a color profile which describes the device and substrate the CMYK inks would be laid down on.

Therefore:

  1. You have image.tif which is a CMYK image.
  2. You need to apply an ICC profile describing the intended output device for that image. (i.e. USWebCoatedSWOP.icc for an image to be created on a flexographic printing press using a coated substrate.)

Following these steps will get you the proper colored image and "correct" RGB values.

So, applying that knowledge to your situation, you would need to programmatically apply an ICC profile.

If you insist on doing the math yourself, (I recommend you avoid this!!!) you can read all about it on the ICC website or code something yourself using LittleCMS.

I don't believe that a COM based programming interface is exposed through InDesign. However, Photoshop, Illustrator and Acrobat all expose some COM based APIs.

I don't know if this is possible using Adobe's exposed API calls, however, I can say that it is possible to do this programmatically through CorelDRAW.

Edit: Apparently, InDesign does have automation capability, however, I could not find any functionality to apply color profiles programmatically. See section (1.5.9. Color).



回答3:

What you have are just four numbers (for CMYK), and you want to derive three numbers from that (for RGB).

Unfortunately your numbers are just that - numbers. To specify a color, more than a quadruple or triple is needed. There are a number of ways how that could be done but the most widely used is associated with the ICC architecture (cf. www.color.org), using concepts like color space, profile connection space, rendering intents, lighting condition etc. In very simply terms: you' need a source profile to charactrize the CMYK numbers you are coming from, and a destination profile for the RGB you wish to derive from your four numbers. Then you could use ICC based color conversion technology (like the open source LittleCMS library, or similar features built into Mac OS, Windows OS or various Adobe products) to carry out the conversion.

It looks like you want to match behavior found elsewhere - unless you find out enough how that "behavior found elsewhere" is exactly doing its job it will be very difficult to come up with an approach that matches it precisely. Unfortunately a lot of those 'behaviors' are substantially under-specified or under-documented.

If you can elaborate on the reasons of why you are doing what your doing, it might be possible to come up with morre useful suggestions.



回答4:

double cyan = 35.0/100.0;
double magenta = 29.0/100.0;
double yellow = 0.0;
double black = 16.0/100.0;

First You divide every value by 100 since it nedd to come in the range of 0 to 1 then

        R=255*(1-cyan)*(1-black);
        G=255*(1-magneta)*(1-black);
        B=255*(1-yellow)*(1-black);

In this way we will get almost approximate values



回答5:

If you are using InDesign scripting, why do you want to convert colors on your own? Simply ask InDesign to do it for you.

app.colorTransform (array of numbers in source color space, source color space, destination color space);

The code above will return an array of numbers representing the color in destination color space.