Randomizing a color with fixed lightness

47 views Asked by At

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!

1

There are 1 answers

0
Rosa Gronchi On

The simplest although not most accurate way of doing that would be to randomly choose R, G & B such that sqrt(R^2+G^2+B^2) == L (probably by drawing R from a normal distribution with mean of L*sqrt(3)/3, and G from a distribution with a mean of (L^2-R^2) / 2 (B is now sqrt(L^2 - R^2 - G^2))