1

Inspired by the top answer on this page I wrote a python program to generate N distinct HEX colours. The difference is that the original author would generate saturation and luminance by using math.random(), whereas I use a trigonometric function which I can guarantee will always give a unique hue, saturation & luminance, whilst also providing the advantage that I could program yellow to appear darker than blue, allowing better contrast against white background & black text (what I need it for).
The code I actually use also converts HSL to HEX codes, via RGB, so that I can create web colour codes.
My questions are:-

  1. Using this model, how can I guarantee that red won't appear next to green??
  2. Generating the colour codes is easy enough, but how can I easily see them? I currently have to upload quite a large file to a server which generates pdf / png / eps before downloading it again.
  3. I can do this with testing, but does anyone have experience with using the HSL model to generate colours whose contrast is maximised against a white background with black text on top of the colours? The blues can make the black text really hard to see and the yellows sometimes hard to see against the white...

ps. This isn't actually the code I use, but it all starts here. The full python script is available here.
Cheers,
Alex

>>> class generate_HSL_colours():
...    def __init__( self, N, shift=0, degrees=360 ):
...        dict.__init__(self)
...        self.N = N
...        hues = [ angle for angle in xrange( shift, shift+degrees , degrees / N ) ] # Default hues from 0 --> 360
...        self.colours = generate_HSL_colours()
...    def generate_HSL_colours(self,angles):
...        colours = []
...        colour = HSLColour()
...        for angle in angles:
...            cos_value = math.cos( angle * math.pi / 360 ) ## <== in radians. Degrees == cos( angle/2 ) ; so cos_value goes from 1 to -1, and 0 <= angle < 180.
...            ## Could use sin_value too...
...            saturation = 90 - (cos_value * 10)  ## Saturation from 80 --> 100
...            luminance = 50 + (cos_value * 10)   ## Lightness from 60 --> 40
...            HSLColour.hue = hue
...            HSLColour.saturation = saturation
...            HSLColour.luminance = luminance
...            colours.append( HSLColour )
...        return colours
...
...    def __iter__(self):  ## I put this in to answer a different question (see below).
...      for colour in self.colours:
...          yield repr(colour.hue, colour.saturation, colour.lightness)
... 

__iter__ function written as an answer for question here

Community
  • 1
  • 1
Alex Leach
  • 1,199
  • 2
  • 12
  • 26
  • Do remember that while the majority of people with colour-blindness have red-green, there are other types of colour blindness. For example blue-yellow colour blindness and total color blindness. Be careful that you don't optimize so much for one group that it makes things worse for the other groups. – Mark Byers Jan 09 '11 at 22:32
  • true, but I work with someone who's red-green colour blind and he's gotta see the results... – Alex Leach Jan 09 '11 at 22:45

1 Answers1

1
  1. Eh? Test, and if you get red close to green you get another color? I'm not sure what the question is here.

  2. You could generate a PNG and open it locally. Thats' probably the easiest. PIL is a good library for that. http://pypi.python.org/pypi/Pillow/

  3. Nope, sorry, I don't know anything about that.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • 1. e.g. On one image, I use the same functions to generate two colour spaces. One has just 3 colours, and using hue from 0 to 360 gives (in order) red, green, blue. Green, blue then red would be fine. 2. Example or link please?? No idea how to do that... I use other people's software to make images... Thanks!! :) 3. No worries, but thanks for reading anyway :) – Alex Leach Jan 09 '11 at 22:38
  • 1. Yeah, I understand. It's just that the answer is so obvious that I assume I have misunderstood the question. If you don't want red and green close to each other, then don't put them close... :) 2. PIL is a good library. I updated the answer. – Lennart Regebro Jan 09 '11 at 22:42
  • 1. What I've done is change shift to 81, and for 3 colours that's fine. But then with more colours I can get red next to green.. I could probably cut red out altogether, but I don't want to do that. With a spectrum where the index is in the same order, I'm sure a colour-blind person could easily figure it out. But when they're next to each other, it would pose problems. – Alex Leach Jan 09 '11 at 22:44
  • I still don't understand what your question is. – Lennart Regebro Jan 09 '11 at 22:46
  • yea, PIL'll do... Thanks! Tick :) – Alex Leach Jan 09 '11 at 22:54