• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Trouble understanding how gradientStops.add() works

Engaged ,
May 13, 2016 May 13, 2016

Copy link to clipboard

Copied

Hello,

I'm just playing with gradientStops.add(), and I was having trouble getting my stops to be evenly spaced along the gradient from 0 to 100.

Here's a semi-functional code snippet:

grad = app.activeDocument.gradients.add();

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

  stop = grad.gradientStops.add();

  stop.color = swatches.color;

  grad.rampPoint = ((100 / l) * (i + 1));

  grad.midPoint = 50;

}

Where my "swatches" var contains a list of color objects.

When I run the above code, I get this:

Screenshot 2016-05-13 12.27.29.png

Why do my gradient stops start at 50% and go towards the right (exponentially?)? I feel like my math is right, but it doesn't seem to be working like how I would expect it to.

My goal is to evenly spread the gradient stops across the full width of the gradient itself, from 0 to 100.

Could anyone help kick me in the right direction?

Thanks so much in advance!


Cheers,

Micky

MgradientStopsgra

TOPICS
Scripting

Views

867

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , May 13, 2016 May 13, 2016

Your should set grad.gradientStops.rampPoint & grad.gradientStops.midPoint, not grad.

Also to notice that when gradients been added, it already has two gradientStops, so your should set the first and last one out the for loop.

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

var sw = app.activeDocument.swatchGroups[1].getAllSwatches();

var l = sw.length;

grad.gradientStops[0].rampPoint = 0;

grad.gradientStops[0].midPoint = 50;

grad.gradientStops[0].color = sw[0].color;

for (i = 1; i < l - 1; i++) {

...

Votes

Translate

Translate
Adobe
Enthusiast ,
May 13, 2016 May 13, 2016

Copy link to clipboard

Copied

Your should set grad.gradientStops.rampPoint & grad.gradientStops.midPoint, not grad.

Also to notice that when gradients been added, it already has two gradientStops, so your should set the first and last one out the for loop.

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

var sw = app.activeDocument.swatchGroups[1].getAllSwatches();

var l = sw.length;

grad.gradientStops[0].rampPoint = 0;

grad.gradientStops[0].midPoint = 50;

grad.gradientStops[0].color = sw[0].color;

for (i = 1; i < l - 1; i++) {

     stop = grad.gradientStops.add();

     stop.color = sw.color;

     grad.gradientStops.rampPoint = i / (l - 1) * 100;

     grad.gradientStops.midPoint = 50;

}

grad.gradientStops[l - 1].rampPoint = 100;

grad.gradientStops[l - 1].midPoint = 50

;

grad.gradientStops[l - 1].color = sw[l - 1].color;

Below is a image I created with a script written many years ago.

70_10365_34a13aa9adf55b7.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 14, 2016 May 14, 2016

Copy link to clipboard

Copied

Thanks to your help, I was able to finish my script pretty quickly after make those changes:

GitHub - mhulse/illy-grad: Create gradient from selected swatches in Adobe Illustrator.

Thanks again for your help today @moluapple, I really appreciate it!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 14, 2016 May 14, 2016

Copy link to clipboard

Copied

LATEST

You are welcome! And nice code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 13, 2016 May 13, 2016

Copy link to clipboard

Copied

Thank you!!!!

That totally worked!

I really appreciate the help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines