Help creating an ArrayList of color values

Hello -

I am trying to create an ArrayList of color values so I can utilize the features it has for dynamically slotting values in and out at various places. But as color is a primitive data type, it will not work in an array list. I have read that the value can be “contained” within in an object. But I have not found any examples of how to make write this class in Processing. I am still quite new to OOP, and ONLY have experience with processing so far, so I am having a brain tangle trying to conceive how to do it. I have read that Java has these built in but the examples shown in Java aren’t making sense to me yet.

A bit more info, I am using a for loop to generate the series of colors I want to put in the array. SO using an object starts to seem almost like its a function… and that makes me feel more confused at this point. But I am sure it will make sense eventually.

Can someone show me how to write this in Processing?

Here are the threads I found.


Thanks in advance for any help you can provide.

  • Toof
2 Likes

Okay, step 1 : make the class

class ColorContainer {

color col1=color(0);

ColorContainer(color c_) {
   col1=c_;
} // the constructor

} // the class 

See reference class and website | tutorials | objects

Step 2 : before setup ()

ArrayList<ColorContainer> listC = new ArrayList();

Step 3

In setup make a for loop using lerpColor

for(int i=0;i<100;i++) {
float amt=map(i,0,100,0,1);// tells point between 2 colors for lerpColor 
color c1=color(255,0,0);
color c2=color(0,255,0);
listC.add(new ColorContainer(lerpColor(c1,c2,amt));
} // for

https://processing.org/reference/lerpColor_.html

Step 4
usage is fill(listC.get(3).col1); for example

3 Likes

You might like to use processing convenience implementation of an Integer list IntList see javadocs and reference. Apparently not well known? Using color to my mind is just plain confusing, it just another one of those processing convenience methods, but is well embedded.

4 Likes

Yeah

ArrayList <IntList> listC = new ArrayList();

See IntList in the reference

1 Like
IntList palette;

void setup() {
  size(200, 200);
  palette = new IntList();
  palette.append(color(255, 0, 0));
  palette.append(color(0, 255, 0));
  palette.append(color(0, 0, 255));
  println(palette);
}

void draw() {
  background(palette.get(2));
  fill(palette.get(0));
  ellipse(width / 2, height / 2, 100, 100);
}

3 Likes

WOW excellent, I just checked and saw your replies. I need to get some non screen time(sleep) now :slight_smile: but I will be on this first thing AM. Thank you so much!

2 Likes

Thank you both for your help… I dunno why I find it hard to comprehend the container… I am close but some how its confusing. I will persevere because it seems like a concept that will help me in the future.

But the Int List is pretty straight forward and has the methods i am looking for so I will probably go with that for the purpose of completing my current project.

thanks you both so much!

Monkstone your right about color being a brainmangler I am actually using HSB just to keep it simpler to cycle colors. Works but has its own issues too.

1 Like

Hello!

Regarding the container; in the for loop, after each cycle through the loop, a new color is generated. Each of those colors need to be stored someplace in order to be retrieved for use later in the program.

also,

That is my approach too. It’s not always evident what is going on behind the scenes, but it will start to make sense the more you work with it.
:slightly_smiling_face:

2 Likes

Keep in mind that you also have multiple color palette library options, including Nice Color Palettes and ColorSheme. There is also some palette generation code in Dawsome Toolkit, I believe.

1 Like

Not to mention colorutils from the awesome toxiclibs library/collection

2 Likes

Hi Eyetoof,

I am not sure if I understand your question but I recently had to work with color arrays for a color filter that I wrote in Processing. You wrote:

But in my script I have done exactly that and it is able to compare and assign color values from these lists. So for example, the Array list looks like this:

int [] colors = {#000000, #FFFFFF, #a49e9c, #0792fb, #1a8236, #2894c5, #238570, #a45b5a, #485189};
1 Like

@Hieronymus That is not too surprising, the antlr pre-processor converts your code to java as follows:-

int [] colors = {0xff000000, 0xffFFFFFF, 0xffa49e9c, 0xff0792fb, 0xff1a8236, 0xff2894c5, 0xff238570, 0xffa45b5a, 0xff485189};

I expect this of no help to @Eyetoof who wants to use ArrayList methods. The IntList is an apparently not well known convenience class that has a similar interface to the ArrayList.

2 Likes

ah, thanks! Learned something new.

Especially it’s not well known that it can be used for colors (because the name doesn’t suggest it and many don’t know colors are only int)

Here is another approach. Since color() returns an integer. We can use an ArrayList of integers, and just append with the color function.

ArrayList<Integer> colors;

void setup(){
  size(500, 500);
  
  colors = new ArrayList<Integer>();
  
  colors.add(color(255, 0, 0));
  
  fill(colors.get(0));
  rect(0, 0, 100, 100);
}
2 Likes

Hi Monkstone -

I looked at Toxilibs material. I am very new to coding and have only really used processing. Is this a library that will install to processing? Is there a way to use their algorithms for an analogous palette for example, in my code as its java?

If so how do I go about it. I have only done very basic library stuff for my college course, importing and using the video library for instance, and a bit of the midi library. I know how to import a library at least :).

Thanks
Toof

There may well be some excellent tutorial by Amnon Owed but you need to join Creative Applications Network to view it. Otherwise you can try and search yourself, it does seem from javadoc that it can do what you want. Yes toxiclibs is install-able from processing ide.

1 Like

So here is a encapsulation of what I am trying to do…

Toxic does seem to have all the color schemes, I wonder if I can get at the math in
there to use it in my system.

BTW I am red/green color blind/deficient an I am trying to make a program to confidently generate harmonious color palettes from a base color.

1 Like

I vaguely remember someone doing something in processing re colour blindness, but can’t remember where. If you do install toxiclibs there are some color-utils examples to follow (where toxi makes use of Iterator class) in recent versions of processing it needs to be explicitly imported as does the collection class see screenshot showing examples index and one running example.

1 Like

OK Cool I got it installed and I see the examples. Will dig further! Thanks!