5 Replies Latest reply on Oct 4, 2017 6:28 AM by beto correa

    Color conversion script?

    Cai Morris

      I'm looking for a way to easily print out, in text, the CMYK, RGB, HEX & possibly some other representation of certain colors, I thought I would write a quick script... easy, right? Apparently not.

       

      Although I've never used it, it looks like Photoshop has the 'SolidColor' Object, which has properties for 'cmyk', 'rgb', 'gray' etc.. exactly what I need, except I want to do this in Illustrator. Illustrator only has the 'CMYKColor', 'RGBColor' etc objects with, from what I can tell, no way of converting to any other color mode.

       

      So.. am I missing something obvious? Or is there no way to easily get different color mode representations of a color object in AI? I know the conversion will be different depending on profiles etc, but the UI color picker has the values right there already and apparently Photoshop can do this with no problem so I'm hoping I'm just missing something obvious!

       

      I tried changing the 'typename' of the Color object, which I've seen suggested, but it made no difference (the docs do say it is read-only).

       

      The only other options I can think of are to create a new document in the color mode I want to convert to, create a new color from the existing one (which from reading the reference sounds like it will auto-convert it to that color mode) read the values from the new color and close the newly created document. But that seems like a horrible workaround. Or, implement my own conversion function, which also sounds horrible (considering I thought this was going to be an easy quick script to write!)

       

      Any input would be appreciated!

        • 1. Re: Color conversion script?
          Silly-V Adobe Community Professional

          You could try to paste my Hobo Swatch code for some use such as examining the code- but honestly my own code from years ago makes my eyes burn. What's worse is that it hasn't improved a whole lot! hahaha!

          Custom Swatch Palette (beta)

           

          The most important thing there is probably the use of app.convertSampleColor() function which takes and gives back an array of numbers, based on the rest of the arguments (color space stuff).

          1 person found this helpful
          • 2. Re: Color conversion script?
            Cai Morris Level 1

            Thanks for the reply... There's quite a lot to look through there haha! I'll take a look at that though, thanks.

            • 3. Re: Color conversion script?
              Cai Morris Level 1

              Your code was very helpful Silly-V, convertSampleColor() was exactly what I was looking for! If you or anyone is interested, this is my script now. I havn't tested it much and it could probably be cleaned up a bit but it should work (it does for me with CS6 on OS X).

               

              It takes all your selected swatches and prints the CMYK, RGB and HEX (and name if it's a Spot color) of all those selected swatches to a txt file. The txt file gets saved with the same name and in the same folder as your Illustrator file. If your Illustrator file is unsaved, it saves the txt file in your home directory.

              // Color Modes To Text
              // ===================
              
              
              // Prints CMYK values, RGB values and HEX value of all selected swatches to a .txt file
              // The .txt file is saved with the same file name and inc the same folder as the .ai file
              // If the .ai file hasn't been saved, the .txt file is saved in the users home directory
              
              
              main();
              function main()
              {
                  var doc = app.activeDocument;
                  var selectedSwatches = doc.swatches.getSelected();
              
              
                  if (selectedSwatches.length > 0)
                  {
                      var text = "";
              
              
                      for (var i = 0; i < selectedSwatches.length; i++)
                      {
                          var swatch = selectedSwatches[i]
                          var color = swatch.color;
              
              
                          // Spot
                          if (color.typename == "SpotColor") {
                              text += color.spot.name + "\n";
                              color = color.spot.color;
                          }
              
              
                          // CMYK Source
                          if (color.typename == "CMYKColor")
                          {
                              // CMYK Values
                              text += "C=" + Math.round(color.cyan) + " M=" + Math.round(color.magenta) + " Y=" + Math.round(color.yellow) + " K=" + Math.round(color.black) + "\n";
              
              
                              // RGB Values
                              var rgb = convertColor("CMYK", "RGB", [Math.round(color.cyan), Math.round(color.magenta), Math.round(color.yellow), Math.round(color.black)]);
                              text += "R=" + Math.floor(rgb[0]) + " G=" + Math.floor(rgb[1]) + " B=" + Math.floor(rgb[2]) + "\n";
              
              
                              // HEX Values
                              text += rgbToHex(Math.floor(rgb[0]), Math.floor(rgb[1]), Math.floor(rgb[2])) + "\n";
                              text += "\n";
                          }
                          // RGB Source
                          else if (color.typename == "RGBColor")
                          {
                              // CMYK Values
                              var cmyk = convertColor("RGB", "CMYK", [Math.round(color.red), Math.round(color.green), Math.round(color.blue)]);
                              text += "C=" + Math.round(cmyk[0]) + " M=" + Math.round(cmyk[1]) + " Y=" + Math.round(cmyk[2]) + " K=" + Math.round(cmyk[3]) + "\n";
              
              
                              // RGB Values
                              text += "R=" + Math.floor(color.red) + " G=" + Math.floor(color.green) + " B=" + Math.floor(color.blue) + "\n";
              
              
                              // HEX Values
                              text += rgbToHex(Math.floor(color.red), Math.floor(color.green), Math.floor(color.blue)) + "\n";
                              text += "\n";
                          }
                      }
                      saveTxt(text);
                  }
                  else {
                      alert("No Swatches Selected.");
                  }
              }
              
              
              function convertColor(src, dest, clrArr)
              {
                  return app.convertSampleColor(ImageColorSpace[src], clrArr, ImageColorSpace[dest], ColorConvertPurpose.defaultpurpose);
              }
              
              
              function componentToHex(c)
              {
                  var hex = c.toString(16);
                  return hex.length == 1 ? "0" + hex : hex;
              }
              
              
              function rgbToHex(r, g, b)
              {
                  return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
              }
              
              
              function saveTxt(txt)
              {
                  var name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
                  var path = (app.activeDocument.path != "") ? app.activeDocument.path : "~";
              
              
                  var saveFile = new File(path + "/" + name + ".txt");
              
              
                  if(saveFile.exists)
                      saveFile.remove();
              
              
                  saveFile.encoding = "UTF8";
                  saveFile.open("e", "TEXT");
                  saveFile.writeln(txt);
                  saveFile.close();
              
              
                  alert("Saved to File:\n" + saveFile.fullName)
              }
              
              • 4. Re: Color conversion script?
                rachelb34367531 Level 1

                Hi there! This is fantastic! Is there any way to generate the name of the color as well?

                 

                For example, could it read something like:

                 

                Bright Red

                C=0 M=100 Y=100 K=0

                R=237 G=28 B=36

                #ed1c24

                 

                Thanks so much!

                 

                Rachel

                • 5. Re: Color conversion script?
                  beto correa Level 1

                  Hi.

                  I need a script that convert all cmyk document swatches colors to gray scale 50%.

                  Is ti possible?

                   

                  Thanks.