Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pick color palette for a pie-chart? [closed]

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?

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.

like image 753
codeguru Avatar asked Oct 25 '08 19:10

codeguru


People also ask

What is a closed color palette?

In an older post on this blog I already mentioned the importance of the use of this palette. It is called a "closed" palette because it leaves little or no space for mixing more colors once it is completed. An open palette is what most painters learn to use and use it freely mixing colors without any specific order.


3 Answers

I solved it as follows:

  1. Choose a base color.
  2. Calculate its hue (baseHue).
  3. Create a color with the same saturation and luminosity, with its hue calculated as:
      hue = baseHue + ((240 / pieces) * piece % 240
    

In C#:

int n = 12;

Color baseColor = System.Drawing.ColorTranslator.FromHtml("#8A56E2");
double baseHue = (new HSLColor(baseColor)).Hue;

List<Color> colors = new List<Color>();
colors.Add(baseColor);

double step = (240.0 / (double)n);

for (int i = 1; i < n; ++i)
{
    HSLColor nextColor = new HSLColor(baseColor);
    nextColor.Hue = (baseHue + step * ((double)i)) % 240.0;
    colors.Add((Color)nextColor);
}

string colors = string.Join(",", colors.Select(e => e.Name.Substring(2)).ToArray());

I used the HSLColor class.

The Google Charts example that uses 12 pieces and a base color of #8A56E2:

Chart Example

like image 137
Niels Bosma Avatar answered Oct 17 '22 11:10

Niels Bosma


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. :)

like image 21
Bill the Lizard Avatar answered Oct 17 '22 13:10

Bill the Lizard


Take a look at Color Brewer, a tool that helps to define a coloring scheme 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...

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

like image 12
Yarik Avatar answered Oct 17 '22 13:10

Yarik