How to break up list of hex values?

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.

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

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

it works just fine.

What might be the cause of this?
Thank you!

Your hex values are encoded in a String.

When you split them, you get an array of Strings.

Casting an array of Strings to an int() doesn’t make sense.


What you need to do is do some additional work on the array of Strings.

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:

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.

1 Like

it works




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

3 Likes

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.

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

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.

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

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

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

1 Like