I use color matching in opengl es on android and I compute a color key to compare it with 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 using:
public static byte floatToByteValue(float f) { return (byte) ((int) (f * 255f)); }
First, the float value is converted to intvalue, and then castet to byte. Float values ββdescribe the colors of red green blue (from 0.0f to 1.0f). Example: 0.0f is converted to 255 (now an integer), and then from 255 to -1 in bytes
This works fine, but opengl seems to make rounding errors several 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
Perhaps my calculation method is wrong? Does anyone have an idea how to avoid these deviations?
source share