Skip to main content
Max Mugen
Inspiring
January 8, 2023
Answered

gradient radial to linear bach change

  • January 8, 2023
  • 2 replies
  • 1474 views

Again another question about scripting


 is there a way to select a bunch of gradients filled shapes and say :
switch from linear to radial ?

Instead of Having to click on each individual shape and go to the gradient panel and change from linear to radial

See attachment for example


thxxxx

This topic has been closed for replies.
Correct answer m1b

Hi @Max Mugen, I had already written this when I saw you already had a virtually equivalent answer. I will post it anyway, because a small difference is I gave it a UI, so you can choose which conversion to make. - Mark

/**
 * Convert Gradient FillColor Type.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/gradient-radial-to-linear-bach-change/m-p/13474116
 */
(function () {

    var doc = app.activeDocument,
        items = doc.selection;

    var convertToGradType = ui()
        ? GradientType.LINEAR
        : GradientType.RADIAL;

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

        var item = items[i];

        if (
            item.fillColor.typename == 'GradientColor'
            && item.fillColor.gradient.type != convertToGradType
        )
            item.fillColor.gradient = newGradFromGrad(doc, item.fillColor.gradient, convertToGradType);

    }


    function newGradFromGrad(doc, grad, convertToType) {

        if (grad.gradType === convertToType)
            return grad;

        var newGrad = doc.gradients.add();

        newGrad.type = convertToType;
        newGrad.stops = grad.stops;

        while (newGrad.gradientStops.length < grad.gradientStops.length)
            newGrad.gradientStops.add();

        for (var i = 0; i < grad.gradientStops.length; i++) {
            newGrad.gradientStops[i].color = grad.gradientStops[i].color;
            newGrad.gradientStops[i].midPoint = grad.gradientStops[i].midPoint;
            newGrad.gradientStops[i].opacity = grad.gradientStops[i].opacity;
            newGrad.gradientStops[i].rampPoint = grad.gradientStops[i].rampPoint;
        }

        return newGrad;

    };

    function ui() {

        var w = new Window("dialog { text:'', properties:{ resizeable:true } }");
        w.add('statictext',undefined,'Convert selected gradients to:');
        var radios = w.add("group {orientation:'row', alignment:['center','center']}");
        var linearRadio = radios.add('radiobutton', undefined, 'Linear');
        var radialRadio = radios.add('radiobutton', undefined, 'Radial');
        radialRadio.value = true;
        var buttons = w.add("group {orientation:'row', alignment:['right','center'], alignChildren:'right', margins:[0,23,0,0] }");
        var cancelButton = buttons.add("Button { text:'Cancel', properties:{name:'cancel'} }");
        var okButton = buttons.add("Button { text:'Convert' }");
        okButton.onClick = function () { w.close(1) };

        w.center();
        var result = w.show();

        if (result == 1)
            return linearRadio.value;

    }


})();

 

2 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
January 8, 2023

Hi @Max Mugen, I had already written this when I saw you already had a virtually equivalent answer. I will post it anyway, because a small difference is I gave it a UI, so you can choose which conversion to make. - Mark

/**
 * Convert Gradient FillColor Type.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/gradient-radial-to-linear-bach-change/m-p/13474116
 */
