There was a question in this episode or episode95 where someone asked if we could just do the math of blending directly in RGB space. Casey Muratori noted that the equation for the blend in RGB space looked like the pythagoras theorem but nothing came out of it. I have tried to figure out an equation that seems to allow me to blend directly in the RGB space. Can someone tell me if I'm wrong about this?
So assuming the y=x^2 curve for gamma I have obtained that if you want to blend two values say (y1 and y2) with a ratio of t in the candela space (y-axis), then you can instead blend the corresponding x1 and x2 values by the ratio tprime
given by
tprime = (sqrt(t*(y2-y1) + y1) - x1)/(x2 - x1)
This still has a square root so it will be no more efficient than just blending in candela space, but you really can just blend in rgb space.
Here is my derivation for the above expression:
- We take the x1, x2 and corresponding y1 and y2 values on the y=x^2 curve as follows. We assume we are trying to blend the y1 and y2 by the ratio t and 1-t as shown on y axis. Furthermore, we note the corresponding ratio of
tprime
on the xaxis
- Next we get the expression of t as a fraction between the y, y1 and y2 values. And also the same kind of expression between
tprime
, x, x1 and x2 shown in boxes below.
- Now since we know that
y
is alwaysx^2
we can eliminate the x from these equations as follows:
Since we know x1
, x2
and t
, and y1
and y2
are just x1^2
and x2^2
, we can find the tprime
for any given t
, x1
and x2
. And once we know that the blend can be done as x1*(1-tprime) + x2*tprime
.
This exercise doesn't give us any computational advantage though since there is still a root to be calculated for each of the r
,g
and b
values. However, maybe it reduces the code complexity a bit by allowing us to just work with the rgb values all the time? That claim is a bit stretched really. But, am I right in saying that we indeed can do the interpolation directly in rgb space like this at least in the approximation of x^2?
P.S. I have not tried to graph the values of tprime
vs t
for different values of x1
and x2
. It looks like it will be some kind of weird surface. But, if that surface has some flat parts in the square of (0,0) to (1, 1) I wonder if we can pull some trick where we don't have to compute all the tprimes
for all the x1,x2,t
's. Maybe we can do some kind of lookup table (although this is not discrete space) to optimize on the number of computations we have to do?