Try this, just hacked from various sources, so forgive any nastiness!
You'll need an image to work from, or you can generate a spectrum as you please.
This demonstrates:
the rgb (range 256)
our hsb (range 360.0, 100.0, 100.0) and java.awt.color.RGBtoHSB (range 1.0)
You can see the minor imperfections in the maths.
Code:
PImage rgb;
void setup() {
size(320,200);
rgb = loadImage("spectrum.jpg");
image(rgb,0,0,320,200);
}
void draw () {
}
void mousePressed () {
colorMode(RGB, 255);
loadPixels();
int col = pixels[mouseY*width+mouseX];
int r = (col & 0xff0000) >> 16;
int g = (col & 0x00ff00) >> 8;
int b = (col & 0x0000ff);
fill (r,g,b);
rect (20, height/2, 20, 20);
println("RGB- " + "r: " + r + " g: " + g + " b: " + b);
colorMode(HSB, 360, 100, 100);
float[] hsb = RGBtoHSB (r,g,b);
fill (hsb[0], hsb[1], hsb[2]);
rect (60, height/2, 20, 20);
println ("myHSB- " + "h: " + hsb[0] + " s: " + hsb[1] + " b: " + hsb[2]);
colorMode(HSB,1.0);
float[] jav = javaRGBtoHSB(r, g, b);
fill (jav[0], jav[1], jav[2]);
rect (100, height/2, 20, 20);
println ("JavaHSB- " + "h: " + jav[0] + " s: " + jav[1] + " b: " + jav[2]);
}
public float[] javaRGBtoHSB(int R, int G, int B) {
float[] hsb = Color.RGBtoHSB(R, G, B, null);
return hsb;
}
float[] RGBtoHSB (int r, int g, int b) {
float[] hsb = new float[3];
hsb[2] = max(max(r,g),b);
float mymin = min(min(r,g),b);
hsb[1] = (hsb[2] <= 0) ? 0 : round(100*(hsb[2] - mymin)/hsb[2]);
hsb[2] = round((hsb[2] /255)*100);
hsb[0] = 0;
if((r == g) && (g == b)){
hsb[0] = 0;
}else if(r >= g && g >= b){
hsb[0] = 60*(g-b)/(r-b);
}else if(g >= r && r >= b){
hsb[0] = 60 + 60*(g-r)/(g-b);
}else if(g >= b && b >= r){
hsb[0] = 120 + 60*(b-r)/(g-r);
}else if(b >= g && g >= r){
hsb[0] = 180 + 60*(b-g)/(b-r);
}else if(b >= r && r >= g){
hsb[0] = 240 + 60*(r-g)/(b-g);
}else if(r >= b && b >= g){
hsb[0] = 300 + 60*(r-b)/(r-g);
}else{
hsb[0] = 0;
}
hsb[0] = Math.round(hsb[0]);
return hsb;
}