19 Replies Latest reply on Jan 17, 2016 5:39 PM by MPrewitt (IIW)

# Sort color swatches in InDesign

Some of the larger publications that I work on accumulate a gazillion color swatches, and I would like to be able to sort them. In the past I've done this manually, by CMYK value. But this is time-consuming and often results in visually convoluted arrangements. The greens are not all together, there are pastels up in the reds and yellows, etc. I would like to do this with a script, to save time and make it more perceptually pleasing and useful.

Any help or advice would be much appreciated. Or if you're a scripting angel, maybe you'd like to write it for me. I've written scripts before, but I'm not sure the best way to iterate through the swatches, or how to sort them into folders or group them.

The basic sorting method I want is to group the swatches in folders like so (each bullet point would be a separate folder in the Swatches palette — or maybe just grouped together in a flat list — could be an option in the script ... and the formula that follows each bullet points describes the criteria for the colors in that group and how they would be sub-sorted):

• Magentas: If M >127 and CYK <64, sort by density
• Reds: If MY >127 and CK <64, and M>=Y, sort by density
• Oranges: If MY >127 and CK <64, and Y>M, sort by density
• Yellows: If Y >127 and CMK <64, sort by density
• Warm Greens: If CY >127 and MK <64, and Y>=C, sort by density
• Cool Greens: If CY >127 and MK <64, and C>Y, sort by density
• Cyans: If C >127 and MYK <64, sort by density
• Blues: If CM >127 and YK <64, and C>=M, sort by density
• Purples: If CM >127 and YK <64, and M>C, sort by density
• Darks: All values are >85, sort by dominant CMY, then by density
• Pastels: All values are <15, sort by dominant CMY, then by density
• Grays: Any where the CMY values are +/- 5 from their average (which also includes CMY=0), sort by dominant CMY value then by density.
• Everything else: Sort by dominant CMY then by density

(In the above section, "If MY >127" means, "If M and Y are both individually less than 127", etc.)

For gray calculation: Average = (C+M+Y)/3

For density sorting: Density = L from (L * a * b), or other formula to calculate lightness/darkness value such as: Density = (C * 0.49) + (M * 0.6) + (Y * 0.07) + (K * 0.91)

For RGB swatches, convert RGB colors and process as CMYK?

Drop all spot colors in their own folder (or at top of list).

Drop all gradients in their own folder (or at end of list).

• ###### 2. Re: Sort color swatches in InDesign

The Ajar script doesn't do exactly what you want. Further Google suggestions to "InDesign sort swatches javascript" gives many answers. And do check Kasyan Servetsky's site (Scripts sorted by categories for InDesign), it has many excellent scripts, including all kinds of things related to swatches and colour. Lots of ideas there.

P.

• ###### 3. Re: Sort color swatches in InDesign

Thanks, but that script merely alphabetizes the list. I wanted them

sorted by color.

• ###### 4. Re: Sort color swatches in InDesign

In the Kasyan site, there was one script to sort swatches, but it too was only an alphabetical sort.

• ###### 5. Re: Sort color swatches in InDesign

Hi

You are very much asking for a custom script. There are several freelances who frequent the forum (including myself) who are available for custom scripts. Contrary to the name "freelances" the services are not free or even partially cheep.

What you are looking for is not that simple, also there is a difference on what the definition of a "gazillion".  I have quite a bit of experience of working  with various color conversion and swatch functions processing  documents with thousands of swatches.

There is a very big difference between working with documents with 1000 - 5000 swatches to documents with 5000 - 11000 and 11,000+ swatches at 14,000+ it gets really tough but either way one has to know what one is dealing with.

There are at least 4 basic approaches to what you are after.

1) The standard mess around with the swatches - create, delete, apply new swatch which will reorder the swatches in a destructive manner.

2) SUI based palette which will leave the swatches alone but display the swatches in any order you like and allow the swatches to be selected from that panel, (The gradient swatches would be a challenge for that).

3) A html (extension based) palette that would replace the built in swatch palette, it would look the same just with the added functionality.

4) A SDK c++ solution that would add the sort function to the built in swatch palette.

Solution 1 would change the index of the current swatches, 2 and 3 wouldn't, 4 could go either way but more likely would.

None of the solutions would be particularly cheep with the higher numbered solutions being more not cheep than the lower ones

I personally would recommend solution 3 as the most practical.

In short quality costs.

HTH

Trevor

Custom Scripts & Services | Creative-Scripts.com

Contact | Creative-Scripts.com

• ###### 6. Re: Sort color swatches in InDesign

Hi MPrewitt,

Provided that CMYK values belong to 0..100, what is the meaning of a criteria such as M > 127?

Is the script supposed to rescale 0..100 into 0..255?

@+

Marc

• ###### 7. Re: Sort color swatches in InDesign

Oops, you are right. I was using 0-255 ranges, except when I got to the rules for darks and pastels, which are based on 0-100. So for the first nine rules, all the "127"s should be 50, and all the "64"s should be 25.

• ###### 8. Re: Sort color swatches in InDesign

Here is the (non-functional, probably buggy) script so far:

