toxiclibs colorutils: turning an array of color into a gradient with X colors with
in
Contributed Library Questions
•
2 years ago
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...
- int[] colors = new int[] {
- 0xFF362624, 0xFFD11B47, 0xFFD18E58, 0xFFD9C271,
- 0xFFBCE0C7
- };
- int[] newColors;
- ColorList colorRangeList;
- ColorList colorGradientList;
- public void setup() {
- size(400, 300);
- setupColors();
- }
- public void setupColors() {
- int colorCount = 7;
- ColorRange cr = new ColorRange(new ColorList(colors));
- colorRangeList = cr.getColors(colorCount);
- colorRangeList.sortByDistance(false);
- ColorGradient cg = new ColorGradient();
- for (int i = 0; i < colors.length; i++) {
- cg.addColorAt((i + 1)
- * (((float) colorCount + 2f) / (float) colors.length),
- TColor.newARGB(colors[i]));
- }
- colorGradientList = cg.calcGradient();
- }
- public void draw() {
- for (int i = 0; i < colors.length; i++) {
- float w = (float) width / colors.length;
- fill(colors[i]);
- rect(i * w, 0, w, 100);
- }
- for (int i = 0; i < colorRangeList.size(); i++) {
- float w = (float) width / colorRangeList.size();
- fill(colorRangeList.get(i).toARGB());
- rect(i * w, 100, w, 100);
- }
- for (int i = 0; i < colorGradientList.size(); i++) {
- float w = (float) width / colorGradientList.size();
- fill(colorGradientList.get(i).toARGB());
- rect(i * w, 200, w, 100);
- }
- }
- public void keyPressed() {
- setupColors();
- }
Animation library for timeline/keyframe based tweens and more: www.ekeneijeoma.com/processing/ijeomamotion/
1