views:

3992

answers:

4

I have some code that generates image of a pie chart. It's a general purpose class, so any number of slices can be given as input. Now I have problem picking good colors for the slices. Is there some algorithm that is good at that?

Or maybe I should just hand-pick and list fixed colors? But how many. Maybe 10 colors and hope there will not be more than 10 slices ever? Also, which 10 colors to pick?

Colors need to follow some rules:

  • they need to look nice
  • adjacent colors should not be similar (blue next to green is a no-go)
  • pie background color is white, so white is out of option

Some algorithm manipulating with RGB values would be a preferred solution.

+3  A: 

I would pre-compile a list of about 20 colors, then start repeating with the 2nd color. This way you won't break your second rule. Also, if someone makes a pie chart with more than 20 slices, they have bigger problems. :)

Bill the Lizard
This is exactly what the web application I'm currently working on does. Creates 20, then repeats those 20 ten times to create a list of 200. However, I have a pie chart with more than 20 slices, and colors are repeating. Any suggestions?
Eric Belair
If you really need ~200 different colors you might want to consider using the Web-safe palette (or a subset). http://en.wikipedia.org/wiki/Web_colors
Bill the Lizard
+1  A: 

I found this pseudocode formula that might help. You could start with a set to seed it.

Colour Difference Formula

The following is the formula suggested by the W3C to determine the difference between two colours.

(maximum (Red value 1, Red value 2) - minimum (Red value 1, Red value 2)) + (maximum (Green value 1, Green value 2) - minimum (Green value 1, Green value 2)) + (maximum (Blue value 1, Blue value 2) - minimum (Blue value 1, Blue value 2))

The difference between the background colour and the foreground colour should be greater than 500.

Here is the source

mugafuga
+2  A: 

There is a generator here. It is intended for web design, but the colours would look great on a pie chart, too.

You could either pre-compile a list of nice colours, or examine the logic behind the generator and do something similar yourself.

Kramii
+3  A: 

If you are looking for some kind of a formula for this, I can't help. I am looking for it myself (from time to time :-).

But, if you just need a good pre-defined palette for your charts, I recommend to take a look at Color Brewer. This is a very handy online tool that helps to define a coloring scheme for all kinds of artefacts that use color to convey qualitative or quantitative information: maps, charts, etc.

Out of three "types" of palettes that this tool can generate - sequential, qualitative, and diverging - you probably need the latter, diverging...

The tool itself is hosted at http://colorbrewer.org (requires Flash player).

You can even download Excel files with RGB definitions of all the palettes.

Yarik