I have some vectors which represent color as a three element vector in the range [0-1]. Because of the way color works in opengl these values are in error if they are outside this range. I want to design a matrix or set of matrices that can smoothly transform a color inside this unit space, so I am starting off of an identity matrix and subtly altering it (which gives me the smoothly changing part). But inevitably the values start to fall outside this range. Are there some conditions I can verify about the transform matrix that will ensure that the iterated matrix will neither transform a vector above one or below zero?
Share
This one is a bit tricky.
You’re trying to constrain your color vectors to the positive octant of a cube, centered on the origin. Here’s a sketch of the problem in 2-D:
A simple rotation matrix would quickly swing the (1,1) color vector out of the allowed “blue” zone. Not to mention that it could also push vectors out of the positive quadrant all together.
So one idea would be transforming your cube-shaped space to a spherical one, doing the transformation there, and then back-transforming. This transformation is (obviously!) nonlinear. Some ideas on how to do this can be found here: blog post.
Once you’re constraining to a sphere instead, things get a little simpler. Compute the eigenvalues of your transform matrix. Any values greater than one mean that the color vector can grow arbitrary large, so those matrices are out. All of them equal to one means that the vector magnitude will stay the same, regardless of how many times you compose the matrix. (Limited by numerical precision on your computer). All less than one means that your color vector will eventually collapse to (0,0).
Now you still need to prevent any vector component from going negative. I think you can enforce this by having all eigenvalues be non-negative.
So in summary, a positive definite matrix with an induced matrix norm of <= 1 should do the trick.
But now that I’ve said all this…. this is probably not what you want to do since this pretty complex and computationally expensive. (“There must be a better way!”). I suggest posting a new question looking for the best technique, rather than trying to hack your given technique in to working. Still… this was a fun thought exercise.