```// Description: SortSwatches v. 0.1. Script to sort InDesign swatches by color value
// This script is provided "as-is" and the author takes no responsibility whatsoever for results of its use.

/*
- Magentas: If M >50 and CYK <25, sort by density
- Reds: If MY >50 and CK <25, and M>=Y, sort by density
- Oranges: If MY >50 and CK <25, and Y>M, sort by density
- Yellows: If Y >50 and CMK <25, sort by density
- Warm Greens: If CY >50 and MK <25, and Y>=C, sort by density
- Cool Greens: If CY >50 and MK <25, and C>Y, sort by density
- Cyans: If C >50 and MYK <25, sort by density
- Blues: If CM >50 and YK <25, and C>=M, sort by density
- Purples: If CM >50 and YK <25, and M>C, sort by density
- Darks: All values are >85, sort by dominant CMY, then by density
- Pastels: All values are <15, sort by dominant CMY, then by density
- Grays: Any where the CMY values are +/- 10 from their average (which also includes CMY=0), sort by dominant CMY then by density.
- Everything else: Sort by dominant CMY then by density

Drop all spot colors in their own folder?

Drop all gradients in their own folder?
*/

// For reference, see http://jongware.mit.edu/idcs5js/index_Color%20Suite.html

(function(){
sortSwatches();
});

function sortSwatches(){
// Alert: Script will convert RGB to CMYK ... OK or cancel
// Alert: Does not currently support custom mixed ink swatches or custom registration swatches

// Load current swatches into arrays (RGB/CMYK, spot, gradient) ... convert RGB to CMYK in the process ??? may not be necessary
allSwatches = app.activeDocument.swatches.everyItem();
allSwatches.splice(0,4); // Don't sort None, Registration, Paper, Black

for (i=0; i<allSwatches.length; i++) {
if ( allSwatches[i] instanceof Swatch.Color ) {
swatchType = allSwatches[i].model;
switch(swatchType) {
case model.SPOT:
arraySpot[arraySpot.length] = allSwatches[i];
break;
case model.PROCESS:
arrayCMYK[arrayCMYK.length] = allSwatches[i];
break;
}
} else if ( allSwatches[i] instanceof Swatch.Gradient ) {
}

/*
else if ( allSwatches[i] instanceof Swatch.MixedInk ) { }
else if ( allSwatches[i] instanceof Swatch.MixedInkGroup ) { }
*/
}

// Sort spot array by name
arraySpot.sort(); // This likely needs to be more complex; each item is an object, and 'name' might not be the primary index

// Sort CMYK array by color
for (i=0; i<arrayCMYK.length; i++) {
color = arrayCMYK[i];
color.space = ColorSpace.CMYK;
var c = color.colorValue[0];
var m = color.colorValue[1];
var y = color.colorValue[2];
var k = color.colorValue[3];

// Determine the density value
var density = c*0.49 + m*0.6 + y*0.07 + k*0.91;

// Determine the average value of CMY
var avgValue = (c+m+y)/3;

// Sort all colors into arrays such as magentas, blues, grays
// Then sort those arrays by density, etc., as explained in rules above
}

// Sort gradient array by name
arrayGradient.sort(); // This likely needs to be more complex; each item is an object, and 'name' might not be the primary index

// Dump spot, CMYK, gradient values back into the palette
// Remember to skip None, Registration, Paper, Black

}

```
• ###### 9. Re: Sort color swatches in InDesign

What does the expression "sort by dominant CMY" mean?

(i.e., what is the implied order there?)

@+

Marc

• ###### 10. Re: Sort color swatches in InDesign

Here is an updated, but still not functional script:

