Why you can't use vector calculus for color spaces
December 2025 (133 Words, 1 Minutes)
I wanted a lazy way to autogenerate thumbnails that with brightly colored text that contrasted against a colored background.
I thought:
- Colors are represented as vectors in RGB space
- Vector dot product tells you how different two vectors are
I was hyped that vector calculus was actually useful for once. I wrote a script to generate random colors and check them against a dot product threshold. You may have predicted that this doesn’t work, but it’s kind of counterintuitive!
If RGB is a vector that represents color, why wouldn’t dot product, which represents the difference between vectors, work as “difference between colors”?
It’s because RGB values represent the amount of “red/green/blue light”, which is unrelated to “how the eye perceive readability.”
Ignoring blue for now, we’ll look at examples red-green space. Below are examples:
- with small dot products: green text on red background (readable); and dark text on dark background (not readable).
- with large dot products: yellow text on dark background (readable); and two similar reds (not readable)
So RGB vectors aren’t very meaningful by themselves. The correct calculation here would be luminance, which is the perceived brightness of an RGB color, and calculated by (0.2126R + 0.7152G + 0.0722*B). The magic constants are derived from the colors that the human eye is most sensitive to.