home..

Why you can't use vector calculus for color spaces

code interactive

I wanted a lazy way to autogenerate thumbnails that with brightly colored text that contrasted against a colored background.

I thought:

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:

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.

© 2026 Boris Bi   •  Theme  Moonwalk