```// Description: SortSwatches v. 0.6. Script to sort InDesign swatches by color value
// Written by Michael Prewitt.
// This script is provided "as-is" and the author takes no responsibility whatsoever for results of its use.

// For reference, see http://jongware.mit.edu/idcs5js/index_Color%20Suite.html

(function(){
sortSwatches();
});

function sortSwatches(){
// Alert: Script will convert RGB to CMYK ... OK or cancel
// Alert: Does not currently support custom mixed ink swatches or custom registration swatches

var oldSwatches = app.activeDocument.swatches.everyItem();
// Don't sort None, Registration, Paper, Black
// var defaultSwatches = oldSwatches.slice(0,4);
oldSwatches.splice(0,4);

var newSwatches = new Array();

var arraySpot = new Array();
var arrayCMYK = new Array();

var arrayMagentas = new Array();
var arrayReds = new Array();
var arrayOranges = new Array();
var arrayYellows = new Array();
var arrayWarmGreens = new Array();
var arrayCoolGreens = new Array();
var arrayCyans = new Array();
var arrayBlues = new Array();
var arrayPurples = new Array();
var arrayGraysC = new Array();
var arrayGraysM = new Array();
var arrayGraysY = new Array();
var arrayGraysK = new Array();
var arrayDarksC = new Array();
var arrayDarksM = new Array();
var arrayDarksY = new Array();
var arrayPastelsC = new Array();
var arrayPastelsM = new Array();
var arrayPastelsY = new Array();
var arrayAssortedC = new Array();
var arrayAssortedM = new Array();
var arrayAssortedY = new Array();

var color, c, m, y, k, avgValue;

// Main

for (i=0; i<oldSwatches.length; i++) {
if ( oldSwatches[i] instanceof Swatch.Color ) {
switch ( oldSwatches[i].model ) {
case model.SPOT:
arraySpot[arraySpot.length] = oldSwatches[i];
break;
case model.PROCESS:
arrayCMYK[arrayCMYK.length] = oldSwatches[i];
break;
}
} else if ( oldSwatches[i] instanceof Swatch.Gradient ) {
}

/*
else if ( oldSwatches[i] instanceof Swatch.MixedInk ) { }
else if ( oldSwatches[i] instanceof Swatch.MixedInkGroup ) { }
*/
}

// Sort spot array by name
arraySpot.sort(byProperty("name")); // Each item is an object, sort by 'name'

// Sort CMYK array by color
for (i=0; i<arrayCMYK.length; i++) {
color = arrayCMYK[i];
color.space = ColorSpace.CMYK; // Convert RGB to CMYK in the process ??? may not be necessary
c = color.colorValue[0];
m = color.colorValue[1];
y = color.colorValue[2];
k = color.colorValue[3];

// Determine the density value
color.density = c*0.49 + m*0.6 + y*0.07 + k*0.91;

// Determine the average value of CMY
avgValue = (c+m+y)/3;

// Sort all colors into arrays such as magentas, blues, grays
if ( m>50 && c<25 && y<25 && k<25 ) arrayMagentas[arrayMagentas.length] = color;
else if ( m>50 && y>50 && c<25 && k<25 && m>=y ) arrayReds[arrayReds.length] = color;
else if ( m>50 && y>50 && c<25 && k<25 && y>m ) arrayOranges[arrayOranges.length] = color;
else if ( y>50 && c<25 && m<25 && k<25 ) arrayYellows[arrayYellows.length] = color;
else if ( c>50 && y>50 && m<25 && k<25 && c>=y ) arrayWarmGreens[arrayWarmGreens.length] = color;
else if ( c>50 && y>50 && m<25 && k<25 && y>c ) arrayCoolGreens[arrayCoolGreens.length] = color;
else if ( c>50 && m<25 && y<25 && k<25 ) arrayCyans[arrayCyans.length] = color;
else if ( c>50 && m>50 && y<25 && k<25 && c>=m ) arrayBlues[arrayBlues.length] = color;
else if ( c>50 && m>50 && y<25 && k<25 && m>c ) arrayPurples[arrayPurples.length] = color;
else if ( c==0 && m==0 && y==0 ) arrayGraysK[arrayGraysK.length] = color;
else if ( Math.abs(c-avgValue)<=10 && Math.abs(m-avgValue)<=10 && Math.abs(y-avgValue)<=10 ) {
if ( c>m && c>y ) arrayGraysC[arrayGraysC.length] = color;
else if ( m>c && m>y ) arrayGraysM[arrayGraysM.length] = color;
else arrayGraysY[arrayGraysY.length] = color;
}
else if ( color.density >= 85 ) {
if ( c>m && c>y ) arrayDarksC[arrayDarksC.length] = color;
else if ( m>c && m>y ) arrayDarksM[arrayDarksM.length] = color;
else arrayDarksY[arrayDarksY.length] = color;
}
else if ( color.density <= 15 ) {
if ( c>m && c>y ) arrayPastelsC[arrayPastelsC.length] = color;
else if ( m>c && m>y ) arrayPastelsM[arrayPastelsM.length] = color;
else arrayPastelsY[arrayPastelsY.length] = color;
}
else {
if ( c>m && c>y ) arrayAssortedC[arrayAssortedC.length] = color;
else if ( m>c && m>y ) arrayAssortedM[arrayAssortedM.length] = color;
else arrayAssortedY[arrayAssortedY.length] = color;
}
}

arrayMagentas.sort(byProperty("density")); // Each item is an object, sort by 'density'
arrayReds.sort(byProperty("density"));
arrayOranges.sort(byProperty("density"));
arrayYellows.sort(byProperty("density"));
arrayWarmGreens.sort(byProperty("density"));
arrayCoolGreens.sort(byProperty("density"));
arrayCyans.sort(byProperty("density"));
arrayBlues.sort(byProperty("density"));
arrayPurples.sort(byProperty("density"));
arrayGraysC.sort(byProperty("density"));
arrayGraysM.sort(byProperty("density"));
arrayGraysY.sort(byProperty("density"));
arrayGraysK.sort(byProperty("density"));
arrayDarksC.sort(byProperty("density"));
arrayDarksM.sort(byProperty("density"));
arrayDarksY.sort(byProperty("density"));
arrayPastelsC.sort(byProperty("density"));
arrayPastelsM.sort(byProperty("density"));
arrayPastelsY.sort(byProperty("density"));
arrayAssortedC.sort(byProperty("density"));
arrayAssortedM.sort(byProperty("density"));
arrayAssortedY.sort(byProperty("density"));

// Sort gradient array by name
arrayGradient.sort(byProperty("name")); // Each item is an object, sort by 'name'

// Stick it all together: spot, CMYK, gradient values

// Dump back into the palette ... skipping None, Registration, Paper, Black
for (i=0; i<oldSwatches.length; i++) {
app.activeDocument.swatches[i+4] = newSwatches[i];
}
}

// Custom sorter
var byProperty = function(prop) {
return function(a,b) {
if (typeof a[prop] == "number") {
return (a[prop] - b[prop]);
} else {
return ((a[prop] < b[prop]) ? -1 : ((a[prop] > b[prop]) ? 1 : 0));
}
};
};

```
• ###### 11. Re: Sort color swatches in InDesign

The intent of that was to sub-sort those swatches so the ones where the C value is highest are together, the ones where the M value is highest are together, and ones where the Y value are highest are together. This would group them by hue (roughly), as opposed to merely sorting by brightness, etc.

