Frion3L
Frion3L

Reputation: 1522

Convert hexidecimal colors to floats

This may seem a bit silly, but I can't realize how I have to enter the desired color in glColor4f. I mean, it needs 4 floats between 0.0 ~ 1.0, and all the colors I find are in hexadecimal or in rgb (which are between 0 ~ 255).

How do I have to make the conversion to adquire the desired color using glColor4f?

I have already tried doing something like this:

glColor4f(1/248, 1/50, 1/99);

But it doesn't make the trick.

I'm using JAVA and OpenGL-ES

Upvotes: 0

Views: 5768

Answers (2)

Pubudu
Pubudu

Reputation: 113

float out=(1.0f/255)*color;

this is work for the AndEngine ColorParticleModifier

Upvotes: 2

Martin Beckett
Martin Beckett

Reputation: 96109

'C' does integer arithmatic in integers

1/248 will round down to zero, you need to use floating point values. ie 1.0/248.0
But since you want to convert 8bit RGB into 0-1 you need to do(float)R/255.0

edit: in whatever language glColor4f() expects a value in the range 0-1 where 0 is black and 1 is full color. To convert a 0-255 value into the range 0-1 you have to divide by 255, not take the reciprocal.

Upvotes: 5

Related Questions