I am looking for a script that will give me a list of all the colors in an Adobe Illustrator document by there color numbers (rgb or cmyk). I have no code and have no idea how to do this, or if you even can. Can anyone please give me any information?
-
Are you looking for the colours of shapes in the .AI file or the list of colour swatches? They are not the same thing. In addition, some shapes can be of colours unused in swatches and some swatches may go unused in the document... – cybernetic.nomad Aug 09 '18 at 18:53
-
1This is a general comment since you have posted several questions about scripting Adobe Illustrator: have you checked out [Adobe's reference material](https://www.adobe.com/devnet/illustrator/scripting.html) – cybernetic.nomad Aug 09 '18 at 18:54
-
Yes i have read it but there are limited on what actions are. I am looking for colors on the art board not the swatches. – Lady_dragon4 Aug 10 '18 at 13:03
2 Answers
From SVG
This solution uses a browser to parse/extract all colors present. You should follow this steps:
- Export the illustrator document as SVG
- Create and empty html file and paste the js code (below)
- Open the file with the browser
The idea is iterating over all elements and using getComputedStyle(), then extract only values like rgb(n, n, n)
using a regex like /(.*)(rgb\([0-9, ]*\))(.*)/g
performing another loop, like this:
var doc = document.getElementsByTagName("*");
var colors = [];
for (let j = 0; j < doc.length; j++) {
var styles = window.getComputedStyle(doc[j], null)
for (let i = 0; i < styles.length; i++) {
if (typeof styles[styles[i]] !== "undefined" && styles[styles[i]].match(/rgb\([0-9, ]*\)/g)) {
let color = styles[styles[i]].replace(/(.*)(rgb\([0-9, ]*\))(.*)/g,"$2")
if (!colors.includes(color))
colors.push(color);
}
}
}
console.log(colors)
Full functional example here: https://jsfiddle.net/gwd35Lyv/ and https://jsfiddle.net/oenmL4t6/ an empty document in which you could include the svg created.
This is assuming we are using a browser, not embebed JavaScript into illustrator file.
You could also open the svg file with a browser, like this https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/AJ_Digital_Camera.svg press F12 and in the console, paste the js code in it.
From Image
Previous solution doesn't compute the colors present in images, because we have to read pixel by pixel, to do that based in this How to get a pixel's x,y coordinate color from an image?. we could do this:
var img = document.getElementById('my-image');
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
var rect = img.getBoundingClientRect();
var colors = [];
for (let y = rect.top; y <= rect.left; y++) {
for (let x = rect.left; x <= rect.right; x++) {
var pixelData = canvas.getContext('2d').getImageData(x, y, 1, 1).data;
var rgb = "rgb(" + pixelData[0] + ", " + pixelData[1] + ", " + pixelData[2] + ")"
if (!colors.includes(rgb)) {
colors.push(rgb);
}
}
}
console.log(colors)
Here an example: http://jsfiddle.net/vkq0ew9n/ The restriction is that the image should be within the same origin (local doesn't work, just http: https: etc, not file:).

- 5,072
- 2
- 25
- 37
-
I am not 100% how to change that to work for adobe illustrator. Do you know? – Lady_dragon4 Aug 09 '18 at 18:32
-
@Lady_dragon4 I've updated the answer in a way I think could be useful for you, see this https://jsfiddle.net/ba7fp6ry/ – Emeeus Aug 09 '18 at 21:42
This will loop through all the shapes in the active document and gets posts an alert with their RGB fill colours:
with (app.activeDocument) {
if (pathItems.length > 0)
{
alert(pathItems.length);
for (var g = 0 ; g < pathItems.length; g++)
{
if (pathItems[g].filled == true)
{
if (pathItems[g].fillColor.red > 200 == true && pathItems[g].fillColor.red < 210 == true && pathItems[g].fillColor.green > 200 == true && pathItems[g].fillColor.green < 210 == true && pathItems[g].fillColor.blue > 200 == true && pathItems[g].fillColor.blue < 210 == true)
{
alert('R' + pathItems[g].fillColor.red + ' G' + pathItems[g].fillColor.green + ' B' + pathItems[g].fillColor.blue);
}
}
}
}
}
Note: You document must be in RGB colour mode for this to work
The long If
line anwers your other question

- 6,100
- 3
- 18
- 31
-
This one seems to have the same problem i had with my other code. It will fine every thing over the 200. It will not stop at the cap of 210 for some reason, so it will seem to fine this that are 255 as well. – Lady_dragon4 Aug 13 '18 at 13:33