Solving a matrix for color manipulation

204 Views Asked by At

I'm making an application that deals with color transforms. The idea is that if you give it an RGB color and apply a color matrix transform it outputs another color. In this case I'm giving the color [255,0,0] and returns [Rf,Gf,Bf]

$$ \begin{bmatrix} 1&.5&0 \\ 0&1&0 \\ 0&0&1 \end{bmatrix} \begin{bmatrix} 255\\0\\0 \end{bmatrix}= \begin{bmatrix} Rf\\Gf\\Bf \end{bmatrix} $$

This is pretty straight forward to solve.

However, how would I solve this if I don't know what the color matrix is but I know what the start and end colors are? So something like this:

$$ \begin{bmatrix} C1&C2&C3 \\ C4&C5&C6 \\ C7&C8&C9 \end{bmatrix} \begin{bmatrix} 255\\0\\0 \end{bmatrix}= \begin{bmatrix} 102\\0\\0 \end{bmatrix} $$

Thanks!

1

There are 1 best solutions below

0
On

First, it is better to use normalized color components in range $[0,1]$. Second, we need to handle black color $(0,0,0)$ as well. Here is one possible way to go: add a fourth component, $1$ for all colors. Given four pairs of RGB-colors, let $T$ be a transformation matrix, $U$ is a non-singular matrix, which columns are the original colors, and $V$ is a non-singular matrix, which columns are the transformed colors:

\begin{align} T\cdot U&=V. \end{align} Then the transformation matrix is just \begin{align} T&=V\cdot U^{-1}. \end{align}

For example, \begin{align} U&= \begin{bmatrix} 1&0&0&0 \\ 0&1&0&0 \\ 0&0&1&0 \\ 1&1&1&1 \end{bmatrix} \\ V&= \begin{bmatrix} 1&0&1&1 \\ 1&1&0&1 \\ 0&1&1&1 \\ 1&1&1&1 \end{bmatrix} \\ U^{-1}&= \begin{bmatrix} 1& 0& 0&0\\ 0& 1& 0&0\\ 0& 0& 1&0\\ -1&-1&-1&1 \end{bmatrix} \\ T=V\cdot U^{-1} &= \begin{bmatrix} 0&-1& 0&1 \\ 0& 0&-1&1 \\ -1& 0& 0&1 \\ 0& 0& 0&1 \end{bmatrix} \end{align}

And here is a sample set of colors transformed by this matrix $T$:

enter image description here