14 Replies Latest reply on Mar 15, 2012 1:04 PM by Paeon

    [CS5 - JS] Convert InDesign colors to Hex

    Emanuele Radaelli Level 1

      Hi,

      I assembled the following functions in order to convert InDesign colors to theyr hex value.

      I tested it with some colors but RGB conversion is not working as expected.

       

       

      function colorToHex(color, tint){

          switch(color.space){

              case ColorSpace.CMYK:

              return cmykToHex((color.colorValue[0] * tint) / 100, (color.colorValue[1] * tint) / 100, (color.colorValue[2] * tint) / 100, (color.colorValue[3] * tint) / 100);

                  break;

              case ColorSpace.RGB:

              return cp_RgbToHex((color.colorValue[0] * tint) / 100, (color.colorValue[1] * tint) / 100, (color.colorValue[2] * tint) / 100);

                  break;

              default:

              return "";

                  break;

          }

      }

       

       

      function cmykToHex(c, m, y, k){

          c = c/100;

          m = m/100;

          y = y/100;

          k = k/100;

          var R = (1 - (c * (1 - k) + k)) * 255;

          var G = (1 - (m * (1 - k) + k)) * 255;

          var B = (1 - (y * (1 - k) + k)) * 255;

          return rgbToHex(R, G, B);

      }

       

       

      function rgbToHex(R, G, B){

          return "#" + nToHex(R) + nToHex(G) + nToHex(B);

      }

       

       

      function nToHex(N){

          if (N == 0 || isNaN(N)){

              return "00";

          }

          N = Math.max(0,N);

          N = Math.min(N,255);

          N = Math.round(N);

          return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);

      }

       

      Do you see something wrong?

      Thanks!

        • 1. Re: [CS5 - JS] Convert InDesign colors to Hex
          Harbs. Level 6

          What's not working? I just tested and it seems to work fine.

           

          The only thing I see wrong is your function name (cp_RgbToHex instead of rgbToHex).

           

          Harbs

          • 2. Re: [CS5 - JS] Convert InDesign colors to Hex
            Emanuele Radaelli Level 1

            Apart from that function name problem, the fact is that conversion do not gives me the right HEX value.

            I make a CMYK color in Fireworks: 75 - 5 - 100 - 0.

            RGB values are 50 - 151- 55.

            Hex value is #329737.

             

            If I try to convert the same CMYK color from InDesign with my functions I get this HEX: #40F200 (RGB values are: 64 - 242 - 0).

            • 3. Re: [CS5 - JS] Convert InDesign colors to Hex
              [Jongware] Most Valuable Participant

              Well, that's actually a different issue.

               

              Your CMYK-to-RGB conversion is extremely basic. There is no straightforward conversion; both InDesign and Fireworks use advanced look-up tables to get an accurate-as-possible value; it also depends on your actual document Color Profile (etc.) settings.

               

              If you want to get the 'good' RGB value, let InDesign convert the color from CMYK to RGB for you by changing the ColorSpace property of the swatch.

              1 person found this helpful
              • 4. Re: [CS5 - JS] Convert InDesign colors to Hex
                Emanuele Radaelli Level 1

                Thanks for your suggestion Jongware.

                Now I'm curious about what InDesign do while converting... what do you mean with "advanced look-up tables"?

                Is there a way via code to make my routine less basic?

                 

                Thanks.

                • 5. Re: [CS5 - JS] Convert InDesign colors to Hex
                  [Jongware] Most Valuable Participant

                  What I meant to say was "InDesign uses an advanced internal system". There is simply no way you could do the same with Javascript.

                   

                  But to use InDesign's color conversion, all you have to do is

                   

                  If you want to get the 'good' RGB value, let InDesign convert the color from CMYK to RGB for you by changing the ColorSpace property of the swatch.

                   

                   

                   

                  • 6. Re: [CS5 - JS] Convert InDesign colors to Hex
                    Harbs. Level 6

                    There's the issue of GCR and RGB / CMYK conversions are very device dependent...

                     

                    Harbs

                    • 7. Re: [CS5 - JS] Convert InDesign colors to Hex
                      [Jongware] Most Valuable Participant

                      Yup. I'd have to test it [*], but if you change the color space by script or manually (in the UI), wouldn't ID automatically use the current document profile? That would make some sense.

                       

                      [*] I have no idea how to test it. I don't know *anything* about color profiles, cmyk conversions, ICC, or CMS, but I'm able to lead a productive and happy live nevetheless.

                      • 8. Re: [CS5 - JS] Convert InDesign colors to Hex
                        Emanuele Radaelli Level 1

                        Thanks guys.

                        Hope this would help...

                         

                         

                        var selection = app.selection[0];

                        colorToHex(selection.fillColor, selection.fillTint);

                         

                        function colorToHex(color, tint){

                            switch(color.space){

                                case ColorSpace.CMYK:

                                return cmykToHex(color, tint);

                                break;

                                case  ColorSpace.RGB:

                                return rgbToHex((color.colorValue[0] * tint) / 100, (color.colorValue[1] * tint) / 100, (color.colorValue[2] * tint) / 100);

                                break;

                                default:

                                returnValue = "";

                            }   

                        }

                         

                        function cmykToHex(color, tint){

                            var newDoc = app.documents.add()

                            newDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;

                            newDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

                            newDoc.documentPreferences.pageWidth = 100;

                            newDoc.documentPreferences.pageHeight = 100;

                            newCol = newDoc.colors.add();

                            newCol.name = "Color";

                            newCol.space = ColorSpace.CMYK;

                            newCol.model = ColorModel.PROCESS;

                            newCol.colorValue = [(color.colorValue[0] * tint) / 100, (color.colorValue[1] * tint) / 100, (color.colorValue[2] * tint) / 100, (color.colorValue[3] * tint) / 100];

                            newCol.space = ColorSpace.RGB;

                            rgbToHex(Math.round(newCol.colorValue[0]), Math.round(newCol.colorValue[1]), Math.round(newCol.colorValue[2]));

                            newDoc.close(SaveOptions.NO);

                        }

                         

                        function rgbToHex(R, G, B){

                            alert("#" + nToHex(R) + nToHex(G) + nToHex(B));

                            return nToHex(R) + nToHex(G) + nToHex(B);

                        }

                         

                        function nToHex(N){

                            if (N == 0 || isNaN(N)){

                                return "00";

                            }

                            N = Math.max(0,N);

                            N = Math.min(N,255);

                            N = Math.round(N);

                            return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);

                        }

                         

                        Obviously I could create a new color object within the document...

                        Cheers!

                         

                        P.S: What's GCR?

                        • 9. Re: [CS5 - JS] Convert InDesign colors to Hex
                          Harbs. Level 6

                          GCR = Gray Component Replacement

                           

                          Harbs

                          • 10. Re: [CS5 - JS] Convert InDesign colors to Hex
                            Paeon Level 1

                            I'm getting an error on line 8, saying that space is not a method of color. (of course it is, but what do I do?)

                            • 11. Re: [CS5 - JS] Convert InDesign colors to Hex
                              [Jongware] Most Valuable Participant

                              There is no 'space' on line 8. But there is, at line 4 or 5 (depending on if you/Extendscript are counting blank lines). Perhaps the variable called 'color' does not contain a proper reference to a Color type object at the moment of evaluation.

                               

                              Insert this just before the 'switch' line:

                               

                              alert ("Variable 'color' is not a Color but actually of type "+color.constructor.name);

                               

                              and report what it says. You also may want to include the entire error message as Extendscript is a bit more verbose than what you quoted, and it usually helps determining the source of the error.

                              • 12. Re: [CS5 - JS] Convert InDesign colors to Hex
                                Paeon Level 1

                                Problem is I¹m not using extendscript. I am using Aptana Studio 3.

                                • 13. Re: [CS5 - JS] Convert InDesign colors to Hex
                                  [Jongware] Most Valuable Participant

                                  I meant the error that InDesign issues when you *run* the script. Surely you are still  using InDesign to run your scripts?

                                  • 14. Re: [CS5 - JS] Convert InDesign colors to Hex
                                    Paeon Level 1

                                    Oh, I think I found the issue. I was hoping to change all the swatches at

                                    once. Seems you can only select one object that has a color applied to make

                                    hex code.