Skip to main content
April 15, 2009
Question

Color Conversion

  • April 15, 2009
  • 2 replies
  • 4126 views

Hi All,

I am working on the Illustrator CS3 SDK for color to gray scale conversion. I have tried the same using VB Script DoAction(using Actions) method since there is no direct method available to convert color to "Garyscale", it quite painful in handling live production.To avoid these problems, I read the SDK documentation and found "AIColorConversionSuite", "AIOverrideColorConversionSuite" methods. Whether color to grayscale conversion can achieved using these suite? If any sample or advice is helpful for me.

Regards,

Selvakumar

This topic has been closed for replies.

2 replies

Toto RoToTO
Inspiring
April 20, 2009

here it is a color conversion method i use.

hoping this will help.

int
ColorConverter::ConvertColor(AIColor* srcCol, AIColorConversionSpaceValue dstSpace, AIColor *dstCol, AIReal srcAlpha, AIReal *dstAlpha)
{
    // determine srcCol's space and sample size:
    AIColorConversionSpaceValue srcSpace;
    int srcSize;
    bool srcHasAlpha = srcAlpha < 1;
    switch (srcCol->kind) {
        case kGrayColor:
            srcSize = 1;
            if (srcHasAlpha) srcSpace = kAIAGrayColorSpace;
            else srcSpace = kAIGrayColorSpace;
            break;
        case kThreeColor:
            srcSize = 3;
            if (srcHasAlpha) srcSpace = kAIARGBColorSpace;
            else srcSpace = kAIRGBColorSpace;
            break;
        case kFourColor:
            srcSize = 4;
            if (srcHasAlpha) srcSpace = kAIACMYKColorSpace;
            else srcSpace = kAICMYKColorSpace;
            break;
        default:
            return -1;
    }

    bool dstHasAlpha = dstSpace == kAIACMYKColorSpace || dstSpace == kAIARGBColorSpace || dstSpace == kAIAGrayColorSpace;

    if (srcSpace >= 0 && dstSpace >= 0) {
        AISampleComponent src[5];
        AISampleComponent dst[5];
        memcpy(src, &srcCol->c, srcSize * sizeof(AISampleComponent));
        if (srcHasAlpha) src[srcSize] = srcAlpha;
        // TODO: why swapping kGrayColor???
        if (srcCol->kind == kGrayColor) src[0] = 1.0f - src[0];
        ASBoolean inGamut;

        AIColorConvertOptions options;
        sAIColorConversion->ConvertSampleColor(srcSpace, src, dstSpace, dst, options, &inGamut);


        if (dstCol == NULL)
            dstCol = new AIColor;
        // init the destCol with 0
        // memset(dstCol, 0, sizeof(AIColor));
        // determine dstCol's kind and sampleSize:
        int dstSize;
        switch (dstSpace) {
            case kAIMonoColorSpace:
            case kAIGrayColorSpace:
            case kAIAGrayColorSpace:
                dstCol->kind = kGrayColor;
                dstSize = 1;
                break;
            case kAIRGBColorSpace:
            case kAIARGBColorSpace:
                dstCol->kind = kThreeColor;
                dstSize = 3;
                break;
            case kAICMYKColorSpace:
            case kAIACMYKColorSpace:
                dstCol->kind = kFourColor;
                dstSize = 4;
                break;
            default:
                return NULL;
        }
        // TODO: why swapping kGrayColor???
        if (dstCol->kind == kGrayColor) dst[0] = 1.0f - dst[0];
        memcpy(&dstCol->c, dst, dstSize * sizeof(AISampleComponent));
        // get back alpha:
        if (dstAlpha != NULL)
            *dstAlpha = dstHasAlpha ? dst[dstSize] : 1;
       
        return 0;
    }
    return -1;
}

best regards,

Thomas.

May 2, 2009

Hi Thomas,

I had tried the sample code you have given but thorws exception.

Error 1 error C2653: 'ColorConverter' : is not a class or namespace name
Error 2 error C2065: 'sAIColorConversion' : undeclared identifier                - Only kAIColorConversionSuite only available in Windows CS3 SDK

Any suggestion on this?

Regards,

Selvakumar

Toto RoToTO
Inspiring
May 4, 2009

error 1:  ColorConverter is an helper class i have created, only for color conversion. So you have to create this class, or add a function in your cpp file as follow.

//after include and define

int ConvertColor(AIColor* srcCol, AIColorConversionSpaceValue dstSpace, AIColor *dstCol, AIReal srcAlpha, AIReal *dstAlpha);

//then

int
ColorConverter::ConvertColor(AIColor* srcCol, AIColorConversionSpaceValue dstSpace, AIColor *dstCol, AIReal srcAlpha, AIReal *dstAlpha)
{
   //function code here..........
}

error 2:

You have to load AIColorConversionSuite suite. just take a look at suite.h and common.h.

A. Patterson
Inspiring
April 15, 2009

You can definitely convert form RGB or CYMK (or a couple of others I don't actually know how to use) into gray scale. You basically just call AIColorConversionSuite::ConvertSampleColor() and give it the to & from colour spaces along with a couple of arrays representing the source colour & a buffer in which to stick the result. There are some options (AIColorConvertOptions) and a gamut boolean, but its fairly straightforward.

A. Patterson
Inspiring
April 15, 2009

Here's a quick and dirty example showing CYMK -> RGB:

AIBoolean inGamut = false;

float rgb[3];

float cmyk[4];

cmyk[0] = localColour.c.f.cyan;

cmyk[1] = localColour.c.f.magenta;

cmyk[2] = localColour.c.f.yellow;

cmyk[3] = localColour.c.f.black;

error = sColorConversion->ConvertSampleColor( kAICMYKColorSpace, cmyk, kAIRGBColorSpace,  rgb, kDefault, &inGamut);

April 16, 2009

Thanks for your sample code. I had tried but it seems there is no conversion happened. Please find the code,

AIColorConversionSuite *colorConv;

//AIBoolean inGamut = false;

ASBoolean inGamut=FALSE;

AIColor *aiColor;

float gray[1];

float cmyk[4];

cmyk[0]=aiColor->c.f.cyan;

cmyk[1]=aiColor->c.f.magenta;

cmyk[2]=aiColor->c.f.yellow;

cmyk[3]=aiColor->c.f.black;

AIColorConvertOptions *colorOpt;

result=colorConv->ConvertSampleColor(kAICMYKColorSpace,cmyk,kAIGrayColorSpace,gray,colorOpt->kDefault,&inGamut);

Selected a single object in illustrator and converted. If any thing wrong in this code?

Thanks,

Selvakumar