• ###### 12. Re: Sort color swatches in InDesign

Hi MPrewitt,

Having fun with your idea although I still get a huge number of unassigned swatches in the 'Others' category (last group).

My script allows to play with the thresholds you've provided, you'll probably want to restore the original values.

Anyway here is the code so far:

```////////////////////////////////////////////////////////////
//
//  SWATCH SORTER for InDesign - v.1.001b - indiscripts.com
//
////////////////////////////////////////////////////////////

#targetengine 'SwatchSorter1001b'

\$.global.hasOwnProperty('ProgressBar')||(function(H/*OST*/,S/*ELF*/,I/*NNER*/)
{
H[S] = function ProgressBar(/*str*/title, /*uint*/width, /*uint*/height)
{
(60<=(width||0))||(width=340);
(40<=(height||0))||(height=60);

var H = 22,
Y = (3*height-2*H)>>2,
W = new Window('palette', ' '+title, [0,0,width,height]),
P = W.add('progressbar', { x:20, y:height>>2, width:width-40, height:12 }, 0,100),
T = W.add('statictext' , { x:0, y:Y, width:width, height:H }),
__ = function(a,b){ return localize.apply(null,a.concat(b)) };

this.pattern = ['%1'];

W.center();

// ---
// API
// ---

this.msg = function(/*str*/s,  v)
// ---------------------------------
{
s && (T.location = [(width-T.graphics.measureString(s)[0])>>1, Y]);

T.text = s;
W.update();
};

this.show = this.reset = function(/*str*/s, /*uint*/v)
// ---------------------------------
{
if( s && s != localize(s,1,2,3,4,5,6,7,8,9) )
{
this.pattern[0] = s;
s = __(this.pattern, [].slice.call(arguments,2));
}
else
{
this.pattern[0] = '%1';
}

P.value = 0;
P.maxvalue = v||0;
P.visible = !!v;

this.msg(s);

W.show();
W.update();
};

this.hit = function(x)
// ---------------------------------
{
++P.value;
('undefined' != typeof x) && this.msg(__(this.pattern, [].slice.call(arguments,0)));
W.update();
};

this.hide = function()
// ---------------------------------
{
W.hide();
};

this.close = function()
// ---------------------------------
{
W.close();
};
};

})(\$.global,{toString:function(){return 'ProgressBar'}},{});

\$.global.hasOwnProperty('SwatchSorter')||(function(H/*OST*/,S/*SELF*/,I/*NNER*/)
{
H[S] = S;

//======================================================
// DATA AND SHORTCUTS
//======================================================

I.O_ROOT = app;
I.O_LOCKED = { 'None':1, 'Black':1,'Paper':1, 'Registration':1 };

I.CM_MIX =     +ColorModel.MIXEDINKMODEL;
I.CM_PROCESS = +ColorModel.PROCESS;
I.CM_REG =     +ColorModel.REGISTRATION;
I.CM_SPOT =    +ColorModel.SPOT;

I.CS_CMYK =    +ColorSpace.CMYK;
I.CS_LAB =     +ColorSpace.LAB;
I.CS_MIX =     +ColorSpace.MIXEDINK;
I.CS_RGB =     +ColorSpace.RGB;

// Keep colors and tints together.
I.O_ORDER = { 'Color':1, 'Tint':1, 'Gradient':3, 'MixedInk':4 };

I.BUILD_CLUSTERS = 1;
I.O_CLUSTERS = {
'_0'     : 'Magentas',    // 0x0000
'_4096'  : 'Reds',        // 0x1000
'_8192'  : 'Oranges',     // 0x2000
// ---
'_16384' : 'Yellows',     // 0x4000
'_20480' : 'Warm Greens', // 0x5000
'_24576' : 'Cool Greens', // 0x6000
// ---
'_32768' : 'Cyans',       // 0x8000
'_36864' : 'Blues',       // 0x9000
'_40960' : 'Purples',     // 0xA000
// ===
'_49152' : 'Grays',       // 0xC000
'_53248' : 'Darks',       // 0xD000
'_57344' : 'Pastels',     // 0xE000
'_61440' : 'Others',      // 0xF000
};

//======================================================
// CMYK ROUTINES
//======================================================

I.F_APPLY_CMYK_TINT = function(/*0..100[4]&*/CMYK, /*]0,1]*/t)
//----------------------------------
{
CMYK[0] *= t;
CMYK[1] *= t;
CMYK[2] *= t;
CMYK[3] *= t;
};

I.F_TO_CMYK_KEY = function F(/*0..100[4]*/CMYK,  d,a,v,i,t)
//----------------------------------
{
const mABS = Math.abs,
mMIN = Math.min,
mMAX = Math.max;

const INK_MIN = 50,
INK_MAX = 35,
GRAY_VAR = 5,
DARK_MIN = 70,
PASTEL_MAX = 25;

F.DENSITY || (F.DENSITY=[49,60,7,91]);
F.BUFFER || (F.BUFFER=[0,0,0,0]);

for( d=0, a=F.BUFFER, v=F.DENSITY, i=-1 ; ++i < 4 ; (d+=v[i]*(t=CMYK[i])), a[i]=-(INK_MAX>t)||+(INK_MIN<t) );

d >>>= 3; // <= 0xA1B

for( t=(0<=a[3]), i=-1 ; (!t) && (++i < 3) ; t = 0 > a[i] && 0 < a[(1+i)%3] && a[(2+i)%3] );

if( 3 > (i&=3) )
{
// CMY CLASSES (0x0000 -> 0xAA1B).
// ---------------------------------------------
//              DOM SUB DENSITY(12b)
//              [i] [t] [d]
// ---------------------------------------------
// Magentas:    00  00  xxxx xxxx xxxx
// Reds:        00  01  xxxx xxxx xxxx
// Oranges:     00  10  xxxx xxxx xxxx
// ---------------------------------------------
// Yellows:     01  00  xxxx xxxx xxxx
// Warm Greens: 01  01  xxxx xxxx xxxx
// Cool Greens: 01  10  xxxx xxxx xxxx
// ---------------------------------------------
// Cyans:       10  00  xxxx xxxx xxxx
// Blues:       10  01  xxxx xxxx xxxx
// Purples:     10  10  xxxx xxxx xxxx
// ---------------------------------------------

++t && (t -= CMYK[(2+i)%3] <= CMYK[(1+i)%3] );
t |= (i<<2);
return d | (t<<12);
}

// ---
// Darks, pastels, grays, others
// ---

d >>>= 2;
i = 0;
t = 3;
while( v = CMYK[0] + CMYK[1] + CMYK[2] )
{
// Other classes (0xC000 -> 0xFFFF)
// ---------------------------------------------
//              MK  CTG DOM  DENSITY(10b)
//                  [i] [t]  [d]
// ---------------------------------------------
// Grays   (M)  11  00  00   xx xxxx xxxx
// Grays   (Y)  11  00  01   xx xxxx xxxx
// Grays   (C)  11  00  10   xx xxxx xxxx
// Grays   (K)  11  00  11   xx xxxx xxxx
// ---------------------------------------------
// Darks   (M)  11  01  00   xx xxxx xxxx
// Darks   (Y)  11  01  01   xx xxxx xxxx
// Darks   (C)  11  01  10   xx xxxx xxxx
// ---------------------------------------------
// Pastels (M)  11  10  00   xx xxxx xxxx
// Pastels (Y)  11  10  01   xx xxxx xxxx
// Pastels (C)  11  10  10   xx xxxx xxxx
// ---------------------------------------------
// Others  (M)  11  11  00   xx xxxx xxxx
// Others  (Y)  11  11  01   xx xxxx xxxx
// Others  (C)  11  11  10   xx xxxx xxxx
// ---------------------------------------------

v /= 3;
t = ( CMYK[1] < CMYK[2] || CMYK[1] < CMYK[0] ) << ( CMYK[2] < CMYK[0] );

// Grays
// ---
if( GRAY_VAR >= mMAX(mABS(CMYK[0]-v),mABS(CMYK[1]-v),mABS(CMYK[2]-v)) ) break;

// Darks
// ---
if( ++i && DARK_MIN < mMIN.apply(null,CMYK) ) break;

// Pastels
// ---
if( ++i && PASTEL_MAX > mMAX.apply(null,CMYK) ) break;

// Others
// ---
++i; break;
}

t |= 0x30 | (i<<2);
return d | (t<<10);
};

I.F_CONVERT_TO_CMYK = function(/*Color*/o,/*ColorSpace*/cs,/*ColorValue*/cv,  r)
//----------------------------------
{
try {
// Convert to cmyk space if possible.
// This might fail due to imported swatches.
// ---
o.space = I.CS_CMYK;
r = o.colorValue;

// Revert to initial color props.
// ---
o.properties = { space:cs, colorValue:cv };
}
catch(_)
{
// Not implemented
// if( I.CS_RGB==cs && 3==cv.length ) r = I.FN_RGB_TO_CMYK_APPROX(cv);
}

return r || false;
};

I.F_COLOR_TO_CMYK = function(/*Color*/o,  r,cv,cs)
//----------------------------------
{
r = false;

if( I.CM_MIX == +o.model ) return r;

cv = o.colorValue;
cs = +o.space;

r = I.CS_CMYK == cs ? cv : I.F_CONVERT_TO_CMYK(o,cs,cv);

return r;
};

//======================================================
// PARSING
//======================================================

I.F_TO_FULL_KEY = function(/*uint*/order,/*[c,m,y,k]*/CMYK,/*str*/name,/*uint*/id)
//----------------------------------
{
return String.fromCharCode(0x40+order,I.F_TO_CMYK_KEY(CMYK)) +
name + '\x01' + id;
};

I.F_PARSE_Color = function(/*Color*/o,/*str*/name,/*uint*/id,  a,k)
//----------------------------------
{
if( I.O_LOCKED.hasOwnProperty(name) ) return '';

if( !(a=I.F_COLOR_TO_CMYK(o)) ) return '';

return I.F_TO_FULL_KEY(I.O_ORDER['Color'],a,name,id);
};

I.F_PARSE_Tint = function(/*Tint*/o,/*str*/name,/*uint*/id,  bc,a,k)
//----------------------------------
{
bc = o.baseColor;

if( I.O_LOCKED.hasOwnProperty(bc.name) ) return '';

if( !(a=I.F_COLOR_TO_CMYK(bc)) ) return '';

I.F_APPLY_CMYK_TINT(a,o.tintValue/100);

return I.F_TO_FULL_KEY(I.O_ORDER['Tint'],a,name,id);
};

//----------------------------------
// Not implemented
{
return '';
};

I.F_PARSE_MixedInk = function(/*Color*/o,/*str*/name,/*uint*/id)
//----------------------------------
// Not implemented
{
return '';
};

//==========================================================================
// ORDERING
//==========================================================================

I.F_APPLY_ORDER_CLUSTERS = function(/*ProgressBar*/PB,/*str{}*/data,/*Swatches*/coll,  n,o,i,k,s,t)
//----------------------------------
{
OC = I.O_CLUSTERS;

n = data.length;
PB.reset("Assigning groups... (%1 / %2)",n);

o = {};

for( i = 0 ; i < n ; ++i )
{
PB.hit(1+i,n);
if( !(k=data[i]) ) continue;

s = '_' + (CM & k.charCodeAt(1));
if( !OC.hasOwnProperty(s) ) continue;
s = OC[s];

k = k.substr(2).split('\x01');
if( !(t=coll.itemByID(parseInt(k[1],10))).isValid ) continue;

(o[s]||(o[s]=[])).push(t);
}

n = o.__count__;
i = 0;
t = I.O_ROOT.colorGroups;
PB.reset("Creating group %1... (%2 / %3)",n);
for( k in o )
{
if( !o.hasOwnProperty(k) ) continue;
PB.hit(k,++i,n);
o[k].length = 0;
}
};

I.F_APPLY_ORDER_FLAT = function(/*ProgressBar*/PB,/*str{}*/data,/*Swatches*/coll,  n,i,k,t,o,d)
//----------------------------------
{
n = data.length;
PB.reset("Reordering swatches... (%1 / %2)",n);

for( i = 0 ; i < n ; ++i )
{
PB.hit(1+i,n);
if( !(k=data[i]) ) continue;
k = k.substr(2).split('\x01');
if( !(t=coll.itemByID(parseInt(k[1],10))).isValid ) continue;

t = t.getElements()[0]; // !important!

switch( t.constructor.name )
{
case 'Color':
o = t.duplicate();
t.remove(o);
o.name = k[0];
break;
case 'Tint':
o = t.properties;
d = o.tintValue > 50 ? -.001 : +.001;
o.tintValue += d;
t.remove(o);
o.tintValue -= d;
break;
default:
// not implemented
}
}
};

I.F_PROCESS_ALL_SWATCHES = function(/*ProgressBar*/PB,/*Swatches*/coll,  ei,a,names,ids,i,n,t,k)
//----------------------------------
{
ei = coll.everyItem();

a = ei.getElements();
names = ei.name;
ids = ei.id;

i = n = a.length;

// Parsing swatches
// ---
PB.reset("Parsing swatches... (%1 / %2)",n);
while( i-- )
{
PB.hit(n-i,n);
t = a[i];
k = 'F_PARSE_' + t.constructor.name;
a[i] = I.hasOwnProperty(k) ? I[k](t,names[i],ids[i]) : '';
}

// Sorting
// ---
PB.reset("Sorting colors...");
a.sort();

// Reordering swatches
// ---
I['F_APPLY_ORDER_' + ((I.BUILD_CLUSTERS && 'colorGroups' in app) ? 'CLUSTERS' : 'FLAT')](PB,a,coll);

a.length = 0;
};

//==========================================================================
// IDLE MANAGER
//==========================================================================

function F(/*?fct*/callback,  t)
//----------------------------------
{

// Cleanup
// ---
{
t.eventListeners.everyItem().remove();
t.remove();
}

// Set callback (if any)
// ---
if( 'function' == typeof callback )
{
}
}:
function F(/*fct*/callback)
//----------------------------------
{
if( 'function' == typeof callback ) callback();
}

(I.F_EXIT_PROCESS = function F()
//----------------------------------
{
// ---

// Close the PB
// ---
F.Q && (F.Q.close());

}).Q = 0;

//==========================================================================
// API
//==========================================================================

S.run = function(/*?Document*/doc,/*bool=0*/NO_CLUSTERS,  PB,t)
//----------------------------------
{
I.BUILD_CLUSTERS = +!NO_CLUSTERS;

PB = new ProgressBar(S + ' \xA9indiscripts.com',400,100);

if( ('panels' in app) && (t=app.panels.itemByName('\$ID/Swatches')).visible ) t.visible = false;

doc || (doc=app.properties.activeDocument);

if( doc instanceof Document )
{
doc.preflightOptions.properties = { preflightOff: true };
t = doc;
}
else
{
t = app;
}

if( ('colorGroups' in app) && 1 < t.colorGroups.length )
{
t.colorGroups.itemByRange(1,-1).ungroup();
}

I.O_ROOT = t;

app.doScript('I.F_PROCESS_ALL_SWATCHES(PB,t.swatches);',
ScriptLanguage.javascript,
undefined,
UndoModes.entireScript,
''+S
);

if( 'panels' in app )
{
app.panels.itemByName('\$ID/Swatches').visible = true;
}

I.F_EXIT_PROCESS.Q = PB;
};

})(\$.global,{toString:function(){return 'SwatchSorter'}}, {});

SwatchSorter.run();
```

