13 Replies Latest reply on Jan 18, 2013 6:07 AM by RLind1

    RGB in colorValue, Possible?

    Saman Level 1

      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

        • 1. Re: RGB in colorValue, Possible?
          [Jongware] Most Valuable Participant

          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)]});
           
          

          • 2. Re: RGB in colorValue, Possible?
            Marijan Tompa [tomaxxi] Level 4

            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/

            1 person found this helpful
            • 3. Re: RGB in colorValue, Possible?
              Saman Level 1

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

              tomaxxi, Nice blog! right in to my bookmark!

              • 4. Re: RGB in colorValue, Possible?
                Marijan Tompa [tomaxxi] Level 4

                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/

                • 5. Re: RGB in colorValue, Possible?
                  [Jongware] Most Valuable Participant

                  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.)

                  • 6. Re: RGB in colorValue, Possible?
                    Marijan Tompa [tomaxxi] Level 4

                    Thanks Jongware for pointing out my mistake

                     

                    --

                    tomaxxi

                    • 7. Re: RGB in colorValue, Possible?
                      Marijan Tompa [tomaxxi] Level 4

                      Ok, i mixed up all and end up with this:

                       

                      myColorAdd(app.activeDocument, "My RGB Color", ColorModel.PROCESS, [80,50,30,10]);
                      
                      function myColorAdd(myDocument, myColorName, myColorModel, myColorValue){
                        if(myColorValue instanceof Array != true){
                          myColorValue = [(parseInt(myColorValue,16) >> 16) & 0xff, (parseInt(myColorValue,16) >> 8) & 0xff, parseInt(myColorValue,16) & 0xff ];
                          myColorSpace = ColorSpace.RGB;
                        }else{
                          if(myColorValue.length == 3)
                            myColorSpace = ColorSpace.RGB;
                          else
                            myColorSpace = ColorSpace.CMYK;
                        }
                        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};
                        }
                        return myColor;
                      }
                      

                       

                      That's it, nice and handy

                       

                      --

                      tomaxxi

                      http://indisnip.wordpress.com/

                      • 8. Re: RGB in colorValue, Possible?
                        Muthuraj Durairaj Level 1

                        Hi Jongware,

                         

                        You are always a great help for me.

                         

                        I am trying to use the existing Indesign CS4 inbuilt FindChangeByList.jsx by modifying the support .txt file. I just want to highlight the changed content with yellow color. I have some yellow color in my template and I have used the color name in the .txt file and it is working fine.

                         

                        grep {findWhat:"—"} {changeTo:" – ", underline:true, underlineColor:"4_PMS 109_secondary", underlineOffset:"-3.75 pt", underlineWeight:"12 pt"} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:true, caseSensitive:true}

                         

                        The template color may get changed over the period of time and I dont want to use some name of the color and want to use CMYK values for the color. Is this possible to use C=0 M=0 Y=100 K=0 as color name?

                         

                        I tried the following and it results in highlighting Black color. Is there anything wrong in this? Please help me.

                         

                        grep {findWhat:"—"} {changeTo:" – ", underline:true, underlineColor:"C=0 M=0 Y=100 K=0", underlineOffset:"-3.75 pt", underlineWeight:"12 pt"} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:true, caseSensitive:true}

                         

                        Regards,

                        Muthuraj. D

                        • 9. Re: RGB in colorValue, Possible?
                          John Hawkinson Level 5
                          The template color may get changed over the period of time and I dont want to use some name of the color and want to use CMYK values for the color. Is this possible to use C=0 M=0 Y=100 K=0 as color name?

                          No. You must use the name of a color swatch (or the swatch itself).

                          You can create a color named C=0 M=0 Y=100 K=0 first and then use that name.

                          • 10. Re: RGB in colorValue, Possible?
                            Muthuraj Durairaj Level 1

                            Hi John,

                             

                            Thanks for your quick reply.

                             

                            I have added this following code to create a new color with name "YellowHighlight" in the .indd document, and update the supporting .txt file with this color name. It is working fine now. Thanks.

                             

                            myColorAdd(app.activeDocument, "YellowHighlight", ColorModel.PROCESS, ColorSpace.CMYK, [0, 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};
                              }
                            }

                             

                            Regards,

                            Muthuraj. D

                            • 11. Re: RGB in colorValue, Possible?
                              RLind1 Level 1

                              Folks-

                               

                              I'm using Adobe InDesign CS6 and when I try to add an RGB color using colors.add as shown above and on http://indisnip.wordpress.com/2010/09/11/quicktip-add-custom-cmykrgbhex-colors-to-document / , I get an error "Object is Invalid".

                               

                              Help would be appreciated!

                               

                              -RL

                              • 12. Re: RGB in colorValue, Possible?
                                [Jongware] Most Valuable Participant

                                You also shuld be getting a line number. What does that line say in your script?

                                • 13. Re: RGB in colorValue, Possible?
                                  RLind1 Level 1

                                  sorry- Line 6:

                                  myName = myColor.name;