Quantcast
Channel: Adobe Community : Popular Discussions - Illustrator Scripting
Viewing all articles
Browse latest Browse all 12845

SpotColor to GrayColor converter (javaScript)

$
0
0

This could be edited to change any spot color to grayscale, currently it is aimed at a color called "spot black". The reason this is better then just using "edit/edit colors/convert to grayscale" is that the tones stay the same value. I'm sure it could be improved, I'm just sharing it so if someone is having the issues I was having they might have an easier time fixing it. The script originated with CarlosCanto on this discussion http://forums.adobe.com/thread/1267562?tstart=0 and was discussed further in this discussion http://forums.adobe.com/message/5610447#5610447. Enjoy.

// script.name = grayscaleFromSpotBlack.jsx; // script.description = changes art color from a Spot swatch named "spot black", to Grayscale tones;// script.requirements = an opened document;// script.parent = CarlosCanto // 08/08/13;// script.elegant = false; // reference http://forums.adobe.com/thread/1267562?tstart=0 && http://forums.adobe.com/message/5610447#5610447 // Note: color values get rounded to the nearest integer, to avoid values like black = 99.99999999999999//              Hidden and/or Locked Objects will be ignored, as well as objects in Hidden and/or Locked Layers 
#target Illustrator 
var idoc = app.activeDocument;
var pi = idoc.pathItems;
var tf = idoc.textFrames;
var fcounter = 0;
var scounter = 0;
var gcounter = 0; //apply grayscale to paths and gradient stopsfor (j=0; j<pi.length; j++) {    var ipath = pi[j];    if (ipath.hidden==false&& ipath.locked==false) {        if (meetTheParents(ipath) == "isLayerFree"){            if (isLayerFree(ipath.parent)){                changePath(ipath);            }else{                if (isGroupFree(ipath)){                    changePath(ipath);                }            }        }    }}// choose text and changefor (t=0; t<tf.length; t++) {    var iTxtFrm = tf[t];    if (iTxtFrm.locked==false&& iTxtFrm.hidden==false) {        if (meetTheParents(iTxtFrm) == "isLayerFree"){            if (isLayerFree(iTxtFrm.parent)){                changeText(iTxtFrm);            }else{                if (isGroupFree(iTxtFrm)){                    changeText(iTxtFrm);                }            }        }    }}
alert(fcounter + ' Fill(s), ' + scounter + ' stroke(s) & ' + gcounter + ' Gradient Stop(s) processed.');
function meetTheParents(PageItem){    if (PageItem.parent.typename = "Layer"){        return"isLayerFree";    }elseif (PageItem.parent.typename = "GroupItem"){        return"isGroupFree";    }}
function isGroupFree(groupItem){    if (groupItem.parent.hidden==false&& groupItem.parent.editable==true){        if (groupItem.parent.typename == "Layer"){            var greatgpa = groupItem.parent;            isLayerFree(greatgpa);        }elseif (groupItem.parent.typename = "GroupItem"){            var gpa = groupItem.parent;            if (isGroupFree(gpa));            returntrue            }else{            returnfalse        }    }}
function isLayerFree(layer){    if (layer.locked == false&& layer.visible == true){        var gpa = layer.parent;        isLayerFree(gpa);        returntrue        }else{        returnfalse    }}// actually changes text
function changeText(textFrame){    var chars = textFrame.characters;    for (c=0; c<chars.length; c++) {        var ltr = chars[c];        if (ltr.characterAttributes.fillColor.typename == "SpotColor"){            if (ltr.characterAttributes.fillColor.spot.name == "spot black"){                var fillk = Math.round(ltr.characterAttributes.fillColor.tint);                SpotToGrey (ltr, true, false, fillk);                fcounter++;            }        }        if (ltr.characterAttributes.strokeColor.typename == "SpotColor"){            if (ltr.characterAttributes.strokeColor.spot.name == "spot black") {                var strokek = Math.round(ltr.characterAttributes.strokeColor.tint);                SpotToGrey (ltr, false, true, strokek);                scounter++;            }        }    }};//actually changes paths
function changePath(PathItem){    var fillColor = PathItem.fillColor;    if (fillColor.typename == "SpotColor") {        if (fillColor.spot.name == "spot black") {            var fillk = Math.round(fillColor.tint);            SpotToGrey (PathItem, true, false, fillk);            fcounter++;        }    }    elseif (fillColor.typename == "GradientColor") {        var stops = PathItem.fillColor.gradient.gradientStops;        for (s=0; s<stops.length; s++) {            var iStop = stops[s];            var stopColor = iStop.color;            if (stopColor.typename == "SpotColor") {                if (stopColor.spot.name == "spot black") {                    var stopk = Math.round(stopColor.tint);                    iStop.color = GrayColor;                    iStop.color.gray = stopk;                    gcounter++;                }            }        }    }    var strokeColor = PathItem.strokeColor;    if (strokeColor.typename == "SpotColor") {        if (strokeColor.spot.name == "spot black") {            var strokek = Math.round(strokeColor.tint);            SpotToGrey (PathItem, false, true, strokek);            scounter++;        }    }    elseif (strokeColor.typename == "GradientColor") {        var stops = PathItem.strokeColor.gradient.gradientStops;        for (s=0; s<stops.length; s++) {            var iStop = stops[s];            var stopColor = iStop.color;            if (stopColor.typename == "SpotColor") {                if (stopColor.spot.name == "spot black") {                    var stopk = Math.round(stopColor.tint);                    iStop.color = GrayColor;                    iStop.color.gray = stopk;                    gcounter++;                }            }        }    }}
function SpotToGrey (path, fill, stroke, k) {    if (fill) {        path.fillColor = GrayColor;        path.fillColor.gray = k;    }    if (stroke) {        path.strokeColor = GrayColor;        path.strokeColor.gray = k;    }}

A file that shows what this script does will be hosted here https://docs.google.com/file/d/0BzEoJSYDhH_WekNoYTFzVHVaOXM/edit?usp=s haring until I forget why it's saved there and delete it.


Viewing all articles
Browse latest Browse all 12845

Trending Articles