@+

Marc

• ###### 13. Re: Sort color swatches in InDesign

Hi Marc and all,

Pretty disappointing that you didn't even get a ta very much from MPrewitt for you 548 line solution but just for the crossword puzzle value here's my half pennies worth.

I didn't implement the sort for the non standard swatches, it would be very easy to add it but I couldn't be bothered.

Your method is obviously  more efficient, the downside  of it being the "other" category. As I wrote I think the only real solution is in the form of an extension that does not rely on the incredibly slow  DOM operations and a really good solution should probably use c++ for the number churning.  Other than that I think the best solution would be to combine our two completely different methods using yours as the primary and mine to deal with the other  category, my only reservation being that there is clearly some miscategorization by your method which is tougher to fix than in mine.

My method is based on comparing the swatches colorValues with colorValues  in a dictionary object that contains the color values in RGB CMYK and Lab of the  standard CSS colors including their shades and the greys, matching the closest definition and indexing accordingly and retrieving the correct color group.

I made this dictionary by greping the rgb.txt file to produce the range of swatches on  an ID document and then converting to CMYK and Lab spaces to add to the object those values, I then used the swatch panel to order and group the colors to inject those values into the dictionary

.

Non sorted colors from rgb.txt file

Manually sorted colors

Manually categorized colors

