How to Generate Random Colors Programmatically

Creating random colors is actually more difficult than it seems. The randomness itself is easy, but aesthetically pleasing randomness is more difficult. For a little project at work I needed to automatically generate multiple background colors with the following properties:

  • Text over the colored background should be easily readable
  • Colors should be very distinct
  • The number of required colors is not initially known

Naïve Approach

The first and simplest approach is to create random colors by simply using a random number between [0, 256[ for the R, G, B values. I have created a little Ruby script to generate sample HTML code:

# generates HTML code for 26 background colors given R, G, B values.
def gen_html
  ('A'..'Z').each do |c|
    r, g, b = yield
    printf "#{c} ", r, g, b
  end
end

# naive approach: generate purely random colors
gen_html { [rand(256), rand(256), rand(256)] }

The generated output looks like this:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

As you can see this is quite suboptimal. Some letters are hard to read because the background is too dark (B, Q, S), other colors look very similar (F, R).

Using HSV Color Space

HSV_cylinder_smallLet's fix the too dark / too bright problem first. A convenient way to do this is to not use the RGB color space, but HSV (Hue, Saturation, Value). Here you get equally bright and colorful colors by using a fixed value for saturation and value, and just modifying the hue.

Based on the description provided by the wikipedia article on conversion from HSV to RGB I have implemented a converter:

# HSV values in [0..1[
# returns [r, g, b] values from 0 to 255
def hsv_to_rgb(h, s, v)
  h_i = (h*6).to_i
  f = h*6 - h_i
  p = v * (1 - s)
  q = v * (1 - f*s)
  t = v * (1 - (1 - f) * s)
  r, g, b = v, t, p if h_i==0
  r, g, b = q, v, p if h_i==1
  r, g, b = p, v, t if h_i==2
  r, g, b = p, q, v if h_i==3
  r, g, b = t, p, v if h_i==4
  r, g, b = v, p, q if h_i==5
  [(r*256).to_i, (g*256).to_i, (b*256).to_i]
end

Using the generator and fixed values for saturation and value: [ruby][/ruby]# using HSV with variable hue
gen_html { hsv_to_rgb(rand, 0.5, 0.95) }
returns something like this:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Much better. The text is easily readable, and all colors have a similar brightness. Unfortunately, since we have limited us to less colors now, the difference between the randomly generated colors is even less than in the first approach.

Golden Ratio

Using just rand() to choose different values for hue does not lead to a good use of the whole color spectrum, it simply is too random.

distribution-random

Here I have generated 2, 4, 8, 16, and 32 random values and printed them all on a scale. Its easy to see that some values are very tightly packed together, which we do not want.

Lo and behold, some mathematician has discovered the Golden Ratio more than 2400 years ago. It has lots of interesting properties, but for us only one is interesting:

[...] Furthermore, it is a property of the golden ratio, Φ, that each subsequent hash value divides the interval into which it falls according to the golden ratio!
-- Bruno R. Preiss, P.Eng.

Using the golden ratio as the spacing, the generated values look like this:

distribution-goldenratio

Much better! The values are very evenly distributed, regardless how many values are used. Also, the algorithm for this is extremly simple. Just add 1/Φ and modulo 1 for each subsequent color.

# use golden ratio
golden_ratio_conjugate = 0.618033988749895
h = rand # use random start value
gen_html {
  h += golden_ratio_conjugate
  h %= 1
  hsv_to_rgb(h, 0.5, 0.95)
}

The final result:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You can see that the first few values are very different, and the difference decreases as more colors are added (Z and E are already quite similar). Anyways, this is good enough for me.

And because it is so beautiful, here are some more colors ;-)
s=0.99, v=0.99, s=0.25, h=0.8, and s=0.3, v=0.99

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Have fun!
Martin

26 thoughts on “How to Generate Random Colors Programmatically”

  1. Nice explanation Martin! I have done something very similar in a ColorUtils for Java. I find it’s an interesting but very subjective matter and for many practical uses, you will want to seed the algorithm with avoidable colors.
    I attacked the problem looking at the color space as a HSV cone, placing points as far away from another in a 3D space (and often seeding the algorithm with the color black and white).

  2. @jon, nice article with calculating white vs. black text! then you have a wider range of colors to choose from. but I have not yet thought about how to select distinct colors when you can change more than just the hue parameter.

  3. Is it safe to assume that you used the golden ratio approach instead of a perfectly even distribution because it was quicker to implement and not far from ideal anyway?

    1. Any number that is near 0.65 and doesn’t isn’t a small integer fraction should do fine. For example:

      0.673467126, which my cat just selected.

      1. The requirement for the number is to make sure the values of ((n * ?) mod 1.0) do not repeat each other.
        If you use any rational number, ? = A/B ((0 < A < B), then it will give you at most B different values. This is obvious – if you substitute B*x + m (0<= m < B) for n into the formula, you will get the same value regardless of the x, so the period length is B. Bottom line: if you take an irrational ?, the period length is infinity.

        Now, there are infinitely many irrational numbers in the range [0, 1]; why should we care about Golden Ratio over any other (like sqrt(2))?

        Because it gives us the best pairwise distance between the numbers when the count of those is not known in advance.

  4. Hello Brian, no, I cannot use a perfectly even distribution because I do not know how many colors I need in advance. I simply pick colors one after the other, and do not change already picked colors. As far as I can tell the golden ratio is the optimal way for this use case.

  5. Thanks for this great resource! I’m working on a very similar problem and your approach sounds really promising.

    Just to split hairs: if you need your HSV-to-RGB converter to absolutely match what Photoshop returns, you have to allow HSV values in [0..1] —including 1— and to replace …*256 by …*255. (Of course this not compliant with the fact that the rand function generates values in [0..1[ , anyway HSV is supposed to support the full [0..1] range so this can make a very slight difference in computing high values.)
    @+
    Marc

    1. When I implemented the algorithm the article had a different description of it, and I just took it from there. I think both algorithms are the same, they first calculate R1, G1, B1 and afterwards add m to it. When you run my code with a range of numbers for h from 0 to 1, you get exactly the graph as in figure 24 on the Wikipedia page, so it should be correct

  6. Pingback: color similarity
  7. This is very informative; thanks for sharing.

    On a recent project I did something similar, but used the new CSS functions hsl() and hsla() to make it much simpler. Basically, I have this:


    var hue = 0;
    Array.prototype.slice.call(document.querySelectorAll('.myClass')).forEach(function(mc) {
    mc.style.color = 'hsla(' + hue + ', 75%, 50%, 0.5)';
    hue += 222.5;
    });

    That is, hsl() and hsla() take care of normalizing the hue to the [0, 360) range for you. 222.5 is approximately 360/?. I only had to do this bit in javascript because the set of elements I’m coloring is dynamic and I didn’t want to do the work on the server, but we’re almost to the point at which we could do even this in CSS only. (come on CSS variables!) Of course, some people prefer javascript to CSS for stuff like this…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>