(function () {

    var doc = app.activeDocument,
        items = doc.selection;

    var convertToGradType = ui()
        ? GradientType.LINEAR
        : GradientType.RADIAL;

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

        var item = items[i];

        if (
            item.fillColor.typename == 'GradientColor'
            && item.fillColor.gradient.type != convertToGradType
        )
            item.fillColor.gradient = newGradFromGrad(doc, item.fillColor.gradient, convertToGradType);

    }


    function newGradFromGrad(doc, grad, convertToType) {

        if (grad.gradType === convertToType)
            return grad;

        var newGrad = doc.gradients.add();

        newGrad.type = convertToType;
        newGrad.stops = grad.stops;

        while (newGrad.gradientStops.length < grad.gradientStops.length)
            newGrad.gradientStops.add();

        for (var i = 0; i < grad.gradientStops.length; i++) {
            newGrad.gradientStops[i].color = grad.gradientStops[i].color;
            newGrad.gradientStops[i].midPoint = grad.gradientStops[i].midPoint;
            newGrad.gradientStops[i].opacity = grad.gradientStops[i].opacity;
            newGrad.gradientStops[i].rampPoint = grad.gradientStops[i].rampPoint;
        }

        return newGrad;

    };

    function ui() {

        var w = new Window("dialog { text:'', properties:{ resizeable:true } }");
        w.add('statictext',undefined,'Convert selected gradients to:');
        var radios = w.add("group {orientation:'row', alignment:['center','center']}");
        var linearRadio = radios.add('radiobutton', undefined, 'Linear');
        var radialRadio = radios.add('radiobutton', undefined, 'Radial');
        radialRadio.value = true;
        var buttons = w.add("group {orientation:'row', alignment:['right','center'], alignChildren:'right', margins:[0,23,0,0] }");
        var cancelButton = buttons.add("Button { text:'Cancel', properties:{name:'cancel'} }");
        var okButton = buttons.add("Button { text:'Convert' }");
        okButton.onClick = function () { w.close(1) };

        w.center();
        var result = w.show();

        if (result == 1)
            return linearRadio.value;

    }


})();

 

Max Mugen
Max MugenAuthor
Inspiring
January 8, 2023

Thank you for posting your solution ! it is very nice also to be able to choose (just because ^^ ) 
Elegant solution too 

Fun fact, I was editing the file from femkeblanco to transform to radial or linear, annnd it worked ! haha 
Besides that, I have no clue about the code, too many var in there !

-- maxmugen.com
Max Mugen
Max MugenAuthor
Inspiring
January 8, 2023

Also, because neither @femkeblanco or I explicitly stated the fact I will say it here: this task would be trivial if you could just set the gradient's type directly. That would have been a nice one-line of script. Oh well.


well, i'm not sure of what you are referring to here ^^' am I missing something ? 
If I draw, I'll know beforehand what kind of gradient I want to use. Some minors change in the artwork would not justify using a script here. 

But this time around, I'm finally going down the rabbit hole of making my own libraries of assets. Swatches here but it is a long road to go.  
In my libs  I find it nice to have both versions of the gradient at hand because I'll probably go over an existing artwork and try to polish it and make it pop enough for publishing. 
At this stage, re edit a lot of stuff can be tedious and every click can be a time saver. Plus the fact of happy creative accidents ? so yea I'll take both and mess around 😊 File cleaning would be next day then haha 



-- maxmugen.com
femkeblanco
Legend
January 8, 2023
/*
 * Changes gradient type, from linear to radial, of selected path items
 * Femke Blanco 08/01/2023
 */
var doc = app.activeDocument;
for (var i = 0; i < doc.selection.length; i++) {
    linearToRadial(doc.selection[i]);
}
function linearToRadial(item) {
    var g1 = item.fillColor.gradient;
    var g2 = doc.gradients.add();
    g2.type = GradientType.RADIAL;
    var l = g1.gradientStops.length;
    for (var j = 0; j < l; j++) {
        if (j == 0) {
            var stop = g2.gradientStops[0];
            initialise(stop, 0);
        } else if (j == l - 1) {
            var stop = g2.gradientStops[l - 1];
            initialise(stop, l - 1);
        } else {
            var stop = g2.gradientStops.add();
            initialise(stop, j);
        }
    }
    var gradientColor2 = new GradientColor();
    gradientColor2.gradient = g2;
    item.fillColor = gradientColor2;
    function initialise(stop, index) {
        stop.color = g1.gradientStops[j].color;
        stop.midPoint = g1.gradientStops[j].midPoint;
        stop.opacity = g1.gradientStops[j].opacity;
        stop.rampPoint = g1.gradientStops[j].rampPoint;
    }
}
m1b
Community Expert
Community Expert
January 8, 2023

Hi @femkeblanco, I wrote a script like this too (I will post it, too, only because it has a UI), and both our scripts create a swatch item for every gradient we make. Any idea how to avoid that? I fiddled for quite a while but couldn't solve it. I feel like I'm missing something obvious.

- Mark

femkeblanco
Legend
January 8, 2023

Hi @m1b.  I didn't notice the added swatches.  I presume it's because the scripts create new gradient colors, in which case I don't think there's a way around it.