Script sorted by my method, a bit of a mess but no others

Sorted by your method, nice and tidy but a lot of others.

Off course my method totally ignores the op's spec (doesn't bother me) and could be significantly improved by applying a better manual sort and grouping.  My method also ignores color profiles.

```(function wrapper(){
function sortSwatches () {
const wantToGroupColor = true; // change to false if err you don't want to group
const process, notCopiableSwatchReg, standardColorReg;
process = +ColorModel.PROCESS;
notCopiableSwatchReg = /^Black|None|Paper|Registration\$/;
standardColorReg = /C/;
var papa, pSwatches, groupColors, l, colorTable, n, colorModel, newColor, oldColor, colorGroup, colorGroups;
// if document open process document swatches otherwise process application swatches
papa = app.properties.activeDocument || app;

// setup color groupings (only useful for CC2015+ I think)
groupColors = wantToGroupColor && ('colorGroups' in app);
if (groupColors) {
if (papa.colorGroups.length > 1) {
papa.colorGroups.itemByRange(1,-1).ungroup();
};

// These names should match the colorGroup names in the cssColors object, use find and change all to change the names
colorGroups = {
"Blues":[],
"Purples":[],
"Cyans":[],
"Greens":[],
"Yellows":[],
"Reds":[],
"Magentas":[],
"Pinks":[],
"Oranges":[],
"Browns":[],
"Pastels":[],
"Light Grays and Whites":[],
"Medium Grays":[],
"Blacks And Darks Grays":[],
}
}

// Make array of processable swatches
pSwatches = papa.swatches.everyItem ().getElements ().slice(0);
l = pSwatches.length;
// if there are less than 2 copyable swatches then we might as well quit;
if (l < 6) exit(); // Black, None, Reg. & Paper are not copyable => 4 + 2 = 6
// Make a filtered array of colors removing non copyable swatches
colorTable = [];
/// Make Color Table
while (color = pSwatches.pop()) {
// for now not bothering to deal with mixed inks, gradients and tints
color && !notCopiableSwatchReg.test(color.name) && standardColorReg.test(color.constructor.name) && colorTable.push({color: color, properties: color.properties});
};

// Index the swatches by comparing with dictionary values
l = colorTable.length;
while (l--) {
colorTable[l].cssData = indexByCSS(colorTable[l].color.colorValue, +colorTable[l].color.space);
colorTable[l]._index = colorTable[l].cssData[0];
colorTable[l].colorGroup = colorTable[l].cssData[1];
}

byValue = function(a, b) {
return a._index < b._index;
}

colorTable.sort(byValue);

// Add the colors and put them in groups if desired
l = colorTable.length;
for (n = 0; n < l; n++) {
colorTable[n].color.remove(newColor);
newColor.properties = colorTable[n].properties;
if (groupColors) {
colorGroups[colorTable[n].colorGroup].push(newColor);
}
}
if (groupColors) {
for (colorGroup in colorGroups) {
}
}
};

/***********************************************************************************************************/
/******                               Helper Functions and const                            ****************/
/***********************************************************************************************************/

const cssColors = {
}

function indexByCSS(colorValues /* Array of Color Values */, space) {
var closestKey, closestValue, key, keys, delta;
/*
+ColorSpace.RGB
Result: 1666336578
+ColorSpace.CMYK
Result: 1129142603
+ColorSpace.LAB
Result: 1665941826
*/
// Associate the swatches to their closest match in the cssColors dictionary object
// Calculate the closest match by using very primitive delta values
// Return an array of the index value + the delta value for the purpose of sorting the colors and the color group for placing in the correct color groups
// Made this function verbose to make it easier to understand
if (space === 1666336578) { // RGB
var r, g, b, R, G, B;
r = colorValues[0];
g = colorValues[1];
b = colorValues[2];
// convert the [10, 0, 255] format into _0a00ff format
key = "_" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
// check if the key is in our index if so just return the keys index and the 0000 delta
if (key in cssColors) return [cssColors[key]._index + "0000", cssColors[key].colorGroup];
// Drat no direct match so we have to work for it
closestKey = "_";
closestValue = 1e10;
for (keys in cssColors) {
R = cssColors[keys].r;
G = cssColors[keys].g;
B = cssColors[keys].b;
//delta = (R - r) * (R - r) + (G - g) * (G - g) + (B - b) * (B - b);
delta = Math.abs(R - r) + .75 * Math.abs(G - g) + Math.abs(B - b);
if (delta < closestValue) {
closestKey = keys;
closestValue = delta;
}
}
closestValue = ("0000" + closestValue.toString(16)).substr(-4);
return [cssColors[closestKey]._index + closestValue, cssColors[closestKey].colorGroup];
} else if (space === 1665941826) { // Lab
var L, A, B, l, a, b;
l = colorValues[0];
a = colorValues[1];
b = colorValues[2];

// No exact key match possible for Lab or CMYK values
closestKey = "_";
closestValue = 1e10;
for (keys in cssColors) {
L = cssColors[keys].L;
A = cssColors[keys].A;
B = cssColors[keys].B;
delta = ((L - l) * (L - l) + (A - a) * (A - a) + (B - b) * (B - b)); // CIE76 (without the sqrt) use the quickest comparison even if it's the most pathetic
if (delta < closestValue) {
closestKey = keys;
closestValue = delta;
}
}
closestValue = ("0000" + closestValue.toString(16)).substr(-4);
return [cssColors[closestKey]._index + closestValue, cssColors[closestKey].colorGroup];
} else if (space === 1129142603) { // CMYK
var C, M, Y, K, c, m, y, k;
c = colorValues[0];
m = colorValues[1];
y = colorValues[2];
k = colorValues[3];
// No exact key match possible for Lab or CMYK values
closestKey = "_";
closestValue = 1e10;
for (keys in cssColors) {
C = cssColors[keys].C;
M = cssColors[keys].M;
Y = cssColors[keys].Y;
K = cssColors[keys].K;
delta = ((C - c) * (C - c) + (M - m) * (M - m) + (Y - y) * (Y - y) + (K - k) * (K - k)); // CIE76 (without the sqrt) use the quickest comparison even if it's the most pathetic
if (delta < closestValue) {
closestKey = keys;
closestValue = delta;
}
}
closestValue = ("0000" + closestValue.toString(16)).substr(-4);
return [cssColors[closestKey]._index + closestValue, cssColors[closestKey].colorGroup];
}
} // end of indexByCSS

var start = new Date;
app.panels.itemByName('\$ID/Swatches').visible = false;
// app.scriptPreferences.userInteractionLevel = +UserInteractionLevels.INTERACT_WITH_ALL;
app.doScript (sortSwatches, undefined, undefined, UndoModes.FAST_ENTIRE_SCRIPT);
app.panels.itemByName('\$ID/Swatches').visible = true;
// \$.writeln("took " + (new Date - start) / 1000 + " seconds");

})(); // run the wrapper

```

