adobe-illustratorextendscript

How can I use Hex codes when specifiying a color for defaultFillColor?


I am just getting started with learning scripting for CC software. I have been reading the documentation and have managed to change the fill colour with:

var colfil      = new RGBColor();
colfil.red      = 100;
colfil.green    = 255;
colfil.blue     = 100;
app.activeDocument.defaultFillColor = colfil

While this is really cool and all. How can I just pass the hex code equivalent of the above:

app.activeDocument.defaultFillColor = "64FF64"

I am relying on another tool to provide me with a colour and it only outputs a hex code, I simply want to pass this Hex colour to illustrator.

I am really not advanced at all, so please pardon me if the obvious eludes me. Is there a way to use Hex codes here?


Solution

  • Something like this?

    function get_rgb_color(hex) {
      var color   = new RGBColor();
      color.red   = parseInt(hex.slice(0,2),16);
      color.green = parseInt(hex.slice(2,4),16);
      color.blue  = parseInt(hex.slice(4,6),16);
      return color;
    }
    
    app.activeDocument.defaultFillColor = get_rgb_color("64FF64");