Reputation: 23
I'd like to create many color swatches in InDesign based on an existing color swatch. I would like to take the numbers of the existing swatch, and create new swatches by adding values, i.e. Cyan +1, Magenta +2, Yellow +0, Black +0. Thank you!
Upvotes: 0
Views: 1227
Reputation: 14537
Here is the rather simple straightforward implementation:
var COLORS = app.activeDocument.colors;
var BASE = COLORS.itemByName("BASE");
function add_color(name,c,m,y,k) {
var color = COLORS.add();
color.space = ColorSpace.CMYK;
color.colorValue = [
BASE.colorValue[0] + c,
BASE.colorValue[1] + m,
BASE.colorValue[2] + y,
BASE.colorValue[3] + k ];
color.name = get_name(name);
}
// color names should not be the same, it adds "+" to the repeated names
function get_name(name) {
try {
return get_name(COLORS.itemByName(name).name + "+");
} catch(e) {
return name;
}
}
add_color("#1", 1, 0, 0, 0);
add_color("#2", 0, 1, 0, 0);
add_color("#3", 0, 0, 1, 0);
add_color("#4", 0, 0, 0, 1);
add_color("#5",-1, 0, 0, 0);
add_color("#6", 0,-1, 0, 0);
add_color("#7", 0, 0,-1, 0);
add_color("#8", 0, 0, 0,-1);
Perhaps you need to think of better way to handle the repeated color names.
Upvotes: 1
Reputation: 2097
Something like this should do the job.
const clamp = (value, min, max) => Math.max(min, Math.min(value, max));
// value: { c: number, m: number, y: number, k: number }
const swatch = (value) => {
const results = [];
const keys = ['c', 'm', 'y', 'k'];
for (let i = 1; i >= -1; i -= 2) {
for (let key of keys) {
if (clamp(value[key] + i, 0, 100) != value[key]) {
results.push({ ...value, [key]: value[key] + i });
}
}
}
return results;
};
console.log(swatch({
c: 40,
m: 20,
y: 40,
k: 20
}));
Upvotes: 1