dimar has asked for the wisdom of the Perl Monks concerning the following question:

Elise teaches elementary school students learning social studies. She has asked me write a quick application that will help the students make up flags for their own newly-declared sovereign nations.

The problem is this, some of the students want to design flags with many different regions, and they want the computer to randomly choose the colors for the regions. Moreover, the 'randomly' colored regions should be 'reasonably distinguishable' (for example, if the flag has two regions, the two 'randomly' chosen colors should not be 'yellow' and 'lightyellow'

Assuming I am working with, say for example, the list of named colors available in your typical web browser, is there a way to choose (n) colors such that all (n) are 'reasonably distinguishable' from each other?

No, this is not a homework question, but if you are incredulous I am more than happy to accept a vague outline of how to approach this problem, or a citation to a website, or a S suggestion for TFW, or a M to go FR.

Replies are listed 'Best First'.
Re: Choosing Colors for Custom Flags
by Corion (Patriarch) on Jul 17, 2004 at 19:48 UTC

    Choosing colours to be "reasonably" distinguishable can be done by representing the colours on a colour wheel and using the angle between the colours as a measure of distance. Google gives me this page which might be a start, and there is also Graphics::ColorObject, which has the HSV (Hue, Saturation, Value), which should have the hue as the angle.

    The n high-contrast values for the colour c are then the colours located at the angles c + k * 360/n for k between 0 and n - 1. This will obviously only work for a small number of "contrasting colours", as the segments become too small soon to be still regarded as contrasting if you allow arbitrary ordering of the colours.

    If you only want local contrast as opposed to global contrast, then things become harder, as you now have to offer colours that have high contrast to all touching colours, and you'll have to find out which flag regions are touching the current flag region to restrict your colour selection offers.

    Update: chromatic points out that the V in HSV stands for "Value".

Re: Choosing Colors for Custom Flags
by gaal (Parson) on Jul 17, 2004 at 19:45 UTC
    Color schemes is a nice website for choosing colors that go together well. Read the About text, it contains some helpful hints.

    Googling for "color scheme" chooser brings up other sites, and some of them contain more ideas, or code.

    (Update: Ah, I remember now why I liked that first site enough to remmeber it. It lets you see what people with abnormal color vision -- of different sorts! -- see when they look at the combinations you choose.

    Totally off topic, but I have to recommend "The Case of the Colorblind Painter" now to everyone. It's from the book An Anthropologist on Mars by Oliver Sacks.)
Re: Choosing Colors for Custom Flags
by Old_Gray_Bear (Bishop) on Jul 17, 2004 at 20:05 UTC
    Visit the notions of Heraldry (or at least British Heraldric practice). There are two groups of colours -- the Tinctures (red, green, blue, black, and purple) and the Metals (gold/yellow and silver/white). Adjacent fields have to belong to opposing groups. A flag consisting of three stripes could be colored red/white/red or red/yellow/blue, but not red/black/white. This method gives the best contrast between fields on a flag or a shield. Throw in some additional "neutral" combinations like checky (chequer board pattern of a tincture and a metal), and a few simple figures (vertical stripe, horizontal stripe, left and right diagonal stripes and a chevron), and you are set.

    ----
    I Go Back to Sleep, Now.

    OGB

      So Germany is breaking the rules then? They have three stripes, black, red, and gold, with the black and the red touching. Also Afghanistan has three stripes, black, red, and green, with the black and the red touching. Albania has a red field with a black two-headed eagle in the centre. Angola's flag also has a red stripe adjacent to a black stripe. Also, Armenia has red next to blue, Burkina Faso and Cameroon have red next to green... Here, take a look. All those nations breaking the rules of heraldry, it shouldn't be allowed!
Re: Choosing Colors for Custom Flags
by aquarium (Curate) on Jul 18, 2004 at 01:18 UTC
    a simpler to implement idea: give them about 20 different flag designs to choose from, which have the different shaped/orientated regions pre-defined....then give them a pallette of 10 contrasting colors to use....and when a color is used, it is taken off the pallette, so it can't be used again. This is somewhat restrictive in terms of design, but straightforward to design the prog.
Re: Choosing Colors for Custom Flags
by qq (Hermit) on Jul 18, 2004 at 12:58 UTC
    they want the computer to randomly choose the colors for the regions

    Really? Children don't want to choose their own colors? Give them a restricted pallette and let them do the choosing - it must be half the fun, and will "teach" them about color contrast.

    qq

Re: Choosing Colors for Custom Flags
by techy (Scribe) on Jul 18, 2004 at 14:26 UTC