Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3670190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:20:04+00:00 2026-05-19T02:20:04+00:00

Inspired by the top answer on this page I wrote a python program to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:20:04+00:00Added an answer on May 19, 2026 at 2:20 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inspired by std::string in a multi-threaded program and another answer (seen somewhere), that STL
Inspired by the discussion in this question . We have all been taught that
Inspired by the encoding scheme of the answer to this question , I implemented
inspired by this answer I'm trying to map a property on a model class
Inspired from the other topic , I wrote this code which simulates a finally
Inspired by this question and answer , how do I create a generic permutations
Inspired by this answer , I tried next example : #include <map> #include <string>
Inspired by this question about Python caching small integers. Is it possible for the
Somewhat inspired by this question about a graphical programming environment. I don't think that
inspired by this answer , I wanted to inquire what other useful movement/editing commands

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.