Skip to main content
Known Participant
January 9, 2020
Answered

how to grab swatches hex colors

  • January 9, 2020
  • 4 replies
  • 2160 views

I am trying to grab hex colors of the documents swatches. here is what i have 

function hexColor() {
    var myDoc = app.activeDocument;
    var myColors = myDoc.colors;
    var swatches = myDoc.swatches;
    
    var array = [];
    
    for (var h = 0; h < swatches.length; h++) {
        var mySwatches = swatches[h].name;
        array.push(mySwatches);
    }

    alert(array);
}

 

I tried to do swatches[h].ColorSpace.hexValue; 

that is not working. any ideas out there?

Correct answer rob day

Here’s a combination of Brian‘s and Sunil’s code:

 

 

app.activeDocument.rgbProfile="sRGB IEC61966-2.1"
var swatches = app.activeDocument.swatches;
var array = [];
    
for (var i = 0; i < swatches.length; i++) {
    try {
        array.push(rgbToHex(swatches[i]));
    }
    catch(e){}
}

alert(array);



function rgbToHex(s){
    var hexStr = "";
    var dup = s.duplicate();
    dup.space = ColorSpace.RGB
    var c = dup.colorValue;
    
    for (var i = 0; i < c.length; i++) {
        var hex = Number(c[i]).toString(16);
        if (hex.length < 2) {
            hex = "0" + hex;
        } 
        hexStr += hex;
    }
    dup.remove();
    return hexStr;
}

 

4 replies

Known Participant
January 10, 2020

all three of these are perfect... trying to mark all correct! thanks for all your help everyone!

rob day
Community Expert
Community Expert
January 10, 2020

You might be interested in this post for manipulating swatches and color conversions—it’s Applescript, but deals with some tricky color conversion problems. The linked AppleScript is annotated

 

https://community.adobe.com/t5/indesign/branding-color-guide/td-p/10818696

Known Participant
January 10, 2020

much appreciated!

Do you know how I can use this same code, but not duplicate or remove swatches? I just want this array so in my CSS file I can line up a color box next to the drop-down swatch I have pulled in.

 

thanks,

kyle

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
January 10, 2020

Here’s a combination of Brian‘s and Sunil’s code:

 

 

app.activeDocument.rgbProfile="sRGB IEC61966-2.1"
var swatches = app.activeDocument.swatches;
var array = [];
    
for (var i = 0; i < swatches.length; i++) {
    try {
        array.push(rgbToHex(swatches[i]));
    }
    catch(e){}
}

alert(array);



function rgbToHex(s){
    var hexStr = "";
    var dup = s.duplicate();
    dup.space = ColorSpace.RGB
    var c = dup.colorValue;
    
    for (var i = 0; i < c.length; i++) {
        var hex = Number(c[i]).toString(16);
        if (hex.length < 2) {
            hex = "0" + hex;
        } 
        hexStr += hex;
    }
    dup.remove();
    return hexStr;
}

 

Sunil Yadav
Legend
January 10, 2020

Try this code:

/////////////////////////////////////////////////////////////////////

var swatches = app.documents[0].swatches;

for(var i = 0; i < swatches.length; i++){
    try{
        var oldSpace = swatches[i].space;
        swatches[i].space = ColorSpace.RGB;
        $.writeln(swatches[i].colorValue);
        swatches[i].space = oldSpace;
        }

    catch(e){}
    }

/////////////////////////////////////////////////////////////////////

 

Best

Sunil

rob day
Community Expert
Community Expert
January 10, 2020

Hi Sunil, I think you would have to be careful about converting to RGB and then back to the the original color mode. For example the conversion of  0|0|0|50 black CMYK swatch to RGB and back would produce new CMYK values—it would be a 4-color mix something like 45|35|35|0 depending on the document profiles, and not the original 0|0|0|50 value.

 

So, assuming the point of getting HEX values is for web coding, the script would have to set the document’s RGB color profile to sRGB (a different profile like AdobeRGB would produce the wrong RGB HEX conversion), duplicate the swatch, run Brian’s function to get the duplicate’s RGB HEX value, and then delete the duplicate.

brian_p_dts
Community Expert
Community Expert
January 10, 2020

Good question. Doesn't look to me like hex is available through the DOM. Luckily, we can roll our own. This assumes all your colors are RGB.

 

Edit: Edited the code a bit to contain more in the function. You'd pass it an RGB colorValue array, and it will spit out a hex string. You could also use a variation on Sunil's contribution to convert any non-RGB colors to RGB to process those swatches. 

 

 

 

var rgbToHex = function (rgb) { 
    var hexStr = "";
    for (var i = 0; i < rgb.length; i++) {
        var hex = Number(rgb[i]).toString(16);
        if (hex.length < 2) {
            hex = "0" + hex;
        } 
        hexStr += hex;
    }
    return hexStr;
};

var activeDoc = app.activeDocument;
var color = activeDoc.swatches.itemByName("RGColorSwatch").colorValue;
alert(rgbToHex(color));