View Full Version : How to Convert from RGB 255 to OpenGL Float Color
12-01-2003, 03:13 AM
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?
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?
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
12-01-2003, 03:17 AM
You can use glColor3ub instead. Or simply compute the float color as (float)ubyte_color / 255.0f.
12-01-2003, 04:28 AM
Originally posted by Tzupy:
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).
12-01-2003, 09:50 AM
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
Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.