views:

40

answers:

0

I am using color picking in opengl es on android and i am calculating a color key to compare it to the values i get from glReadPixels:

ByteBuffer PixelBuffer = ByteBuffer.allocateDirect(4);
PixelBuffer.order(ByteOrder.nativeOrder());
gl.glReadPixels(x, y, 1, 1, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, PixelBuffer);
byte b[] = new byte[4];
PixelBuffer.get(b);
String key = "" + b[0] + b[1] + b[2];

This key can be calculated manually for any color with:

public static byte floatToByteValue(float f) {
    return (byte) ((int) (f * 255f));
}

First the float value is converted to an intvalue and then castet to byte. The float values describe the colorchanels red green blue (from 0.0f to 1.0f). Example: 0.0f is converted to 255 (now integer) and then from 255 to -1 in byte

This works fine but opengl seems to make rounding errors some times. Example:

0.895 -> -28  and opengl returns -27
0.897 -> -28  and opengl returns -27
0.898 -> -28  and opengl returns -27
0.8985 -> -27 and opengl returns -27
0.899 -> -27  and opengl returns -26
0.9 -> -27    and opengl returns -26
0.91 -> -24   and opengl returns -24

maybe my calculation method isnt correct? Does anyone have an idea how to avoid these deviations?