Skip to main content
Nicky G.
Known Participant
August 2, 2023
Answered

Generate Adobe Color files from hexadecimal

  • August 2, 2023
  • 2 replies
  • 2255 views

There is a way to generate an *.ASE file for importing color samples into Adobe software (PS, AI, ID...) starting from a list I have in a TXT, CSV, EXCEL or other file.

example of the list:
#e0cffc
#c29ffa
#a370f7
#8540f5
#6610f2
#520dc2
#3d0a91
#290661
#140330

therefore I would like these color codes to be saved in an *.ASE file that can be imported into INDESIGN which generates the 9 colors (9 as 10,000 colors)

Step 2 (optional) - if these colors can somehow be "also" converted from HEX to CMYK

This topic has been closed for replies.
Correct answer rob day

Hi @Nicky G. This script will make a new document with RGB swatches from a hex list saved as a .txt file on your desktop—it expects just the hex value (no # prefix) in this format:

 

e0cffc

c29ffa

a370f7

8540f5

 

The swatches get saved in a color group named sRGB HEX, and from there you can save an .ASE file from the Swatches panel

 

 

 

 


var hc = readFile("~/Desktop/HexColors.txt")
//an array of hex values (no # prefix)
var hexList = hc.split("\n")

//a new document with sRGB as its assigned RGB profile
var d = app.documents.add();
app.activeDocument.rgbProfile="sRGB IEC61966-2.1"

//remove default swatches
var us = d.unusedSwatches
for (var i = 0; i < us.length; i++){
    us[i].remove()
};

//make a new color group named HEX
var cg = makeColorGroup(d, "sRGB HEX")

//create RGB swatches with HEX names from the  hexList array
var ns;
for (var i = 0; i < hexList.length; i++){
    ns = makeSwatch(d, hexList[i], cg)
    ns.properties = {model:ColorModel.PROCESS, space:ColorSpace.RGB, colorValue:hexToRgb(hexList[i])}
};   


/**
* Gets the RGB values as an array from the provided hex value 
* @ param the hex value 
* @ return array of RGB values 
*/
function hexToRgb(hx) {
    var r = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hx);
    var a = [];
    var i = 3;
    while (i--) a.unshift(parseInt(r[i+1], 16));
    return a
}
  

/**
* Makes a new named Swatch 
* @ param d document 
* @ param color name 
* @ param group name 
* @ return the new swatch 
*/

function makeSwatch(d, n, cg){
    if (d.colors.itemByName(n).isValid) {
        return d.colors.itemByName(n);
    } else {
        return d.colors.add({name:n, parentColorGroup:cg});
    }
}


/**
* Makes a new color group
* @ param d document 
* @ param n group name 
* @ return the new group 
* 
*/
function makeColorGroup(d, n){
    if (d.colorGroups.itemByName(n).isValid) {
        return d.colorGroups.itemByName(n);
    } else {
        return d.colorGroups.add({name:n});
    }
}


/**
* Read a text file 
* @ param p the path to the text file 
* @ return the file‘s text 
*/

function readFile(p) {
	var f = new File(p);
	f.open("r");  
	var x = f.read(); 
	f.close();
	return x; //returns the text in the file sampletext.txt
}

 

 

 

After running:

 

Also, HEX is an RGB notation, so there isn’t a CMYK equivalent. You can convert RGB Hex colors to CMYK, but the resulting values depend on the source RGB profile and the destination CMYK profile which could be anything.

 

You can convert HEX values to CMYK on an Export to PDF by setting a Output tab’s Destination to a CMYK profile, which will color manage the conversion into the chosen CMYK space during the export.

2 replies

Nicky G.
Nicky G.Author
Known Participant
August 3, 2023

@rob day 

sorry for the inconvenience...
Could this script be adapted for RGB and CMYK data sources?

colorname RGB 24 154 149
{and}
colorname CMYK 24 100 56 12


-----

Using as default color profiles in adobe (AdobeRGB 1998)
do you think it would be better to put in:
app.activeDocument.rgbProfile="sRGB IEC61966-2.1"
the profile
app.activeDocument.rgbProfile="Adobe RGB (1998)"

to make sure that if I detect a color change from HEX to CMYK in the PHOTOSHOP color panel it gives me the same conversion values that I get from the Indesign script?
Or is it tasteful to keep sRGB 1966-2.1?

rob day
Community Expert
Community Expert
August 3, 2023

If you change the for loop to this the colors will be named using the hex value’s RGB exivalent:

 

//create RGB swatches with RGB names from the  hexList array
var ns, ha, cn;
for (var i = 0; i < hexList.length; i++){
    ha = hexList[i].split("\t")
    cn = ha[0] + " #" + ha[1]
    ns = makeSwatch(d, cn, cg)
    ns.properties = {model:ColorModel.PROCESS, space:ColorSpace.RGB, colorValue:hexToRgb(ha[1])}
    ns.name = ha[0] + " " + ns.colorValue[0] + " " + ns.colorValue[1] + " " + ns.colorValue[2]
};   

 

 

 

 

 

Using as default color profiles in adobe (AdobeRGB 1998) do you think it would be better to put in:
app.activeDocument.rgbProfile="sRGB IEC61966-2.1"

 

I was assuming the usage for the hex values was for getting web HTML values. If you change the profile to something other than sRGB the appearance of the hex colors is going to change:

 

sRGB

 

Adobe RGB

 

ProPhoto RGB

 

All of these colors are significantly out-of-gamut so a conversion to any CMYK space will change the appearance:

 

Nicky G.
Nicky G.Author
Known Participant
August 3, 2023

OK for color profile.

Maybe I misunderstood point number 1.

