Skip to main content
Inspiring
September 10, 2010
Answered

RGB in colorValue, Possible?

  • September 10, 2010
  • 3 replies
  • 4247 views

Hello All,

I have a Hex Color value in a variable and I can convert that to RGB. I want to use that in a style, so I can apply that style to a character string. But I don’t know how to pass a RGB value to fillColor property.
Is it even possible to use an RGB value in this?
fillColor:myDocument.colors.add({colorValue:[RGB VALUE??]})
Or is there a totally deferent way to work with RGB?

Any help highly appreciated.

Thanks
Sam

This topic has been closed for replies.
Correct answer Jongware

The color array you provide has to conform to the color space that gets applied to the new color; I think ColorSpace.CMYK is the default for a new color swatch.

If you set the color space as well as the color values, entering RGB is okay. You cannot enter then in hex, you need to convert them first to decimals (I think you already did that).

myDocument = app.activeDocument;

app.selection[0].fillColor = myDocument.colors.add({space:ColorSpace.RGB, colorValue:[parseInt("AB",16), parseInt("1E",16), parseInt("FF",16)]});

3 replies

SamanAuthor
Inspiring
September 10, 2010

Thank you very much guys! Both answers saved my day! Really appreciated.

tomaxxi, Nice blog! right in to my bookmark!

tomaxxi
Inspiring
September 10, 2010

Hey!

This will help you add color to swatches. Also it first checks do you already have color with same name. First line shows you how to use this function.

myColorAdd(app.activeDocument, "My RGB Color", ColorModel.PROCESS, ColorSpace.RGB, [0, 100, 0]);

function myColorAdd(myDocument, myColorName, myColorModel, myColorSpace, myColorValue){
  try{
    myColor = myDocument.colors.item(myColorName);

    myName = myColor.name;
  }
  catch (myError){
    myColor = myDocument.colors.add();
    myColor.properties = {name:myColorName, model:myColorModel, space:myColorSpace ,colorValue:myColorValue};
  }
}

Hope it helps.

--

tomaxxi

http://indisnip.wordpress.com/

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
September 10, 2010

The color array you provide has to conform to the color space that gets applied to the new color; I think ColorSpace.CMYK is the default for a new color swatch.

If you set the color space as well as the color values, entering RGB is okay. You cannot enter then in hex, you need to convert them first to decimals (I think you already did that).

myDocument = app.activeDocument;

app.selection[0].fillColor = myDocument.colors.add({space:ColorSpace.RGB, colorValue:[parseInt("AB",16), parseInt("1E",16), parseInt("FF",16)]});

tomaxxi
Inspiring
September 10, 2010

Here is these two functions combined in one multifunctional that will accept HEX or RGB (in array):

myColorAdd(app.activeDocument, "My RGB Color", ColorModel.PROCESS, ColorSpace.RGB, "224499" /*[34,68,153]*/);

function myColorAdd(myDocument, myColorName, myColorModel, myColorSpace, myColorValue){
    if(myColorValue instanceof Array != true){
        var myHEXstr = Array();
        for(var i = 0; i < 3; i++)myHEXstr.push(parseInt(myColorValue.substr (i*2, 2), 16));
        myColorValue = myHEXstr;
    }
    try{
        myColor = myDocument.colors.item(myColorName);
        myName = myColor.name;
    }
    catch (myError){
        myColor = myDocument.colors.add();
        myColor.properties = {name:myColorName, model:myColorModel, space:myColorSpace ,colorValue:myColorValue};
    }
}

--

tomaxxi

http://indisnip.wordpress.com/

Jongware
Community Expert
Community Expert
September 10, 2010

Heh -- I think I would have written the hex-to-value thingy as something like this

myColorValue = [ (parseInt(myColorValue,16) >> 16) & 0xff,
   (parseInt(myColorValue,16) >> 8) & 0xff,
    parseInt(myColorValue,16) & 0xff ];

Perhaps yours is a bit more clear ... but it will fail on "012"

(... but you could add "000000" to the front of the string and rewrite the parseInt stuff to count backwards from the end.)

If you return the newly added 'myColor' -- or the one found when the name already exists --, you can use this:

someObject.fillColor = myColorAdd (...etc.);

which might be useful if the color only is needed once. (Using it more often than once isn't actually a problem, thanks to your checking.)