There's one trick we use at work, and now I'm using in my current medieval village building game project, which apparently isn't as well-known as I th

Transforming colors with matrices | lisyarus blog

submited by
Style Pass
2024-10-10 16:30:04

There's one trick we use at work, and now I'm using in my current medieval village building game project, which apparently isn't as well-known as I thought: transforming colors using matrices, interpreting colors as 3D RGB or 4D RGBA vectors. In this article I'll try to explain how it works and which operations on colors can be represented this way.

I mean, if you're a graphics programmer, you've probably worked with colors before, and you probably never needed to apply matrices to colors, unless you're doing something hardcore like transforming to CIE XYZ or Oklab color spaces. You probably needed to compute lighting, apply some post-processing and tone-mapping, - operations which are almost never linear. Why matrices?

At work, we're rendering stylized maps, and we allow the user to have a fair amount of customization. In particular, the user can decide to e.g. roads darker, make buildings more saturated, and shift the hue of forests into the yellow region. All these are simple operations, but they are vastly different in implementation. It would be a nightmare to support a dozen uniform parameters in the shader just to implement all these operations.

However, we can represent all these operations as matrices! Now composition of these is trivial (matrix-matrix multiplication), and applying them in the shader is also trivial (matrix-vector multiplication). Matrices are simple, efficient, and easy to reason about, so it's a total win-win.

Leave a Comment