-
Notifications
You must be signed in to change notification settings - Fork 2
/
rgbtocmyk.jsx
121 lines (109 loc) · 3.48 KB
/
rgbtocmyk.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// (c) 2024 MapCreator BV, the Netherlands
var defaultColorMappings=
[
//list of colors to replace.
// format of rgbin is "<R> <G> <B>" with R,G,B as whole numbers from 0..255
// format of cmykin is "<C> <M> <Y> <K>" with C,M,Y,K as fractional numbers rounded to 2 decimal places, incl trailing zeros from 0..100
// format of cmykout is "<C> <M> <Y> <K>" with C,M,Y,K as fractional numbers from 0..100
//samples:
//{rgbin:"214 250 217", cmykout:"100 0 0 0"},
//{rgbin:"255 255 255", cmykout:"100 0 0 0"},
//{cmykin:"14.12 0.00 12.94 1.96", cmykout:"0 0 100 0"}
]
{
//Convert colors to cmyk
var items = app.activeDocument.pageItems;
for (var i = 0; i < items.length; i++) {
AddNote(items[i]);
}
app.executeMenuCommand('doc-color-cmyk');
var items = app.activeDocument.pageItems;
for (var i = 0; i < items.length; i++) {
ParseNote(items[i],defaultColorMappings);
}
alert('Ready');
}
function AddNote(item) {
try {
item.note=onecol(item.textRange.characterAttributes.fillColor)+'|'+onecol(item.textRange.characterAttributes.strokeColor);
}catch(e){};
try {
var f=onecol(item.fillColor);
if (!item.filled)
f="none";
var s=onecol(item.strokeColor);
if (!item.stroked)
s="none";
item.note=f+'|'+s;
}catch(e){};
}
function ParseNote(item,defaultColorMappings) {
if (item.note!="")
{
var colors=ParseOne(item.note,defaultColorMappings);
if (item.textRange!=null)
{
try {
item.textRange.characterAttributes.fillColor=colors.fill;
item.textRange.characterAttributes.strokeColor=colors.stroke;
item.note="";
}catch(e){};
}
else
{
try {
if (item.filled)
item.fillColor=colors.fill;
if (item.stroked)
item.strokeColor=colors.stroke;
item.note="";
}catch(e){}
}
}
}
function onecol(col) {
if (col.toString()=="[NoColor]")
return "none";
else
if (col.toString()=="[GrayColor]")
return ''+col.gray*2.55+' '+col.gray*2.55+' '+col.gray*2.55;
else
return ''+col.red+' '+col.green+' '+col.blue;
}
function ParseOne(note,defaultColorMappings) {
var parts=note.split("|");
return { fill:parse(parts[0],defaultColorMappings), stroke:parse(parts[1],defaultColorMappings) };
}
function parse(col,defaultColorMappings) {
if (col=="none")
return new NoColor();
for (var i = 0; i < defaultColorMappings.length; i++) {
if (defaultColorMappings[i].rgbin==col)
return colFromString(defaultColorMappings[i].cmykout);
}
var rgb=col.split(" ");
var c=(255-Number(rgb[0]))/255;
var m=(255-Number(rgb[1]))/255;
var y=(255-Number(rgb[2]))/255;
var k=Math.min(c,Math.min(m,y));
var res= new CMYKColor();
res.black = k*100;
res.cyan = (c-k)*100;
res.magenta = (m-k)*100;
res.yellow = (y-k)*100;
var searchColor = ''+res.cyan.toFixed(2)+' '+res.magenta.toFixed(2)+' '+res.yellow.toFixed(2)+' '+res.black.toFixed(2);
for (var i = 0; i < defaultColorMappings.length; i++) {
if (defaultColorMappings[i].cmykin==searchColor)
return colFromString(defaultColorMappings[i].cmykout);
}
return res;
}
function colFromString(s) {
var res= new CMYKColor();
var cmyk=s.split(" ");
res.cyan = Number(cmyk[0]);
res.magenta = Number(cmyk[1]);
res.yellow = Number(cmyk[2]);
res.black = Number(cmyk[3]);
return res;
}