12 Replies Latest reply on Dec 16, 2014 2:14 PM by Laubender

    Exporting correct Hex colors via scripting

    DerKäse

      Hello all,

       

      I need to export the fillColor of a paragraph style via scripting.

       

      I can get the fillColor property of the paragraph style, and can also convert CMYK to Hex. However, I do realize that the color values shown in InDesign are in whatever Color Space the document is using.

       

      My question is, is there a native API to convert the color values to a web safe color value? If not, how do I go about mapping the colors?

       

      Additionally, I see that Adobe internally knows how to convert it to a proper hex color (screenshot attached - note the color property in the Export Tagging pane). If I cannot use some native API, is there a way to access the text in that pane?

       

      Screen Shot 2014-12-15 at 7.25.20 PM.png

       

      Thanks all, in advance!

        • 1. Re: Exporting correct Hex colors via scripting
          [Jongware] Most Valuable Participant

          You need to (1) convert the CMYK color space to RGB, then (2) find the closest 'web safe' value.

           

          Converting CMYK to RGB is just a matter of changing the color space, but you will find it's as accurate as all color space conversions (i.e., "not very"). However, there is no reasonable alternative.

           

          Changing the RGB values from 0..255 to the 216 color "web safe" palette is a matter of maths: there are 6 levels of R,G,B each.  Divide by 51, round, multiply by 51 again and you have right value. Note that your example shows #aaaad0, which is not one of the web safe colors. The nearest equivalent would be #9999cc.

          • 2. Re: Exporting correct Hex colors via scripting
            DerKäse Level 1

            Thanks for your reply!

             

            I'm actually not looking to transform it to a web safe palette. I'm looking for True Color (24 bit, 256 values for each R, G and B). I'm simply using chrome for testing the color; and making sure it looks the way it does in InDesign.

             

            The CMYK that I had was 33, 30, 3 and 0, which translates to an RGB of 171, 179, 247, which then translates to a hex value of #abb3f7.

            However, what I'm looking for is how do I map that CMYK color space (U.S. Web Coated SWOP V2), to an RGB, such that I get an output of 170, 170, 208 (aaaad0). If Adobe's doing it internally, so precisely, there's got to be a way!

            • 3. Re: Exporting correct Hex colors via scripting
              [Jongware] Most Valuable Participant

              Ah okay; you mentioned "web safe color" and I wondered why, as that is So 1995.

               

              It seems that internal conversion is not exposed to scripting. Off the record: using my trusty old CS4, I get different values for 33,30,3,0: 170,169,206, which is #AAA9CE in hex. So, very close to what you are seeing in the Export Tags dialog. I did not see any differences when assigning different color profiles, or possibly I did not do that the right way.

              • 4. Re: Exporting correct Hex colors via scripting
                DerKäse Level 1

                Oh no! I'm so sorry! I thought I had removed that part in the question, and made it "True Color", but I guess that's why I shouldn't write posts in a hurry! Long chain of edits that led to web safe colors. I also cannot seem to find an edit button to fix it!

                 

                Thanks for pointing that out btw!

                 

                Anyways, thanks a bunch for your prompt responses! You're right - I spent a couple hours yesterday going through almost the entire APIs and didn't see anything that caught my eye. Thought I might have missed out on something. BUT!!!! Progress - I *just* found out that if I account for the tint value, and then edit a current CMYK color to RGB color through the "Swatch Options" window in InDesign, I can get the exact RGB color that I'm looking for. That being said, I'm looking to see whether I can do this:

                 

                1. Programmatically create a new Color.
                  1. If I'm converting from a Tint instance (CMYK with Tint value), give the newly created color instance the updated (tint compensated) color values in CMYK.
                  2. Or, if it's not a tint, then simply give it the CMYK value.
                2. Set it's ColorSpace to RGB - I'm hoping this will trigger whatever internal fairy dust is being used.
                3. Read the new color values
                4. Celebrate victory.

                 

                I cannot seem to find a method to "create" a "new instance of Color", so I'm going to use the "duplicate" method of an existing color, and update it's color values, and use it as a scratch instance for converting all my colors.

                 

                If this works, I'll certainly update the post so that the community may make use of it!

                • 5. Re: Re: Exporting correct Hex colors via scripting
                  DerKäse Level 1

                  Win! It worked!!!

                   

                  Here's the complete solution -

                   

                  Assuming the original color is stored in a variable named color:

                   

                  var color = passedInColor; //the original color that we wish to convert.
                  
                  // Create a temporary color instance that we'll use to extract updated colorValues.
                  var scratchColor = workingDoc.colors.add({
                                           model: color.model,
                                           space: color.space,
                                           colorValue: color.colorValue
                                      });
                  
                  // Now, we force adobe's internal color conversion mechanism to trigger by changing the scratchColor's color space.
                  scratchColor.space = ColorSpace.RGB;
                  var updatedValues = scratchColor.colorValue; // Updated values now has the properly mapped and converted RGB values.
                  // You may need to round off the R, G and B values in the updatedValues array.
                  // And that's it!
                  

                   

                  Why am I not surprised that it had to be a hacky work around?! *sigh*. Working on this platform has been so so so frustrating! I do hope adobe gets its act together!

                  • 6. Re: Re: Exporting correct Hex colors via scripting
                    Laubender Adobe Community Professional & MVP

                    @DerKäse (btw. funny name) – there is an add() method for colors that will take a Object Color as argument.

                    Use that for your purpose.

                     

                    Here an example for RGB:

                     

                    var myDoc = app.documents[0];
                    var myColorName = "myNewRGBColor";
                    
                    if(!myDoc.swatches.itemByName(myColorName).isValid){
                       
                        var myNewColor = myDoc.colors.add(
                            {
                               
                                name:myColorName,
                                colorValue:[255,255,0], //A bright yellow
                                model:ColorModel.PROCESS,
                                space:ColorSpace.RGB
                               
                                }
                            );
                           
                        };
                    

                     

                    Here one for CMYK:

                     

                    var myDoc = app.documents[0];
                    var myColorName = "myNewCMYKColor";
                    
                    if(!myDoc.swatches.itemByName(myColorName).isValid){
                        
                        var myNewColor = myDoc.colors.add(
                            {
                                
                                name:myColorName,
                                colorValue:[0,0,100,0], //A bright yellow
                                model:ColorModel.PROCESS,
                                space:ColorSpace.CMYK
                                
                                }
                            );
                            
                        };
                    

                     

                    Hope, that helps…

                     

                    Uwe

                    • 7. Re: Re: Exporting correct Hex colors via scripting
                      Laubender Adobe Community Professional & MVP

                      Oops, too late… ;-)

                       

                      Uwe

                      • 8. Re: Re: Exporting correct Hex colors via scripting
                        DerKäse Level 1

                        Thanks a bunch anyway! This is corroboration that I'm doing it the right way!

                        • 9. Re: Re: Re: Exporting correct Hex colors via scripting
                          DerKäse Level 1

                          Oh! One more thing I forgot to add!

                           

                          Always cleanup after yourselves guys!

                           

                          // Make sure you delete the scratch color once you're done with it!
                          scratchColor.remove();
                          
                          

                           

                           

                          Hah! Glad you get the name Laubender!!

                          • 10. Re: Re: Exporting correct Hex colors via scripting
                            Laubender Adobe Community Professional & MVP

                            Not to spoil your triumph, but:

                             

                            What makes you so sure about your assumption, that all rules in the color conversion mechanism defined in your color settings of InDesign are applied? Eg. in my tests in the UI of InDesign I'm getting confusing results when changing a CMYK swatch to an RGB one:

                             

                            InDesign CMYK Swatch:

                            0,0,100,0 CMYK (+ ISO Coated v2 300%) => sRGB = 251,224,21

                            0,0,100,0 CMYK (+ ISONewspaper26v4) => sRGB = 251,224,21

                             

                            In my tests both conversions yield the same values.
                            And in my opinion that could not be right!

                             

                            Take PhotoShop and do a conversion for the same CMYK values with different CMYK ICC-Profiles.

                            Check the converted values. They will be different.

                             

                            PhotoShop Pixel Area filled with 0,0,100,0 CMYK:

                             

                            0,0,100,0 CMYK (+ ISO Coated v2 300%) => sRGB = 251,224,21

                            0,0,100,0 CMYK (+ ISONewspaper26v4) => sRGB = 248,221,0

                             

                            Your values might vary depending on the conversion module (Adobe (ACE) vs. Apple CMM) or the color conversion priority (perceptive vs. relative color metric etc.pp.).

                             

                            (I ran all my tests with InDesign CS5.5 and PhotoShop CS5.1)

                             

                            Uwe

                            • 11. Re: Re: Exporting correct Hex colors via scripting
                              DerKäse Level 1

                              Good question! And this is an awesome test!

                               

                              So firstly, apropos this specific use case (the reason why I'm writing the script); I'm provided with standard color settings; thereby always using U.S. Web Coated (SWOP) v2. So it seems to *visually* work.

                               

                              Now, considering the broader question of color settings, which is good, because we (or others) might use this under different use cases: I'm doing this on CS6.

                               

                              When I go to Edit > Convert to Profile (check the preview box), and select a different profile, my color values change, and the way the color looks is also different. Owing to that, the RGB output is different.

                               

                              I tried another test, wherein I changed my color setting to Coated FOGRA39(ISO 12647-2:2004), created a new swatch, gave it the same CMYK values as before, and the converted RGB values were different.

                               

                              Are you using a different method/test case of changing these?

                              • 12. Re: Re: Exporting correct Hex colors via scripting
                                Laubender Adobe Community Professional & MVP

                                Yes. Could be, that something changed from CS5.5 (my version I did the tests) to CS6. Or its something in the color conversion policies. Whatever, I will do some more tests tomorrow. It's too late here in Germany right now (11 pm) and I need some sleep.

                                 

                                Uwe