I want to randomize a number of colors, all having the same lightness (but different purdy colors). Now there's a nice colorspace like LAB
that allows you to do that, but it gets a bit tricky when converting back to RGB as not all colors that you can pick in LAB
space can be represented in the normal RGB
.
I've decided to take the following approach (poor pseudocode, never mind the ranges and exact values):
L = some fixed value
while r, g or b not in range 0.0 to 1.0:
a = random value
b = random value
r, g, b = LABtoRGB(L, a, b)
Now this tends to require only 1 or 2 iterations for L=50
(because RGB
can represent a large fraction of colors for that lightness), but I don't like this solution.
Surely there's a better way? Cheers!
The simplest although not most accurate way of doing that would be to randomly choose
R, G & B
such thatsqrt(R^2+G^2+B^2) == L
(probably by drawingR
from a normal distribution with mean ofL*sqrt(3)/3
, andG
from a distribution with a mean of(L^2-R^2) / 2
(B
is nowsqrt(L^2 - R^2 - G^2)
)