Colors don't match

Anything libgdx related goes here!

Colors don't match

Postby mmtbb » Sun Nov 28, 2010 1:03 am

I use the RGB Color(int,int,int,int)

With new Color(248, 135, 0,255);
it is supposed to create orange. However, it makes a yellow. I have to do:
Color(248/255f, 135/255f, 0,1) to make it work.

I use RGB in Photoshop and all my other programming. Is there a way to do this in libgdx?
mmtbb
 
Posts: 135
Joined: Thu Nov 11, 2010 6:56 pm

Re: Colors don't match

Postby consiliens » Sun Nov 28, 2010 2:07 am

Try com.badlogic.gdx.graphics.Color.
consiliens
 
Posts: 177
Joined: Wed Oct 27, 2010 6:55 am

Re: Colors don't match

Postby mzechner » Sun Nov 28, 2010 2:50 am

com.badlogic.gdx.graphics.Color(int, int, int, int) is not there. The arguments are floats, in the range 0-1 each. It's still rgb just not RGBA8888 as you tried to use it. The documentation of that class clearly states what ranges to use for each color component:

Code: Select all
/**
 * A color class, holding the r, g, b and alpha component as floats in the range
 * [0,1]. All methods perform clamping on the internal values after execution.
 *
 * @author mzechner
 *
 */
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm

Re: Colors don't match

Postby mmtbb » Sun Nov 28, 2010 4:03 am

Yes, that's what I read. I was hoping there was an easy method I missed.
mmtbb
 
Posts: 135
Joined: Thu Nov 11, 2010 6:56 pm

Re: Colors don't match

Postby consiliens » Sun Nov 28, 2010 4:11 am

Color.toFloatBits takes four ints. Color should probably be overloaded with an int constructor for convenience.

Any idea why this shade of green doesn't render as green in libgdx? Entering the rgb values in an image editor produces the correct color.
VertexBufferObjectShaderTest
consiliens
 
Posts: 177
Joined: Wed Oct 27, 2010 6:55 am

Re: Colors don't match

Postby mzechner » Sun Nov 28, 2010 1:12 pm

I tested and debugged this for ours now. Boils down to this:

Code: Select all
int r = 60;
int g = 235;
int b = 170;
int a = 255;
int coli = ((a << 24) & 0xff000000)| ((b << 16) & 0xff0000) | ((g << 8) & 0xff00) | (r & 0xff);
System.out.println(Integer.toHexString(coli));
float colf = Float.intBitsToFloat(coli);
System.out.println(Integer.toHexString(Float.floatToRawIntBits(colf)));


which outputs

Code: Select all
ffaaeb3c <- correct
ffeaeb3c <- wrong


That's more or less how color does it's magic. The problem is the Float.intBitsToFloat() call which should transform an integer bit pattern to a float bit pattern. Sadly, Float.intBitsToFloat() decides to rework the bit pattern for some values. That's the reason why this went undetected in the tests as we used values that didn't trigger that behaviour. You can read up on that at http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html#intBitsToFloat(int). I tried going native for the conversion via

Code: Select all
JNIEXPORT jfloat JNICALL Java_com_badlogic_gdx_utils_BufferUtils_int2float
  (JNIEnv *, jclass, jint value )
{
   return *((jfloat*)&value);
}


But the returned float is massaged before it enters the VM.

I try to fix this. I hate Java.
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm

Re: Colors don't match

Postby mzechner » Sun Nov 28, 2010 2:32 pm

Well i fixed it. Here's my commit comment:

[fixed] Color.toFloatBits() methods. Will half the precision of the alpha channel of 32-bit rgba colors. Nothing i can do about it. Problem is that Java only has a single canonical NaN value. Using Float.intBitsToFloat() will convert any signaling NaN to that canonical value, transforming colors like 0xfeaaeb3c to 0xfeeaeb3c. The fix just masks the 32-bit int via 0xfeffffff before it is passed ot Color.toFloatBits() effectively making it impossible to return canonical NaNs. This of course means that the least significant bit of the alpha component is always 0, so you only have 128 alpha values really :p. Darn Java. Darn FPUs.
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm


Return to Libgdx

Who is online

Users browsing this forum: MSN [Bot] and 1 guest