Using Hexidecimal colors from a table (BEGINNER)

Very basic question about how to pull hexi colors from a table. I want to have a bunch of six-character, regular ol’ hexidecimal colors in a column and use them to color my circles or lines or whatever. But it seems that adding “FF” before the color is necessary because alpha comes FIRST in hexidecimal??? This just seems wrong to me. In the super-simple code below I just create two circles from a table, one green and the other blue. And it works, but I don’t want to have to add “FF” at the front of all my hexidecimal lists. Lots of notes in the code to make it all clear:

The “SampleData” table just has two rows with X and Y positions and “ff00ff00” (green) and “ff0000ff” (blue). To repeat: I just want to have “00ff00” and “0000ff” without having to add those initial alphas. I hope I’m making sense - the code below should be very clear.

void setup() {
  size (100,200);
  fill (#ff0000); circle (50,50,50); //Draws a red circle without using the table
  // Now I want to draw two more circles, green and blue, using a table:
  Table table1 = loadTable("SampleData.csv", "header");
  for (int i = 0; i<table1.getRowCount(); i++){
  TableRow row = table1.getRow(i);
    int x = row.getInt("Xpos");
    int y = row.getInt("Ypos"); 
  //So far so good. Now to add color. The following code works when "FF" precedes the normal hexi: 
    String hs = row.getString("Hue");
    int hi = unhex(hs);
    circle (x,y,50);
 /*But that requires that I put "FF" in front of my familiar six-character hexidecimal colors
 on the data table itself, because alpha comes FIRST in hexidecimal code. 
 Why can't I just have a table with normal hexi, like "#00ff00" and then do something like:
   int c = row.getInt("Hue"); 
   fill (c);
   circle (x,y,50);

    color c = row.getInt("Hue");
    fill (c);
    circle (x,y,50);

these both just result in black circles at the moment. So: Any way to just use regular hexidecimal from a table?


1 Like

Hi @BenEvans ,

Have a look at the Bitwise Operators section of the reference, specifically OR , the | symbol. If I create a demo .csv file like this


then update a section of your code to look like this

    String hs = row.getString("Hue");
    int hi = unhex(hs);
    fill(0xff000000 | hi);
    circle (x, y, 50);

the println with hex reports the following colors:


If you don’t want to add the initial alpha to the .csv file, you can add it in the code by compositing opaque black (0xff000000 or #000000) with the source color from the table. My sketch looks like this:



1 Like

Thanks, jeremy! Basically it turns out that you just DO have to deal with the alpha coming first, and your technique of using the “|” line makes it a bit easier. Still, it is all a bit counterintuitive. It feels like if I can use “#FF0000” in regular non-table based code, I ought to be able to put exactly that same thing in a table, and then assign a variable which pulls exactly that from the table and sticks in the stroke or fill or whathaveyou. Why does “#FF0000” just return the color red, but if “#FF0000” is harvested as a string from a table it thinks the first two FFs are alpha information? Not that you have to answer, the code you provide solves the issue, but it does seem weird to me. Maybe best to stick with R, G, B values, but then I have to have three separate variables, which is a pain when there are multiple objects needing different color information!