# How to break up list of hex values?

**URL:** <https://discourse.processing.org/t/how-to-break-up-list-of-hex-values/27814>\
**Category:** Coding Questions\
**Created:** [February 14, 2021, 6:37am UTC](https://discourse.processing.org/t/how-to-break-up-list-of-hex-values/27814 "2021-02-14T06:37:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)\
**Post date:** [February 14, 2021, 6:37am UTC](https://discourse.processing.org/t/how-to-break-up-list-of-hex-values/27814/1 "2021-02-14T06:37:20Z")

</div>

I’d like to break up a string of color values and then return it (so I can then use it in my stroke function).

However, when I run the following, my code will not render.

```auto
color colorDeterminer(int class) {
  color c = #FFFFFF;

String colors = "#EF8AB3,#DE91C4,#C89AD1,#AEA2DA,#90AADD,#70B1DB,#4FB6D3,#30BAC6,#21BDB5,#2EBEA1,#47BE8C,#61BC77,#7AB963,#92B552,#A9B046,#BFA93F,#D3A241,#E59949,#F39157,#FD8968";
int[] colorsSplit = int(colors.split(","));

  if (class > 700 && class < 710) {
    c = colorsSplit[1];
  }
}

```

If I run

```auto
  if (class > 700 && class < 710) {
    c = #EF8AB3;
  }

```

it works just fine.

What might be the cause of this?  
Thank you!

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [February 14, 2021, 8:05am UTC](https://discourse.processing.org/t/how-to-break-up-list-of-hex-values/27814/2 "2021-02-14T08:05:03Z")

</div>

Your hex values are encoded in a `String`.

When you split them, you get an array of `String`s.

Casting an array of `String`s to an `int()` doesn’t make sense.

* * *

What you need to do is do some additional work on the array of `String`s.

Loop over each string in turn. ("#A3B3C6")  
Drop the leading # sign using `substring()` to get a new string that is only numbers and letters. (“A3B3C6”)  
Then split that… and use `unhex()`…

* * *

Actually, you might be better off just doing:

```auto
color[] colors = {#EF8AB3,#DE91C4,#C89AD1,#AEA2DA,#90AADD,#70B1DB,#4FB6D3,#30BAC6,#21BDB5,#2EBEA1,#47BE8C,#61BC77,#7AB963,#92B552,#A9B046,#BFA93F,#D3A241,#E59949,#F39157,#FD8968"};

```

That might just work.

---

<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:** [February 14, 2021, 11:28am UTC](https://discourse.processing.org/t/how-to-break-up-list-of-hex-values/27814/3 "2021-02-14T11:28:10Z")

</div>

it works

```auto

void setup() {
  size(420, 600);
  noCursor();
  noLoop();
}

void draw() {

  // String colors = "#EF8AB3,#DE91C4,#C89AD1,#AEA2DA,#90AADD,#70B1DB,#4FB6D3,#30BAC6,#21BDB5,#2EBEA1,#47BE8C,#61BC77,#7AB963,#92B552,#A9B046,#BFA93F,#D3A241,#E59949,#F39157,#FD8968";

  color[] colors2 = { 
    #EF8AB3, #DE91C4, #C89AD1, #AEA2DA, #90AADD, #70B1DB, #4FB6D3, #30BAC6, #21BDB5, #2EBEA1, #47BE8C, #61BC77, #7AB963, #92B552, #A9B046, #BFA93F, #D3A241, #E59949, #F39157, #FD8968
  };

  for (int i=0; i < colors2.length; i++) {
    color c = color( colors2[i] );
    fill(c); 
    rect(i*20 + 12, 100, 19, 19);
  }
}

```

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [February 14, 2021, 12:39pm UTC](https://discourse.processing.org/t/how-to-break-up-list-of-hex-values/27814/4 "2021-02-14T12:39:20Z")

</div>

You are not really converting the strings to integers. I think that it would be better to use Integer.parseInt(“String”) to do this. If not, Chrisir’s approach is also good.

---

<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:** [February 14, 2021, 2:06pm UTC](https://discourse.processing.org/t/how-to-break-up-list-of-hex-values/27814/5 "2021-02-14T14:06:55Z")

</div>

> [@SNICKRS](#):
>
> Integer.parseInt(“String”) to do this

How does this work, can hex be seen as int? Is there a type hex? What’s the result, int or hex?

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [February 14, 2021, 5:24pm UTC](https://discourse.processing.org/t/how-to-break-up-list-of-hex-values/27814/6 "2021-02-14T17:24:40Z")

</div>

I don’t really think that this approach works for hexes, because those are encoded. This approach would only work for strings that have integer numbers like “8” inside. The `unhex()` function that processing provides would be good enough. However, in order to decode all 3 integers of data inside the hex code, a better approach would be to split the string into 3 hexes of rgb values, add 4 or 6 0’s to the ends in order to make a rgb color. Then, maybe an `unhex()` would work to get all 3 values. An array of colors would be better if you don’t want to go through the ardrous process of decoding the hexadecimal.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [February 14, 2021, 5:49pm UTC](https://discourse.processing.org/t/how-to-break-up-list-of-hex-values/27814/7 "2021-02-14T17:49:49Z")

</div>

here is my program for getting colors:  
enter in format:  
“RGB/HSB:red/hue:green/saturation:blue/brightness”

> **Code**
>
> ```auto
> void setup() {
> }
> void draw() {
> background(splitColorV2("HSB:"+(int)random(255)+":255:255"));
> noLoop();
> }
> void mousePressed() {loop();}
> color splitColorV2(String input) {
> String parameters[] = split(input,":");
> printArray(parameters);
> String mode = parameters[0];
> int p1 = int(parameters[1]), p2 = int(parameters[2]), p3 = int(parameters[3]),a = ((parameters.length == 5)? int(parameters[4]) : 255);
> println(parameters.length);
> if(mode == "RGB") {
> return color(p1,p2,p3,a);
> } else {
> return toRGB(p1,p2,p3,a);
> }
> }
> color toRGB(final color h, final color s, final color b, final color a) {
> pushStyle();
> colorMode(HSB);
> final color rgb = color(h, s, b, a);
> popStyle();
> return rgb;
> }
> 
> ```

edit:  
just add split(“text”,","); to split into segments.

---

<div class="post-metadata">

**Author:** ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)\
**Post date:** [February 14, 2021, 6:05pm UTC](https://discourse.processing.org/t/how-to-break-up-list-of-hex-values/27814/8 "2021-02-14T18:05:36Z")

</div>

Thanks! Yes, this works. I didn’t realize that we could just make a color list out of hex values.
