We are working on Abode Illustrator to create SVG files, we are trying to create javaScripts to list all the color codes coded in the SVG file.
And also trying to get the count of each color coded in each objects in the SVG.
We tried using the solution provided in the below link provided by @Emeeus. Link: Illustrator script to find all colors in document
We opened the SVG file in a empty web page and ran the below code in the Console:
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)
It does give you the list of color codes used in the file. But there are more 2 requirements we need:
- The color code is in RBG form, we require the same in HEX code.
- The count of each color code is not extracted in the output.
Getting out using the above code: Result using JavaScript