Loading...
Logo
Processing Forum

colorMode() question

in Programming Questions  •  3 years ago  
Hi,
I do have a sketch which "on-the-fly" creates some colors using the HSB model, so I was using colorMode(HSB,1,1,1,1).
Other parts of the sketch, however, use the RGB model, so I switch between these two models every so often.
 
This was fine, until I started using mutliple-threads and now the various calls of colorModel() started to interfere and I had to get rid of them.
My question now is:
 
What is the best way of defining a color with known HSB values (as numbers) while being in RGB colormode, but without actually switching the colorMode() ?
Or spelled out: I do have 3 variables containing the H S and B value (ranged 0..1) of a color and I want to create a color variable of that color, but I do not want to leave the colorMode(RGB).

Replies(3)

You can use Java's own conversion to move between HSB and RGB values. For example:


import java.awt.Color;

void setup()
{
  size(400,400);
}

void draw()
{
  float hue = 0.2;
  float sat = 0.5;
  float bri = 0.8;
  background (Color.HSBtoRGB(hue, sat, bri));
}

Thanks, this will help.
Can I store a color like this in a color variable?
Copy code
  1. color mycol = Color.HSBtoRGB(hvar,svar,bvar); 
 
Can I use the alpha channel in the same way?
 
Copy code
  1. color mycol = Color.HSBtoRGB(hvar,svar,bvar,avar);
I usually do my sketches when I'm offline. Can anybody tell me where  I can download a good "offline" documentation of the relevant Java commands? Is there somewhere a "complete" documentation ready for download?
Yes you can store colour in that way. The alpha value should not be part of the HSB to RGB conversion, but you can add it in easily when you wish to do something with the colour (note also that by default the alpha value will be scaled between 0-255 when used in this way). For example,

import java.awt.Color;

void setup()
{
  size(400,400);
}

void draw()
{
  background (255);
  
  float hue = 0.2;
  float sat = 0.5;
  float bri = 0.8;
  float alf = 128;
  color myColour = Color.HSBtoRGB(hue, sat, bri);
   
  fill(myColour,alf);
  rect (20,20,100,100);
}
You can get the full Java API documentation for offline browsing at the  Java web site. See the 'Download zip' button to the right of 'Java SE 6 Documentation'.