Menu

1 / 2

OKLab vs RGB: Why Your Color Matching Algorithm is Wrong

DEV Community·BMBrick·6 months ago
#OS74MGcU
Reading 0:00
15s threshold

The Problem with RGB

If you've ever tried to find the "closest" color from a limited palette, you probably used Euclidean distance in RGB space:

function rgbDistance ( c1 , c2 ) { return Math . sqrt ( ( c1 . r - c2 . r ) ** 2 + ( c1 . g - c2 . g ) ** 2 + ( c1 . b - c2 . b ) ** 2 ); }

Enter fullscreen mode

Exit fullscreen mode

This looks reasonable. It's fast, it's simple, and it's completely wrong.

Why RGB Distance Lies

RGB is not a perceptually uniform color space. A distance of 50 units between two reds looks very different from a distance of 50 units between two blues. The human eye is much more sensitive to green variations than blue variations, but RGB treats them equally.

The result: algorithms that pick "mathematically closest" colors that look obviously wrong to humans. Green skin tones. Muddy hair. Flat shadows.

Enter OKLab

OKLab was created by Björn Ottosson in 2020.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.