sky-chaser-high / adobe-illustrator-scripts

This is a collection of scripts for Adobe Illustrator.
MIT License
80 stars 7 forks source link

importCSVtoSwatch #3

Closed creold closed 1 year ago

creold commented 1 year ago

I suggest adding support for CSV with HEX column. Sample: https://gist.github.com/creold/a605cd81a89e9884c384df1683c20654

sky-chaser-high commented 1 year ago

Hi Sergey. Good idea. I'll add the feature.

creold commented 1 year ago

Regarding the setName(color) function. As far as I can tell, if the name is empty, Illustrator will automatically assign it in the format "C=.... M=... Y=... K=..." and "R=... G=... B=...". Or you added the condition if (color.mode == ... for safety?

sky-chaser-high commented 1 year ago

I thought the swatch name had to be set. Thanks for letting me know.

creold commented 1 year ago

In any case, it never hurts to be careful. Especially in Illustrator with its unpredictable bugs. Perhaps forming a name within the function will help avoid errors.

sky-chaser-high commented 1 year ago

Added Hex color.

creold commented 1 year ago

I also looked at how Illustrator works when the hex string in the input field is shorter than 6 characters. Illustrator adds leading zeros. To avoid errors, you can add the same behavior to the function.

function getHexColor(values) {
    var val = values[0];
    val = val.replace(/[^0-9A-F]/gi, '');
    if (val.length > 0 && val.length < 6) {
      val = ('000000' + val).slice(-6);
    }
    var colors = val.match(/../g);
    return {
        name: (values[1]) ? values[1] : val.toUpperCase(),
        red: parseInt(colors[0], 16),
        green: parseInt(colors[1], 16),
        blue: parseInt(colors[2], 16)
    };
}
sky-chaser-high commented 1 year ago

If the Hex color is 3-digit, it modified the behavior to be similar to CSS. Otherwise, it now fills in with 0.