Skip to content

Instantly share code, notes, and snippets.

@getflourish
Created September 23, 2014 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getflourish/b1d0e03ef1e18265b3c5 to your computer and use it in GitHub Desktop.
Save getflourish/b1d0e03ef1e18265b3c5 to your computer and use it in GitHub Desktop.
// (shift control cmd c)
function theFunction() {
var color = null,
results = 0,
referenceColor = null,
selected = null;
if (selection.count() == 1) {
// get the selected layer
selected = selection[0];
// get color of selected layer
referenceColor = getColorOf(selected);
// loop through all layers of the page
var layers = doc.currentPage().children().objectEnumerator();
while (layer = layers.nextObject()) {
// get color of layer
try {
color = getColorOf(layer);
if (areEqual(color, referenceColor)) {
// select layer if colors are equal
layer.setIsSelected(true);
results++;
}
} catch (error) {
log("error comparing colors");
}
}
doc.showMessage(results + " layers of " + doc.currentPage().children().count() + " selected. " + referenceColor);
} else {
doc.showMessage("Please select a reference layer.");
}
};
function getColorOf(_layer) {
var color = null,
fill = null,
style = null,
fills = null,
className = String(_layer.className());
switch (className) {
case "MSTextLayer":
try {
// get the text color
color = _layer.textColor();
// check if the text layer has a fill color
fill = layer.style().fills().firstObject();
if (fill != undefined && fill.isEnabled()) {
color = fill.color();
}
} catch (error) {
log(error);
}
break;
case "MSOvalShape":
case "MSShapeGroup":
case "MSShapePathLayer":
case "MSRectangleShape":
try {
// try to determin the fill type
style = _layer.style();
if (style.fills()) {
fills = style.fills();
if (fills.count() > 0) {
fill = fills.firstObject();
if (fill != null && fill.isEnabled()) {
if(fill.fillType() == 0) {
// solid color
color = fill.color();
} else {
// any other fill
color = fill;
}
}
}
}
} catch (error) {
log("could not get color");
}
break;
}
return color;
}
theFunction();
function areEqual(a, b) {
var ca = null;
var cb = null;
var hex_a = null;
var hex_b = null;
try {
ca = String(a.className());
cb = String(b.className());
log("received classnames " + ca + " / " + cb);
} catch (error) {
log("could not receive classnames");
}
try {
if (ca === "MSColor" && cb === "MSColor") {
try {
hex_a = String(a.hexValue());
} catch (error) {
log("couldn’t get hex a");
}
try {
hex_b = String(b.hexValue());
} catch (error) {
log("couldn’t get hex a");
}
if (hex_a && hex_b) {
return hex_a === hex_b;
}
} else if (ca === "MSStyleFill" && cb === "MSStyleFill") {
switch (a.fillType()) {
// pattern
case 4:
if (a.image() == b.image()) return true;
break;
// gradient
case 1:
// check if both gradients have the same number of stops
var stopsA = a.gradient().stops();
var stopsB = b.gradient().stops();
if (stopsA.count() == stopsB.count()) {
for (var i = 0; i < stopsA.count(); i++) {
if (stopsA.objectAtIndex(i).color() != stopsB.objectAtIndex(i).color() || stopsA.objectAtIndex(i).position() != stopsB.objectAtIndex(i).position()) {
return false;
}
}
return true;
}
break;
default:
log("default");
break;
}
}
} catch (error) {
log("error comparing " + a.fillType() + " / " + b.fillType());
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment