-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathjamBooks.jsxinc
More file actions
489 lines (484 loc) · 22.9 KB
/
jamBooks.jsxinc
File metadata and controls
489 lines (484 loc) · 22.9 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//------------------------------------------------------------------------------
// File: jamBooks.jsxinc
// Version: 4.5.1
// Release Date: 2016-10-13
// Copyright: © 2016 Michel MARIANI <http://www.tonton-pixel.com/blog/>
// Licence: GPL <http://www.gnu.org/licenses/gpl.html>
//------------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------
// Version History:
// 4.5.1:
// - Added book description and simplified name of color components fields in
// function jamBooks.getColorBookFileColors ().
// 4.5:
// - Initial release.
//------------------------------------------------------------------------------
/**
* @fileOverview
* @name jamBooks.jsxinc
* @author Michel MARIANI
*/
//------------------------------------------------------------------------------
if (typeof jamBooks !== 'object')
{
/**
* Global object (used to simulate a namespace in JavaScript) containing
* a set of color books-related functions for scripts written with the
* <a href="http://www.tonton-pixel.com/blog/json-photoshop-scripting/json-action-manager/">JSON Action Manager</a> engine.<br>
* Uses information found in the documents
* <a href="http://magnetiq.com/pages/acb-spec/">Adobe Color Book File Format Specification</a> and
* <a href="http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577411_pgfId-1066780">Adobe Photoshop File Formats Specification</a>.
* @author Michel MARIANI
* @version 4.5.1
* @namespace
*/
var jamBooks = { };
//
(function ()
{
/**
* @description Test if a given file is a color book file (*.acb).
* @param {Object} file File object
* @returns {Boolean} true if color book file
* @example
* function colorBookFileFilter (f)
* {
* return (f instanceof Folder) || jamBooks.<strong>isColorBookFile</strong> (f);
* }
* var select = (File.fs === "Macintosh") ? colorBookFileFilter : "Color Book Files:*.acb,All Files:*";
* var colorBookFile = File.openDialog ("Select a color book file:", select);
* if (colorBookFile !== null)
* {
* alert ("OK!");
* }
*/
jamBooks.isColorBookFile = function (file)
{
return (file.type === '8BCB') || file.name.match (/\.acb$/i);
};
//
/**
* @description Parse a color book file (*.acb) into a data structure in JSON object format.
* @param {String|Object} colorBookFile Color book file path string or File object
* @param {Boolean} [actualComponents] List actual color components instead of raw color components
* @returns {Object|String} Parsed color book file data structure in JSON object format, or error message string
* @see jamBooks.dataToColorBookFile
* @example
* var acbFilter =
* (File.fs === "Macintosh") ?
* function (f) { return (f instanceof Folder) || jamBooks.isColorBookFile (f) } :
* "Color Book Files:*.acb,All Files:*.*";
* var colorBookFile = File.openDialog ("Open color book file:", acbFilter);
* if (colorBookFile)
* {
* var colorBookData = jamBooks.<strong>dataFromColorBookFile</strong> (colorBookFile);
* if (typeof colorBookData === 'string')
* {
* alert ("Error: " + colorBookData);
* }
* else
* {
* alert ("Color space: " + colorBookData["colorSpace"]);
* alert ("Number of colors: " + colorBookData["colors"].length);
* }
* }
*/
jamBooks.dataFromColorBookFile = function (colorBookFile, actualComponents)
{
var file;
if (typeof colorBookFile === 'string')
{
file = new File (colorBookFile);
}
else if (colorBookFile instanceof File)
{
file = colorBookFile;
}
//
var colorBookData;
if (file.open ("r"))
{
try
{
file.encoding = 'BINARY';
var magicNumber = file.read (4);
if (magicNumber === '8BCB')
{
function readBEInt (file, byteCount)
{
var bytes = file.read (byteCount);
var intValue = 0;
for (var index = 0; index < byteCount; index++)
{
intValue = (intValue << 8) + bytes.charCodeAt (index);
}
return intValue;
}
function readUnicodeString (file)
{
var unicodeString = "";
var unicodeLength = readBEInt (file, 4); // Includes terminating null
for (var index = 0; index < unicodeLength; index++)
{
var unicodeChar = readBEInt (file, 2);
if (unicodeChar !== 0)
{
unicodeString += String.fromCharCode (unicodeChar);
}
}
return unicodeString;
}
var formatVersion = readBEInt (file, 2);
if (formatVersion === 1)
{
var colorBook = { };
colorBook["bookID"] = readBEInt (file, 2);
colorBook["bookName"] = readUnicodeString (file);
colorBook["colorNamePrefix"] = readUnicodeString (file);
colorBook["colorNameSuffix"] = readUnicodeString (file);
colorBook["bookDescription"] = readUnicodeString (file);
var colorCount = readBEInt (file, 2);
colorBook["colorsPerPage"] = readBEInt (file, 2);
colorBook["keyColorIndex"] = readBEInt (file, 2);
var colorSpace = readBEInt (file, 2);
var colorSpaces = [ "RGB", null, "CMYK", null, null, null, null, "Lab" ];
if ((colorSpace < 0) || (colorSpace > colorSpaces.length))
{
throw new Error ("[jamBooks.dataFromColorBookFile] Invalid color space: " + colorSpace);
}
else
{
colorBook["colorSpace"] = colorSpaces[colorSpace];
}
var colors = [ ];
for (var colorIndex = 0; colorIndex < colorCount; colorIndex++)
{
var color = { };
color["colorName"] = readUnicodeString (file);
color["colorKey"] = file.read (6);
var components = file.read ((colorSpace === 2) ? 4 : 3);
var componentArr = [ ];
for (componentIndex = 0; componentIndex < components.length; componentIndex++)
{
var componentValue = components.charCodeAt (componentIndex);
if (actualComponents)
{
switch (colorSpace)
{
case 2: // CMYK
componentValue = (255 - componentValue) / 255 * 100; // 0% to 100%
break;
case 7: // Lab
if (componentIndex > 0) // a or b
{
componentValue -= 128; // -128 to 127
}
else // L (luminance)
{
componentValue = componentValue / 255 * 100; // 0% to 100%
}
break;
}
}
componentArr.push (componentValue);
}
color[(actualComponents) ? "actualComponents" : "rawComponents"] = componentArr;
colors.push (color);
}
colorBook["colors"] = colors;
if (!file.eof)
{
colorBook["spotProcess"] = file.read (8); // Either 'spflspot' or 'spflproc'
}
colorBookData = colorBook;
}
else
{
throw new Error ("[jamBooks.dataFromColorBookFile] Unrecognized format version: " + formatVersion);
}
}
else
{
throw new Error ("[jamBooks.dataFromColorBookFile] Unrecognized magic number: " + magicNumber);
}
}
catch (e)
{
colorBookData = e.message;
}
finally
{
file.close ();
}
}
else
{
colorBookData = "[jamBooks.dataFromColorBookFile] Cannot open file";
}
return colorBookData;
};
//
/**
* @description Generate a color book file (*.acb) from a data structure in JSON object format.
* @param {String|Object} colorBookFile Color book file path string or File object
* @param {Object} colorBookData Color book file data structure in JSON object format
* @returns {String} Error message string (empty if no error)
* @see jamBooks.dataFromColorBookFile
* @example
* var jsonFilter =
* (File.fs === "Macintosh") ?
* function (f) { return (f instanceof Folder) || f.name.match (/\.json$/i) } :
* "JSON Text Files:*.json,All Files:*.*";
* var jsonFile = File.openDialog ("Open JSON text file:", jsonFilter);
* if (jsonFile)
* {
* var colorBookData = jamUtils.readJsonFile (jsonFile);
* if (colorBookData)
* {
* var result = jamBooks.<strong>dataToColorBookFile</strong> ("~/Desktop/color-book.acb", colorBookData);
* if (result)
* {
* alert ("Error: " + result);
* }
* else
* {
* alert ("Color book file generated on Desktop.");
* }
* }
* else
* {
* alert ("Invalid JSON text file!");
* }
* }
*/
jamBooks.dataToColorBookFile = function (colorBookFile, colorBookData)
{
var file;
if (typeof colorBookFile === 'string')
{
file = new File (colorBookFile);
}
else if (colorBookFile instanceof File)
{
file = colorBookFile;
}
//
var result = "";
if (file.open ('w', '8BCB', '8BIM'))
{
try
{
function writeBEInt (file, byteCount, intValue)
{
var bytes = "";
for (var index = 0; index < byteCount; index++)
{
bytes = String.fromCharCode (intValue & 0xFF) + bytes;
intValue >>= 8;
}
file.write (bytes);
}
function writeUnicodeString (file, unicodeString)
{
var unicodeLength = unicodeString.length;
writeBEInt (file, 4, unicodeLength); // Doesn't include terminating null!
for (var index = 0; index < unicodeLength; index++)
{
writeBEInt (file, 2, unicodeString.charCodeAt (index));
}
}
file.encoding = "BINARY";
file.write ('8BCB');
writeBEInt (file, 2, 1);
writeBEInt (file, 2, colorBookData["bookID"]);
writeUnicodeString (file, colorBookData["bookName"]);
writeUnicodeString (file, colorBookData["colorNamePrefix"]);
writeUnicodeString (file, colorBookData["colorNameSuffix"]);
writeUnicodeString (file, colorBookData["bookDescription"]);
var colors = colorBookData["colors"];
var colorCount = colors.length;
writeBEInt (file, 2, colorCount);
writeBEInt (file, 2, colorBookData["colorsPerPage"]);
if ("keyColorIndex" in colorBookData)
{
writeBEInt (file, 2, colorBookData["keyColorIndex"]);
}
else if ("keyColorPage" in colorBookData) // Legacy...
{
writeBEInt (file, 2, colorBookData["keyColorPage"]);
}
var colorSpace = colorBookData["colorSpace"];
var colorSpaces = { "RGB": 0, "CMYK": 2, "Lab": 7 };
if (colorSpace in colorSpaces)
{
writeBEInt (file, 2, colorSpaces[colorSpace]);
}
else
{
throw new Error ("[jamBooks.dataToColorBookFile] Invalid color space: " + jamJSON.stringify (colorSpace));
}
for (var colorIndex = 0; colorIndex < colorCount; colorIndex++)
{
var color = colors[colorIndex];
writeUnicodeString (file, color["colorName"]);
var colorKey = color["colorKey"];
if (colorKey.length === 6)
{
file.write (colorKey);
}
else
{
throw new Error ("[jamBooks.dataToColorBookFile] Invalid color key: " + jamJSON.stringify (colorKey));
}
if ("actualComponents" in color)
{
var components = color["actualComponents"];
for (componentIndex = 0; componentIndex < components.length; componentIndex++)
{
var componentValue = components[componentIndex];
switch (colorSpace)
{
case "CMYK":
componentValue = 255 - Math.round (componentValue * 255 / 100);
break;
case "Lab":
if (componentIndex > 0) // a or b
{
componentValue = Math.round (componentValue) + 128;
}
else // L (luminance)
{
componentValue = Math.round (componentValue * 255 / 100);
}
break;
case "RGB":
componentValue = Math.round (componentValue);
break;
}
file.write (String.fromCharCode (componentValue));
}
}
else if ("rawComponents" in color)
{
var components = color["rawComponents"];
for (componentIndex = 0; componentIndex < components.length; componentIndex++)
{
file.write (String.fromCharCode (components[componentIndex]));
}
}
else if ("components" in color) // Legacy...
{
var components = color["components"];
for (componentIndex = 0; componentIndex < components.length; componentIndex++)
{
file.write (String.fromCharCode (components[componentIndex]));
}
}
}
if ("spotProcess" in colorBookData)
{
file.write (colorBookData["spotProcess"]); // Either 'spflspot' or 'spflproc'
}
}
catch (e)
{
result = e.message;
}
finally
{
file.close ();
}
}
else
{
result = "[jamBooks.dataToColorBookFile] Cannot open file";
}
return result;
};
//
/**
* @description Get the list of colors from a color book file (*.acb).
* @param {String|Object} colorBookFile Color book file path string or File object
* @param {Boolean} [roundComponents] Round color components
* @returns {Object|Null} List of colors from a color book file (*.acb), or null if error
* @example
* var acbFilter =
* (File.fs === "Macintosh") ?
* function (f) { return (f instanceof Folder) || jamBooks.isColorBookFile (f) } :
* "Color Book Files:*.acb,All Files:*.*";
* var colorBookFile = File.openDialog ("Open color book file:", acbFilter);
* if (colorBookFile )
* {
* var colorBookColors = jamBooks.<strong>getColorBookFileColors</strong> (colorBookFile, true);
* if (colorBookColors)
* {
* alert ("First color:\r"+ jamJSON.stringify (colorBookColors["bookColors"][0], '\t'));
* }
* }
*/
jamBooks.getColorBookFileColors = function (colorBookFile, roundComponents)
{
var colorBookColors = null;
var colorBookData = this.dataFromColorBookFile (colorBookFile, true);
if (colorBookData !== 'string')
{
colorBookColors = { };
colorBookColors["bookFile"] = File.decode (colorBookFile.name);
colorBookColors["bookName"] = localize (colorBookData["bookName"]);
colorBookColors["bookDescription"] = localize (colorBookData["bookDescription"]);
var colorNamePrefix = localize (colorBookData["colorNamePrefix"]);
var colorNameSuffix = localize (colorBookData["colorNameSuffix"]);
var colorSpace = colorBookData["colorSpace"];
var colors = colorBookData["colors"];
bookColors = [ ];
for (var i = 0; i < colors.length; i++)
{
var bookColor = { };
var colorName = localize (colors[i]["colorName"]);
if (colorName)
{
bookColor["name"] = colorNamePrefix + colorName + colorNameSuffix;
var components = colors[i]["actualComponents"];
var color = { };
switch (colorSpace)
{
case "CMYK":
color["C"] = (roundComponents) ? Math.round (components[0]) : components[0];
color["M"] = (roundComponents) ? Math.round (components[1]) : components[1];
color["Y"] = (roundComponents) ? Math.round (components[2]) : components[2];
color["K"] = (roundComponents) ? Math.round (components[3]) : components[3];
break;
case "Lab":
color["L"] = (roundComponents) ? Math.round (components[0]) : components[0];
color["a"] = components[1];
color["b"] = components[2];
break;
case "RGB":
color["R"] = components[0];
color["G"] = components[1];
color["B"] = components[2];
break;
}
bookColor["color"] = color;
bookColors.push (bookColor);
}
}
colorBookColors["bookColors"] = bookColors;
}
return colorBookColors;
};
} ());
}
//------------------------------------------------------------------------------