Here we have an interesting real-world algorithm requirement involving colors.
N
Pretty Colors: In order to draw a beautiful chart (i.e: pie chart) we need to pick a random set ofN
colors that are "different enough" and look good together. This could be accomplished by fixing brightness and saturation and stepping through hue in steps of360/N
.- Stable Color Assignment: given Pie_1 with sectors labelled ('A','B','C') and Pie_2 with sectors labelled ('B','C','D'), it would be nice if the color of sectors B and C are the same on both Pie_1 and Pie_2. This will help prevent confusion if sectors are removed or added to the chart over time. The label is the only stable thing.
- Allows Hard-coded colors: The algorithm should allow hardcoded label->color relationships as an input but will compute colors (according to rules 1 and 2) for the rest of the labels.
I think this algorithm, even if it looks quite ad-hoc, will be useful in more than one situation.
Any ideas?
Update: Eric is right that is impossible to guarantee the stability of colors for each label as new labels appear and disappear. But I'm happy if it is "stable enough", i.e. color changes are minimized.
I was thinking of something like:
- Every label gets a random hue value using hash(label)%360
- In order to guarantee that the generated hues are different enough, we divide the hue circle in a fixed amount of steps (i.e:
2*N
) and try to 'round' the previous hue values to the new differentiated ones. - In the case of different labels going to the same rounded hue value, we break the tie somehow and move the point somewhere else.
But this leaves aside the issue of hard-coded colors.