# How to Convert from RGB 255 to OpenGL Float Color

Hi there,
I’m a newbie to Opengl and was wondering if i had a color value say 233, how do i convert that color value to opengl float color value? glColor3f(1.0, 0.0, 0.0) is how a color is set in opengl, but how do i covert say 233 to the 1.0 ro 0.0 equivalent?

James

Hello,

think of it this way: you know that you want 0 (out of 255) to map to 0 (out of 1), and you want to map 255 (out of 255) to 1 (out of 1). Its reasonable to assume that this is a linear mapping, right? 'course it is. So, what is the equation of a line?

y=mx+c

where you need to find m (the gradient) and c (the y-intercept). two unknowns, but with two equations, ie.

1=255m+c …  and
0=0m+c … 

from  we can see that c is 0. substitution c into equation , we see that m is 1/255.0. so, to answer your question, you need the formula

b/255.0 = f

where b is your byte value and f is the corresponding f value

cheers,
John

Hi,
You can use glColor3ub instead. Or simply compute the float color as (float)ubyte_color / 255.0f.

Originally posted by Tzupy:
Hi,
You can use glColor3ub instead. Or simply compute the float color as (float)ubyte_color / 255.0f.

Nitpick:

``````float out=(1.0f/255)*byte_in;
``````

This maximizes your chances that the compiler will pick the FIMUL instruction.
(1.0f/255 should be killed by constant folding, so that’s cheap).

all it is is your color in ubytes lets say 233 divided by 255 which = 0.91372549019607843137254901960784

well, 1/255.0 is an implementation detail, not an algorithmic detail (which was the point of my post…)

but its good to point these things out, yer