# Help creating an ArrayList of color values

**URL:** <https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475>\
**Category:** Beginners\
**Created:** [March 7, 2020, 6:25am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475 "2020-03-07T06:25:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eyetoof](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Eyetoof](https://discourse.processing.org/u/Eyetoof)\
**Post date:** [March 7, 2020, 6:25am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/1 "2020-03-07T06:25:26Z")

</div>

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.

> **[r/processing - Alternatives to an ArrayList of color primitives](https://www.reddit.com/r/processing/comments/6xfpvo/alternatives_to_an_arraylist_of_color_primitives/)**
>
> 6 votes and 13 comments so far on Reddit

  

> <https://stackoverflow.com/questions/28863583/integer-or-int-in-processing>

Thanks in advance for any help you can provide.

- Toof

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 7, 2020, 6:51am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/2 "2020-03-07T06:51:21Z")

</div>

Okay, step 1 : make the class

```auto
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

```auto
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](https://processing.org/reference/lerpColor_.html)

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

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [March 7, 2020, 7:59am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/3 "2020-03-07T07:59:53Z")

</div>

You might like to use processing convenience implementation of an Integer list `IntList` see [javadocs](http://processing.github.io/processing-javadocs/core/) and [reference](https://processing.org/reference/IntList.html). 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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 7, 2020, 8:05am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/4 "2020-03-07T08:05:51Z")

</div>

Yeah

`ArrayList <IntList> listC = new ArrayList();`

See IntList in the reference

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [March 7, 2020, 8:52am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/5 "2020-03-07T08:52:16Z")

</div>

```java
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);
}

```

 ![palette](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8d1990b3e99b80ff1ccc3f6f5161bc612dd6db27.png)

---

<div class="post-metadata">

**Author:** ![Eyetoof](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Eyetoof](https://discourse.processing.org/u/Eyetoof)\
**Post date:** [March 7, 2020, 9:17am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/6 "2020-03-07T09:17:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Eyetoof](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Eyetoof](https://discourse.processing.org/u/Eyetoof)\
**Post date:** [March 7, 2020, 9:42pm UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/7 "2020-03-07T21:42:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [March 7, 2020, 11:08pm UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/8 "2020-03-07T23:08:27Z")

</div>

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.

> [@Chrisir](#):
>
> `ArrayList<ColorContainer> listC = new ArrayList();`

> [@Chrisir](#):
>
> listC.add(new ColorContainer(lerpColor(c1,c2,amt));

also,

> [@Eyetoof](#):
>
> I will persevere because it seems like a concept that will help me in the future.

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.  
🙂

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 8, 2020, 2:25am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/9 "2020-03-08T02:25:00Z")

</div>

Keep in mind that you also have multiple [color palette library options](https://www.processing.org/reference/libraries/), including Nice Color Palettes and ColorSheme. There is also some palette generation code in Dawsome Toolkit, I believe.

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [March 8, 2020, 7:13am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/10 "2020-03-08T07:13:08Z")

</div>

Not to mention [colorutils from the awesome toxiclibs library/collection](http://toxiclibs.org/docs/colorutils/)

---

<div class="post-metadata">

**Author:** ![Hieronymus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hieronymus/32/2018_2.png) [@Hieronymus](https://discourse.processing.org/u/Hieronymus)\
**Post date:** [March 11, 2020, 10:40am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/11 "2020-03-11T10:40:18Z")

</div>

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:

> [@Eyetoof](#):
>
> But as color is a primitive data type, it will not work in an array list.

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};

```

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [March 11, 2020, 11:02am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/12 "2020-03-11T11:02:55Z")

</div>

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

```java
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`.

---

<div class="post-metadata">

**Author:** ![Hieronymus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hieronymus/32/2018_2.png) [@Hieronymus](https://discourse.processing.org/u/Hieronymus)\
**Post date:** [March 11, 2020, 11:09am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/13 "2020-03-11T11:09:16Z")

</div>

ah, thanks! Learned something new.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 11, 2020, 7:32pm UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/14 "2020-03-11T19:32:01Z")

</div>

> [@monkstone](#):
>
> The `IntList` is an apparently not well known convenience class

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)

---

<div class="post-metadata">

**Author:** ![matthewortega](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matthewortega/32/309_2.png) [@matthewortega](https://discourse.processing.org/u/matthewortega)\
**Post date:** [March 11, 2020, 8:08pm UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/15 "2020-03-11T20:08:06Z")

</div>

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

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![Eyetoof](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Eyetoof](https://discourse.processing.org/u/Eyetoof)\
**Post date:** [March 18, 2020, 4:17am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/16 "2020-03-18T04:17:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [March 18, 2020, 6:58am UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/17 "2020-03-18T06:58:40Z")

</div>

There may well be some [excellent tutorial by Amnon Owed](https://www.creativeapplications.net/processing/working-with-toxiclibs-processing-tutorial/) but you need to join [Creative Applications Network](https://www.creativeapplications.net) to view it. Otherwise you can try and search yourself, it does seem from [javadoc](http://toxiclibs.org/docs/colorutils/) that it can do what you want. Yes toxiclibs is install-able from processing ide.

---

<div class="post-metadata">

**Author:** ![Eyetoof](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Eyetoof](https://discourse.processing.org/u/Eyetoof)\
**Post date:** [March 18, 2020, 5:10pm UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/18 "2020-03-18T17:10:22Z")

</div>

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

 ![HSB_Sub_to_Add](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c72512b816ece65d7c8ed9ad72650ed4ec6d6526.jpeg)

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.

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [March 18, 2020, 7:22pm UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/19 "2020-03-18T19:22:13Z")

</div>

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.

 ![toxiclibs](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fa5e36ef3e879eb2f45f67912b41a1ea591ae76b.png)

---

<div class="post-metadata">

**Author:** ![Eyetoof](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Eyetoof](https://discourse.processing.org/u/Eyetoof)\
**Post date:** [March 18, 2020, 7:27pm UTC](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475/20 "2020-03-18T19:27:06Z")

</div>

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

[Next page](https://discourse.processing.org/t/help-creating-an-arraylist-of-color-values/18475.md?page=2)