Regards Trevor

Custom Scripts & Services | Creative-Scripts.com

• ###### 14. Re: Sort color swatches in InDesign

My method also does some bad categorization so given the difference in the efficiency of the methods I guess mine can go in the trash, oh well that's how it goes some times!

• ###### 15. Re: Sort color swatches in InDesign

Thank you for all your efforts!

• ###### 16. Re: Sort color swatches in InDesign

Most of the problem palettes I've dealt with are maybe 40-100 swatches. I've never seen a publication with more than 100 or so swatches in 20+ years. But I've only worked for nonprofits, and everything designed in-house with no third-party content. Large palettes with 1000+ swatches are basically the same as the libraries that ship with InDesign, which are more-or-less already sorted (although not always in the best way).

• ###### 17. Re: Sort color swatches in InDesign

Thanks, that is awesome.

Sorry I didn't reply sooner. I actually didn't see your answer until today, and not sure why. I thought I was subscribed to updates on this thread, but did not see any until now. Maybe they were getting grouped in my inbox and bumped out of order. I was also out of the office for about 5 days.

Anyway, that is a great start. I will try it out and report back.

• ###### 18. Re: Sort color swatches in InDesign

I just tested Marc's script. It works great, but I will try some tweaks (see comments below).

Turns out I had more colors than I thought. In this one magazine I would have guessed 50-60 colors, but there was actually 128. I use the list view, and there are more colors than there seemed to be.

