Skip to content

Instantly share code, notes, and snippets.

@iceydee
Created September 10, 2011 21:43
Show Gist options
  • Save iceydee/1208833 to your computer and use it in GitHub Desktop.
Save iceydee/1208833 to your computer and use it in GitHub Desktop.
A simple Illustrator CS5 script to 4-bit align all of the swatches. (Useful for working with RGBA4444 images). Can easily be modified to work with other types of color formats (see the alignment part of alignedColor function).
var docRef = app.activeDocument;
app.activeDocument.RGB
var alignedColor = function(color, alignment) {
if (typeof(alignment) == "undefined") {
alignment = 16;
}
var returnColor = (Math.round((color + 1) / alignment) * alignment) - 1;
if (returnColor < 0) {
return 0;
}
return returnColor;
}
var keys = function(obj) {
var results = [];
for (var property in obj) {
results.push(property);
}
return results;
};
var fixSwatchGroup = function(swatchGroup) {
for (var i = 0, len = swatchGroup.length; i < len; i++) {
var currentSwatch = swatchGroup[i];
var rgbColor = currentSwatch.color;
if (rgbColor.typename == "SpotColor") {
rgbColor = rgbColor.spot.color;
if (rgbColor.typename == "RGBColor") {
rgbColor.red = alignedColor(rgbColor.red, 16);
rgbColor.green = alignedColor(rgbColor.green, 16);
rgbColor.blue = alignedColor(rgbColor.blue, 16);
}
}
}
};
fixSwatchGroup(docRef.swatches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment