Loading...
Logo
Processing Forum
After some trial and error with the ColorGradient class I hacked this code... Im more or less getting the number of colors I want but when I change the colorCount from 7 to 10 it returns 9 colors but when I change it to 4 it works... also some other numbers work but there are too many exceptions... The " (float) colorCount + 2f" on line 29 a hack and works sometimes but not always. I tried some other variations but couldnt get it to work...

Copy code
  1. int[] colors = new int[] { 
  2.   0xFF362624, 0xFFD11B47, 0xFFD18E58, 0xFFD9C271,
  3.   0xFFBCE0C7
  4. };
  5. int[] newColors;

  6. ColorList colorRangeList;
  7. ColorList colorGradientList;

  8. public void setup() {
  9.   size(400, 300);

  10.   setupColors();
  11. }

  12. public void setupColors() {
  13.   int colorCount = 7;

  14.   ColorRange cr = new ColorRange(new ColorList(colors));

  15.   colorRangeList = cr.getColors(colorCount);
  16.   colorRangeList.sortByDistance(false);

  17.   ColorGradient cg = new ColorGradient();

  18.   for (int i = 0; i < colors.length; i++) {
  19.     cg.addColorAt((i + 1)
  20.       * (((float) colorCount + 2f) / (float) colors.length),
  21.     TColor.newARGB(colors[i]));
  22.   }

  23.   colorGradientList = cg.calcGradient();
  24. }

  25. public void draw() {
  26.   for (int i = 0; i < colors.length; i++) {
  27.     float w = (float) width / colors.length;
  28.     fill(colors[i]);
  29.     rect(i * w, 0, w, 100);
  30.   }

  31.   for (int i = 0; i < colorRangeList.size(); i++) {
  32.     float w = (float) width / colorRangeList.size();
  33.     fill(colorRangeList.get(i).toARGB());
  34.     rect(i * w, 100, w, 100);
  35.   }

  36.   for (int i = 0; i < colorGradientList.size(); i++) {
  37.     float w = (float) width / colorGradientList.size();
  38.     fill(colorGradientList.get(i).toARGB());
  39.     rect(i * w, 200, w, 100);
  40.   }
  41. }

  42. public void keyPressed() {
  43.   setupColors();
  44. }

Animation library for timeline/keyframe based tweens and more: www.ekeneijeoma.com/processing/ijeomamotion/

Replies(2)

Hi Ekene, that's because the calculation of your gradient marker positions for each color has some issues... To be honest, it was so cryptic I rewrote it so I understand it myself ;)
Copy code
  1. public void setupColors() {
  2.   int colorCount = 10;

  3.   ColorRange cr = new ColorRange(new ColorList(colors));

  4.   colorRangeList = cr.getColors(colorCount);
  5.   colorRangeList.sortByDistance(false);

  6.   ColorGradient cg = new ColorGradient();

  7.   for (int i = 0; i < colors.length; i++) {
  8.     // FIXED
  9.     float pos=i*(float)colorCount/(colors.length-1);
  10.     cg.addColorAt(pos, TColor.newARGB(colors[i]));
  11.   }
  12.   colorGradientList = cg.calcGradient();
  13. }
Seems to work now... Hth!

http://postspectacular.com | http://toxiclibs.org
Thanks for the fast reply! Hah well thats why I said it was a hack. Anyhow shouldnt the last/right most color in the returned colorlist be the same as the last color in the "colors" array?

Animation library for timeline/keyframe based tweens and more: www.ekeneijeoma.com/processing/ijeomamotion/