How to repeat a gradient multiple times in Illustrator and Photoshop?


Question

How to repeat a gradient 'n' times, for example, repeating black to white gradient 5 times (along the path of stroke) like I've done manually in below example image.

Is there a way to automate it to multiply 'n' times, like 50 or 100, without manually copying the gradient slider?

Gradient repeated multiple times manually

1
12
7/22/2016 4:54:00 PM

Use scripting!

As others have answered, you should use scripting. But some of the other solutions here only use RGB, whereas mine uses colours you choose from your document. Also some solutions didn't produce even colour at the wraparound point, or had too many and/or overlapping gradient stops, so my script addresses those issues.

To use it, select 2 or more paths that are filled with colours intended for the gradient, then when prompted enter the number of times to repeat the gradient.

http://pastie.org/10924009

Edit: pastie site not working, so I've included the code below:

// select two paths, then run this script
if (app.activeDocument.selection.length < 2) {

    alert("Please select two or more paths with fills.");

} else {

    var cycles = Number(prompt ("Repeat the gradient how many times?")) || 5;  
    var myselection = app.activeDocument.selection;
    var colors = [];

    for (var i = 0; i < myselection.length; i++) {
        var newColor = myselection[i].fillColor;
        colors.push(newColor);
    }

    var stops = colors.length * cycles - 1; // “stops” does not include default 2 stops
    var interval = 100 / (cycles * colors.length); // ... the distance between stops

    var newGradient = app.activeDocument.gradients.add();  

    newGradient.type = GradientType.LINEAR;     // asymmetric, for 3 or more colours
    //newGradient.type = GradientType.RADIAL;   // symetric, for 3 or more colours

    //  the default 2 gradient stops (at beginning and end)
    //  should be the same colour, so that the gradient smoothly wraps around:
    newGradient.gradientStops[0].color = colors[0]; 
    newGradient.gradientStops[1].color = colors[0]; 

    // now add stops between beginning and end stops:
    for ( i = 1; i <= stops; i++ ) {

        var thisStop = newGradient.gradientStops.add();
        thisStop.rampPoint = i * interval;
        thisStop.color = colors[i % colors.length];

    }

    // to get a even result, the first and last rampPoints cannot be 0 and 100:
    newGradient.gradientStops[0].rampPoint = 0.1;
    newGradient.gradientStops[stops + 1].rampPoint = 99.9;
}

Example 1: black and white, repeats 6 times, CMYK document:

example 1

Example 2: 3 colour gradient, 6 repeats:

example 2

Example 3: RGB doc, 6 colours, 20 repetitions. Notice how the filled paths are overlapping? That stacking order (front to back) determines the order of colours in the gradient.

example 3

Changing colours in the gradient: Select a path with the gradient applied, then choose the Swatches Panel fly-out menu → Add selected colors. New global swatches will be added to the swatches panel, and when you edit one, it is updated everywhere it appears.

example 4

7
9/3/2016 2:26:00 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
^