Testing HSL colours, ideally avoiding Red adjacent to Green (common colour-blindness type)

StackOverflow https://stackoverflow.com/questions/4642219

  •  09-10-2019
  •  | 
  •  

Question

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

Was it helpful?

Solution

  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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top