 # How to convert ofColor from RGB to HSB in 007?

#1

Hi,

I need to convert my RGB colors to HSB while I do some color interpolation and then back to RGB, I look at the ofColor class and can see a few set/get methods related to hsb but I cant work out how to use them.

Any help will be much appreciated

• rS
#2

Hello. 007 have this functions to get HSB colors

``````

float getHue () const;
float getSaturation () const;
float getBrightness () const; // brightest component
float getLightness () const; // average of the components
void getHsb(float& hue, float& saturation, float& brightness) const;

``````

First start by setting a RGB color

``````

ofColor color;
color.set(0,255,0); //Green color

``````

And then you can choose to ways of doing it.

``````

int h,s,b;
// A way
h = color.getHue();
s = color.getSaturation();
b = color.getBrightness();
// B way
color.getHsb(h,s,b);

// For checking
cout << "Hue: " << h << endl;
cout << "Saturation: " << s << endl;
cout << "Brightness: " << b << endl;

``````

Note that in the B way you are using reference operators http://www.cplusplus.com/doc/tutorial/pointers/
http://en.wikipedia.org/wiki/Reference-(C%2B%2B)

#3

I almost got it, but struggle with the input values, HSB is usually H: 0 - 360, and S, B: 0 - 100 so I got confuse there.

What value range do I need to feed getHsb() for a more straight forward approach? can you provide an example?

Thanks!

• rS
#4

You could just map them like:

``````

ofColor color;
float saturation;
brightness = color.getBrightness();
ofMap(brightness, 0, 255, 0, 100, true);

``````

I noticed that doing

``````

color.set(255, 255, 255);
color.getHsb(hue, saturation, brightness);
cout << " 1: " << hue << " " << saturation << " " << brightness << endl;

``````

returns:

``````
1: 0 0 65025

``````

which seems off, b/c the getBrightness() method returns 255 for that.