I found I got much better results, with far fewer 'other' colors, using these values:

INK_MIN = 25,

INK_MAX = 60,

GRAY_VAR = 20,

DARK_MIN = 70,

PASTEL_MAX = 30;

The main remaining issues with the auto-sorting seems to be with ink density.

• For example, I have a color C0 M90 Y100 K0. The script categorizes it as an orange, because the yellow has the highest value. But visually it is more like red. The issue is that the M ink is so intense, it overpowers the Y ink even though the Y ink percentage is higher.
• The cyan color is a tricky beast too. For example, C51 M100 Y0 K47 is grouped with magentas, but looks more purple. It doesn't take much cyan to throw the magentas toward purple.
• C97 M9 Y54 K9 is grouped with cyans, but looks like a cool green. It doesn't take much yellow to throw the cyans and blues toward green.
• C90 M90 Y30 K50 is grouped with blues, but looks like a purple.
• Grays contains only the very lightest and darkest grays/blacks. It's missing a lot of colors that are grayish, like C70 M71 Y43 K52, which was put in the purple group, and C63 M52 Y47 K37, which was put in the cyan group.
• Some light yellows and light blues got classed as 'other' even though visually they fall in the pastel range.

I will keep experimenting.

• ###### 19. Re: Sort color swatches in InDesign

Hi Marc,

Are you interested in developing this further? Would you be OK with posting it to GitHub for community development? I can do that, or you can, whichever you prefer.

Regards,

Michael