Copy link to clipboard
Copied
Is there a Javascript command to change all swatches in a document to the "Name With Color Value" checkbox in Swatch Options? I know there are scripts to change swatch names based on the color value, but I'm looking for a script that applies this specific checkbox to everything.
Thanks in advance!
This should do it:
...(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.id);
switch (c.space){
case ColorSpace.RGB:
c.name='R='+c.colorValue[0]+
' G='+c.colorValue[1]+
' B='+c.colorValue[2];
break;
case ColorSpace.LAB:
c.name='L='+c.colorValue[0]+
' A='+c.colorValue[1]+
' B='+c.colorValue[2];
break;
case ColorSpace.CMYK:
c.name='C='+c.colorValue[0]+
'
Copy link to clipboard
Copied
Neither app.document.color nor app.document.swatch seems to have a setting for that checkbox.
I don’t think this is possible to do by script.
Copy link to clipboard
Copied
The one thing I can see is that if you change the name of a swatch (in the InDesign UI) to the accurate name of the colors, this causes the "Name with Color Value" checkbox to become checked.
So if you have a swatch that's called "MyColor" with CMYK values 10,20,30,40, and in the UI you change the name to "C=10 M=20 Y=30 K=40", that checkbox comes on!
Ariel
Copy link to clipboard
Copied
This should do it:
(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.id);
switch (c.space){
case ColorSpace.RGB:
c.name='R='+c.colorValue[0]+
' G='+c.colorValue[1]+
' B='+c.colorValue[2];
break;
case ColorSpace.LAB:
c.name='L='+c.colorValue[0]+
' A='+c.colorValue[1]+
' B='+c.colorValue[2];
break;
case ColorSpace.CMYK:
c.name='C='+c.colorValue[0]+
' M='+c.colorValue[1]+
' Y='+c.colorValue[2]+
' K='+c.colorValue[3];
break;
}
}catch(e){};
}
}())
Copy link to clipboard
Copied
Win7, CS6.
After running Vamitul's script:
1. Some of the names turns to be "user unfriendly" .
Could be the values rounded to the nearest whole number?
Like in this Dave Saunders script for converting RGB to nicely rounded CMYK...
2. The "Name with Color Value" checkbox remains still not checked. Checking it manually fixes (rounds) the swatch name. Actual values remain untouched, though.
Copy link to clipboard
Copied
Looks like we asked the same question seconds apart
Copy link to clipboard
Copied
That only points the problem is actual
Edit:
and it's not a cmyk/rgb conversion case...
all my swatches in a test doc we originally cmyk...
Copy link to clipboard
Copied
That worked perfectly, Vamitul, thank you!
The issue I'm running into now is that before this script is run, I have it set to convert all swatches to CMYK process, so anything that was previously an RGB swatch gets renamed to the exact CMYK value:
Is there a way to round the CMYK values?
Copy link to clipboard
Copied
How would you like to convert RGB to CMYK?
Honoring all the settings in color management?
Using Rendering Intents?
Using black point compensation when doing it with Releative Colorimetric?
Usage of a distinct CMM (Color Management Module)?
How would you go about tints from RGB colors?
This opens a wide field…
Uwe
Copy link to clipboard
Copied
HI.
I need to convert my process swatch to a specific color. E.g. black 50%.
Is it possible?
Copy link to clipboard
Copied
Hi beto,
you simply create a new color and remove the old one with the new one.
Here an example:
var doc = app.documents[0];
var colorToSubstitute = doc.colors.itemByName("ColorToSubstitute");
var newColorName = "50 Black";
var newColor = doc.colors.itemByName( newColorName );
if(!newColor.isValid)
{
var newColor = doc.colors.add
(
{
name : newColorName ,
colorValue : [0,0,0,50] ,
model : ColorModel.PROCESS ,
space : ColorSpace.CMYK
}
);
};
colorToSubstitute.remove(newColor);
Regards,
Uwe
Copy link to clipboard
Copied
Great!
This script create a new color.
Is posssible remove all process swatches replacing by 50 Black?
Regards
Copy link to clipboard
Copied
Maybe David Blatner will answer you! …
https://indesignsecrets.com/topic/script-to-convert-all-colors-to-cmyk-process
(^/)
Copy link to clipboard
Copied
Hi Ariel,
just a note on naming colour schemes.
Depending on the locale version of InDesign, the naming would be a bit different.
In a French locale version, I think "K" will not be associated with the Black channel. And the "Y" not with Yellow.
Cyan, Magenta, Yellow, Black will become Cyan, Magenta, Jaune an Noir.
Leading perhaps to:
"C=10 M=20 J=30 N=40"
Note:
I have no French version installed, so I could not verify this.
Uwe
Copy link to clipboard
Copied
To round up the names:
in the script, everywhere you see c.colorValue[nr]
replace with Math.round(c.colorValue[nr])
I don't know if that will also fix the checkbox. if not, the actual color values of the swatch need to be adjusted also.
Copy link to clipboard
Copied
Perfect! The swatches still retain the decimal values, but the names are rounded and it does apply the Name With Color Value checkbox as well. Thank you!
Copy link to clipboard
Copied
... and it doesn't touch named swatches which values are the same as already existing swatch, named with actual values. It's ok, I think.
@Uwe:
You could be an invaluable employee for some insurance company...
Copy link to clipboard
Copied
Here is code for rounding decimal point nearest number. İts not my code just found it on this forum ı guess but ı dont remember which post it was.
(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.id);
switch (c.space){
case ColorSpace.RGB:
c.name='R='+Math.round(c.colorValue[0])+
' G='+Math.round(c.colorValue[1])+
' B='+Math.round(c.colorValue[2]);
break;
case ColorSpace.LAB:
c.name='L='+Math.round(c.colorValue[0])+
' A='+Math.round(c.colorValue[1])+
' B='+Math.round(c.colorValue[2]);
break;
case ColorSpace.CMYK:
c.name='C='+Math.round(c.colorValue[0])+
' M='+Math.round(c.colorValue[1])+
' Y='+Math.round(c.colorValue[2])+
' K='+Math.round(c.colorValue[3]);
break;
}
}catch(e){};
}
}())
Copy link to clipboard
Copied
Hi!
I need to convert all swatch colors (remove and replace with) to only one at once.
E. g.
Is it possible?
Can you help me?
Regards.
Copy link to clipboard
Copied
Hello you gods of code!!!
i tried to change your script to change all RGB Swatch Name 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;
}
any idea or interest to help me???? THIS WOULD BE GREAT!!!!