Skip to main content
Qwertyfly___
Legend
September 24, 2015
Question

Issue recoloring gradients - only first stop changes

  • September 24, 2015
  • 1 reply
  • 633 views

Hi all,

helping someone here out with a script to remove opacity.

started as a simple script and is growing more complex each day...

I've tweaked it so it can deal with gradients.

but it is only effecting the first gradientStop.

I am sure it's just something simple I have missed but it's driving me bonkers...

Thanks in advance.

function RemoveOpacity(){ 

    // to work on selected items only set to SEL 

    // to work on whole document set to ALL 

    var work_on = "SEL"; 

    //--------------------------------------------------------- 

    function changeColor(obj,op){

        if(obj == '[SpotColor]'){ 

            obj.tint = obj.tint*op; 

        }else if(obj == '[CMYKColor]'){ 

            obj.cyan = obj.cyan*op; 

            obj.magenta = obj.magenta*op; 

            obj.yellow = obj.yellow*op; 

            obj.black = obj.black*op; 

        }else if(obj == '[RGBColor]'){ 

            obj.red = obj.red+(255-obj.red)*op; 

            obj.blue = obj.blue+(255-obj.blue)*op; 

            obj.green = obj.green+(255-obj.green)*op; 

        }

    }

    var doc = app.activeDocument;   

    if(work_on === 'SEL'){ 

        var items = doc.selection; 

    }else if(work_on === 'ALL'){ 

        var items = doc.pathItems; 

    }else{ 

        alert('the "work_on" variable is not set correctly'); 

        return; 

    } 

    for(var i = 0; i<items.length;i++){

        if(items.filled === true){

            if(items.fillColor == '[GradientColor]'){

                for(var j = 0; j<items.fillColor.gradient.gradientStops.length;j++){

                    changeColor(items.fillColor.gradient.gradientStops.color,(items.fillColor.gradient.gradientStops.opacity/100)*(items.opacity/100));

                    items.fillColor.gradient.gradientStops.opacity = 100;

                }

            }else{

                changeColor(items.fillColor,items.opacity/100);

            }

        }

        if(items.stroked === true){

            if(items.strokeColor == '[GradientColor]'){

                for(var j = 0; j<items.strokeColor.gradient.gradientStops.length;j++){

                    changeColor(items.strokeColor.gradient.gradientStops.color,(items.strokeColor.gradient.gradientStops.opacity/100)*(items.opacity/100));

                    items.strokeColor.gradient.gradientStops.opacity = 100;

                }

            }else{

                changeColor(items.strokeColor,items.opacity/100);

            }

        }

        items.opacity = 100;   

    } 

RemoveOpacity();

This topic has been closed for replies.

1 reply

Silly-V
Legend
September 24, 2015

Odd stuff, I tried this, it only affected one stop. Then, I put an alert box into the j loop on a line above changeColor, and it alerted twice for my 2-stop gradient, and both stops were affected.  Now, every time I run it, both stops are affected to the point that I'm starting to disbelieve myself that I even witnessed the first result correctly.

Qwertyfly___
Legend
September 24, 2015

not fair.

I have alerts all the way through.

they tell me it's all working exactly as I want it to, but it does not change.

very frustrating...

Silly-V
Legend
September 24, 2015

Just tested on my Mac 2015, it works perfect on 1st try.