The script you sent me to replace,
"//create RGB swatches with RGB names from the hexList array"

It does nothing but insert the RGB Nomenclature in the name of the color swatch. It always starts from a HEX data source and creates an RGB color sample with the nomenclature COLOR NAME 125 145 145 (RGB value).

I simply asked to be able to generate the list of color samples starting from a data source expressed with the RGB color triad instead of the hexadecimal
color_name RGB 24 154 149

ditto I was asking the same thing for a data source expressed in CMYK

color_name CMYK 24 15 80 5

 

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
August 2, 2023

Hi @Nicky G. This script will make a new document with RGB swatches from a hex list saved as a .txt file on your desktop—it expects just the hex value (no # prefix) in this format:

 

e0cffc

c29ffa

a370f7

8540f5

 

The swatches get saved in a color group named sRGB HEX, and from there you can save an .ASE file from the Swatches panel

 

 

 

 


var hc = readFile("~/Desktop/HexColors.txt")
//an array of hex values (no # prefix)
var hexList = hc.split("\n")

//a new document with sRGB as its assigned RGB profile
var d = app.documents.add();
app.activeDocument.rgbProfile="sRGB IEC61966-2.1"

//remove default swatches
var us = d.unusedSwatches
for (var i = 0; i < us.length; i++){
    us[i].remove()
};

//make a new color group named HEX
var cg = makeColorGroup(d, "sRGB HEX")

//create RGB swatches with HEX names from the  hexList array
var ns;
for (var i = 0; i < hexList.length; i++){
    ns = makeSwatch(d, hexList[i], cg)
    ns.properties = {model:ColorModel.PROCESS, space:ColorSpace.RGB, colorValue:hexToRgb(hexList[i])}
};   


/**
* Gets the RGB values as an array from the provided hex value 
* @ param the hex value 
* @ return array of RGB values 
*/
function hexToRgb(hx) {
    var r = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hx);
    var a = [];
    var i = 3;
    while (i--) a.unshift(parseInt(r[i+1], 16));
    return a
}
  

/**
* Makes a new named Swatch 
* @ param d document 
* @ param color name 
* @ param group name 
* @ return the new swatch 
*/

function makeSwatch(d, n, cg){
    if (d.colors.itemByName(n).isValid) {
        return d.colors.itemByName(n);
    } else {
        return d.colors.add({name:n, parentColorGroup:cg});
    }
}


/**
* Makes a new color group
* @ param d document 
* @ param n group name 
* @ return the new group 
* 
*/
function makeColorGroup(d, n){
    if (d.colorGroups.itemByName(n).isValid) {
        return d.colorGroups.itemByName(n);
    } else {
        return d.colorGroups.add({name:n});
    }
}


/**
* Read a text file 
* @ param p the path to the text file 
* @ return the file‘s text 
*/

function readFile(p) {
	var f = new File(p);
	f.open("r");  
	var x = f.read(); 
	f.close();
	return x; //returns the text in the file sampletext.txt
}

 

 

 

After running:

 

Also, HEX is an RGB notation, so there isn’t a CMYK equivalent. You can convert RGB Hex colors to CMYK, but the resulting values depend on the source RGB profile and the destination CMYK profile which could be anything.

 

You can convert HEX values to CMYK on an Export to PDF by setting a Output tab’s Destination to a CMYK profile, which will color manage the conversion into the chosen CMYK space during the export.

Nicky G.
Nicky G.Author
Known Participant
August 2, 2023

Great Script, does what I'm looking for Thanks!
Only if possible make improvements:

 

1. The script creates a New document to insert the RGB/HEX color swatches - I noticed that the script inserts the colors in RGB, but it creates a CMYK document - preset for Printing. (should be WEB preset RGB)

 

2. you can read the source file with colors in this format:
Color_Name: #Hex
Navy Blue: 171547

 

- For a visual logic too, writing the data source like this would be correct: Blue Navy: #171547
if it would be possible (eliminate the hash # when composing the color tables)

then split the name and the Hex into two lists, where the color code will generate the color and the Name will appear in the color list in the panel?

 

3. RGB to CMYK color conversion is done directly from the ID swatches panel. But for a correct conversion the working space of the source document must be WEB RGB

 

 

rob day
Community Expert
Community Expert
August 2, 2023

but it creates a CMYK document - preset for Printing. (should be WEB preset RGB)

 

InDesign doesn’t have a document color mode, a page can have a mix of RGB, CMYK, and Lab colors and objects. The Document Setup lets you choose an Intent but all that does is set the initial defaults—you can still create a mix of RGB, CMYK or Lab defined swatches in any document. The hex colors the script is generating are all RGB, so the Intent doesn’t matter. When you Save the sRGB HEX group as an .ASE file, the swatches will be saved as RGB. When you Load the .ase file, the appearance of the loaded RGB Swatches is going to depend on the loading document’s assigned RGB profile.

 

RGB to CMYK color conversion is done directly from the ID swatches panel. But for a correct conversion the working space of the source document must be WEB RGB

 

If you convert the sRGB values to CMYK from the Swatches panel, the Color Settings’ Working Spaces are not used—it is the document’s assigned profiles that make the conversion from RGB to CMYK. Check Edit>Assign Profiles to set the source RGB profile and the destination CMYK profile (not Color Settings). Web browsers use sRGB as the RGB space for HTML hex coded values.

 

In this example the conversion of the selected swatches would be from sRGB to Coated FOGRA39 (not from the current Color Settings AdobeRGB and Coated GRACoL 2006 Working Spaces). If you choose a different CMYK assignment you will get different CMYK values. Also the Solid Color Intent you choose can have some affect the converted CMYK values.