Monday, February 28, 2011

OpenGL and Colours


Often times, in OpenGL, I tend to think about tinting colours.  Doing colour arithmetic.  Then the painfully obvious realization came to me.

I normally treat an RGBA vector as a point in 4-space.  Then why don't I normally define colour transformations through matrix operations?  Making a monochrome image becomes re-orienting (transforming) the space of colours to that of a single line running from black to white.

Even wikipedia has the matrices to go from RGBA to YUV space.  Continuing along that logic, I could build matrices that take in an image in RGBA and increase the Y component of it.

Even further along, if I use 5x5 homogeneous transformation matrices then the values can be shifted.  So blacks can become red...  Actually, this is very nicely done using SIMD operations (128-bit vectors, I think processors sporting the new 256-bit vectors are out or will soon be out.  I pray my code is compiler friendly enough to not need to explicitly worry about the issue).

Rotations do as they are expected.  For example, rotating from red to blue would continually swap the colours.  Potentially a very elegant way to manipulate colour.

Normally, interpolating of values between transformation matrices can be a bad idea.  The vectors within the matrix represent the visible space.  (imagine a 3x3 matrix.  When drawn, the resulting area corresponds to how the matrix will alter points passed to it.  Swapping the x-y coordinates is as simple as swapping the first and second rows of a 3x3 matrix when using column vectors.)  Interpolating them can cause a collapse (go to vector 0) of the space.

Obviously, there are OpenGL extensions to support this.  Some of it in software (writing the shader code or the software code to do such transforms is trivial).  I just thought of putting this out as glColor alone is very restricted compared to what can be done with matrices.  Especially since all intuitions from linear transformations in space can be applied to colours.

No comments:

Post a Comment