Converting RGB values in 0-255 format to values in 0.0f-1.0f format

Obviously simple conversion from int to float won’t work (different ranges). How can I go about it?



//edit: Nevermind, got it.

Just for anyone else searching for an answer, simple maths:



[java]

// a number between 0-255

double x = 127.5;

// newX is 0.5 as it is halfway between 0-255

float newX = (1/255)*x;

[/java]

@MatthewHenley, simpler:



[java]int x = 127;

float newX = x/255;[/java]



Don’t know if it makes a difference CPU-wise.

@memonick



If it works, I guess CPU wise would have a few less instructions with your method, the reason I did it that way was because I’m not sure how it would react if x was equal to 0, at a guess it shouldn’t throw an error where 255/0 would. Just not at a java compiler to test :slight_smile:

You guys need to actually test that code before posting it because the integer math will destroy your results.



int x = 127;

float newX = x / 255



newX == 0



You’d have to turn one of the operands into a float in order to get a float divide.

float newX = x / 255f;



…for example. And x = 0 is not a consideration here if the math is done right. 0 / 255f == 0

6 Likes

I’ve wrote after “getting this” two helper functions that will convert RGB and RGBA values into OpenGL format. Put it in some class and you’ll be fine - you can do whatever you want with it:

[java]

public static float[] RGBToOpenGL(int r1, int g1, int b1) {

float[] tmp = new float[3];

tmp[0] = r1/255f;

tmp[1] = g1/255f;

tmp[2] = b1/255f;

return tmp;

}

public static float[] RGBAToOpenGL(int r1, int g1, int b1, int a1) {

float[] tmp = new float[4];

tmp[0] = r1/255f;

tmp[1] = g1/255f;

tmp[2] = b1/255f;

tmp[3] = a1/255f;

return tmp;

}[/java]

@pspeed said:
You'd have to turn one of the operands into a float in order to get a float divide.
float newX = x / 255f;

...for example. And x = 0 is not a consideration here if the math is done right. 0 / 255f == 0


This.

Corrected:

@Sploreg said:
ah so the divide did handle that. I'm so used to java not playing nice with that.
Ignore my previous post then


Java will automatically up cast to the highest precision to make an operation work. So, of the two operands, the highest precision operand is used.

Some operators like & and | will always promote smaller values to int but will do the operation on long if one of the operands is long. Basically, bit-wise operators do not work on anything smaller than int.

Floats are overkill here - after all “standard” RGB values are integers from 0-255 range. Now I think of it, even int is overkill here, byte would be sufficient.

@Sploreg said:
Corrected:


It was fine before, though. Only one of the operands needs to be float for the operation to be done in float.

ah so the divide did handle that. I’m so used to java not playing nice with that.

Ignore my previous post then

Y’all can just multiply by (1/255) also (0.0039215…).

@androlo said:
Y'all can just multiply by (1/255) also (0.0039215...).


1/255f... or you'll get 0.
1 Like
@pspeed said:
1/255f... or you'll get 0.

xD ..your kids will get F grades adding f's to all numbers that are supposed to work with fractions ^^ "Listen, you gotta write like in GLSL here, just use the '.0'" -- "Okay dad." ^^
1 Like

i bet they play with legos :slight_smile:

@wezrule said:
i bet they play with legos ^_^


Of course they do but that's just good parenting. My 6 yr old daughter does spend a lot of time on Mythruna at the second desk/computer in my office, though.