• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

how to change Name of RGB swatches to hex Value via script

Community Beginner ,
Nov 04, 2020 Nov 04, 2020

Copy link to clipboard

Copied

Hello out there!!!

I tried changing a script to change all of the RGB swatch names to the hex value. But I failed !!!
here my first attempt:

(function(){  
  var doc=app.activeDocument,  
  sw=doc.swatches.everyItem().getElements(),  
  c;  
  for(var i=3; i<sw.length; i++){  
  try{  
  c=doc.colors.itemByID(sw[i].id);  
  switch (c.space){  
  case ColorSpace.RGB:  
  c.name='hex='+Math.round(c.colorValue[3]);  
  }  
  }catch(e){};  
  }  
}()) 

now i know i have to implement the funktion "hex to rgb" but i dont know how!!!
here is a script that shows all the Hex Values... 

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;
}

How could i merge the two scripts???? 
An Idea would be awesome!!!! 

TOPICS
Scripting

Views

524

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 04, 2020 Nov 04, 2020

Hi Malcolm, the returned values in your script don’t always match InDesign’s HEX code for an RGB color.

 

I don’t have a Javascript translation, but this AppleScript works where I’m getting the hex pairs for each channel:

 

 

--https://en.wikipedia.org/wiki/Hexadecimal#Other_simple_conversions

tell application id "com.adobe.indesign"
	set sw to every swatch of active document whose space is RGB
	--name the rgb swatches with hex value
	repeat with x in sw
		try
			set cv to color value of x
			s
...

Votes

Translate

Translate
Community Expert ,
Nov 04, 2020 Nov 04, 2020

Copy link to clipboard

Copied

Hi,

 

I think something like this should work - (Not tried it out, just merged the above code:

 

var doc=app.activeDocument, sw=doc.swatches.everyItem().getElements(),  c;  
  for(var i=3; i<sw.length; i++){  
  try{  
  c=doc.colors.itemByID(sw[i].id);  
  switch (c.space){  
   c ase ColorSpace.RGB:  
    c.name='hex='+rgbToHex ( c);  
  }  
  }catch(e){};  
}  

  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;
}

 

Regards

 

Malcolm

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 04, 2020 Nov 04, 2020

Copy link to clipboard

Copied

Hi Malcolm, the returned values in your script don’t always match InDesign’s HEX code for an RGB color.

 

I don’t have a Javascript translation, but this AppleScript works where I’m getting the hex pairs for each channel:

 

 

--https://en.wikipedia.org/wiki/Hexadecimal#Other_simple_conversions

tell application id "com.adobe.indesign"
	set sw to every swatch of active document whose space is RGB
	--name the rgb swatches with hex value
	repeat with x in sw
		try
			set cv to color value of x
			set hv to my convertRGB(item 1 of cv, item 2 of cv, item 3 of cv)
			set name of x to hv
		end try
	end repeat
end tell

--get an individual channel’s hex.
--d is the r,g, or b value 0-255
on toHex(d)
	set r to d mod 16
	if (d - r) is 0 then
		return my toChar(r)
	end if
	set hn to (my toHex((d - r) / 16)) & (my toChar(r))
	return hn
end toHex

--get the hex string
on toChar(n)
	set alphaList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
	set pos to (n as number) + 1
	return item pos of alphaList
end toChar

--convert 
on convertRGB(r, g, b)
	set h1 to my toHex(r)
	if (count of characters in h1) is 1 then
		set h1 to "0" & h1
	end if
	set h2 to my toHex(g)
	if (count of characters in h2) is 1 then
		set h2 to "0" & h2
	end if
	set h3 to my toHex(b)
	if (count of characters in h3) is 1 then
		set h3 to "0" & h3
	end if
	return "#" & h1 & h2 & h3
end convertRGB

 

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 04, 2020 Nov 04, 2020

Copy link to clipboard

Copied

Hi @rob day 

 

You are probably correct, the question was "How could i merge the two scripts???? " So I merged the two scripts exactly as they where in the question, that is all,  I didn't check the the code any further than making sure it compiled and ran.

 

Regards

 

Malcolm

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 04, 2020 Nov 04, 2020

Copy link to clipboard

Copied

THANK YOU SO MUCH!!!

Your so great!!! 

At first it didn't work, but then I noticed that I still use file extension .jsx instead of .applescript

love your work !!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 04, 2020 Nov 04, 2020

Copy link to clipboard

Copied

LATEST

I think this JS translation works:

 

var sw = getSwatches();


for (var i = 0; i < sw.length; i++){
    
    if (sw[i].space == ColorSpace.RGB) {
        var cv = sw[i].colorValue
        var hv = getHex(cv[0],cv[1],cv[2])
        sw[i].name = "hex=" + hv;
    } 
};   


/**
* Get hex number or letter 
* @Param a 
* @Return hex character 
* 
*/
function toChar(n){
    var alphaList = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]
    return alphaList[Math.round(n)]
}


/**
* Get a hex pair for a number 0-255
* @Param the number to convert 
* @Return hex pair string
* 
*/
function toHex(d){
    var r = d % 16
    if ((d-r) == 0) {
        return toChar(r)
    } 
    var hn = toHex((d - r) / 16) + "" +  toChar(r)
    return hn
}


/**
* Get RGB hex value
* @Param red channel number 
* @Param green channel number 
* @Param blue channel number 
* @Return full hex string 
* 
*/
function getHex(r,g,b){
    var h1 = toHex(r)
    if (h1.length == 1) {
        h1 = "0" + h1;
    } 
    var h2 = toHex(g)
    if (h2.length == 1) {
        h2 = "0" + h2;
    }
    var h3 = toHex(b)
    if (h3.length == 1) {
        h3 = "0" + h3;
    }
    return h1 + h2 + h3
}


/**
* Gets a collection of the active document’s read/write swatches 
* @Return an array of swatches 
* 
*/
function getSwatches(){
    var p=app.activeDocument.swatches; 
    var def = ["Black", "Cyan", "Magenta", "Yellow", "Paper", "Registration", "None"]; 
    var s = [];
    var d = false;
    
    for (var i = 0; i < p.length; i++){
        for (var j = 0; j < def.length; j++) {
            if (def[j] === p[i].name) {
                d = true;
            }
        }
        if (!d) {s.push(p[i])} 
        d = false;
    }
    return s
}







Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines