Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A small Python script to generate random color sequences, e.g. for use in plotting. Just call the "generate_new_color(existing_colors,pastel_factor)" function to generate a random color that is (statistically) maximally different from all colors in "existing_colors". The "pastel_factor" parameter can be used to specify the "pasteliness"(?) of th…
import random
def get_random_color(pastel_factor = 0.5):
return [(x+pastel_factor)/(1.0+pastel_factor) for x in [random.uniform(0,1.0) for i in [1,2,3]]]
def color_distance(c1,c2):
return sum([abs(x[0]-x[1]) for x in zip(c1,c2)])
def generate_new_color(existing_colors,pastel_factor = 0.5):
max_distance = None
best_color = None
for i in range(0,100):
color = get_random_color(pastel_factor = pastel_factor)
if not existing_colors:
return color
best_distance = min([color_distance(color,c) for c in existing_colors])
if not max_distance or best_distance > max_distance:
max_distance = best_distance
best_color = color
return best_color
#Example:
if __name__ == '__main__':
#To make your color choice reproducible, uncomment the following line:
#random.seed(10)
colors = []
for i in range(0,10):
colors.append(generate_new_color(colors),pastel_factor = 0.9)
print "Your colors:",colors
@avrilcoghlan

This comment has been minimized.

Copy link

@avrilcoghlan avrilcoghlan commented Dec 3, 2015

Hi there is a typo,
colors.append(generate_new_color(colors),pastel_factor = 0.9)
should be
colors.append(generate_new_color(colors,pastel_factor = 0.9))

@dgergel

This comment has been minimized.

Copy link

@dgergel dgergel commented Sep 7, 2017

This works great. Thanks!

@gerardogc2378

This comment has been minimized.

Copy link

@gerardogc2378 gerardogc2378 commented Nov 7, 2017

Thanks for share this code.

@parthi2929

This comment has been minimized.

Copy link

@parthi2929 parthi2929 commented Jul 23, 2018

Strange. My ipython kernel dies right after executing this code

@Thew1965

This comment has been minimized.

Copy link

@Thew1965 Thew1965 commented Jul 24, 2019

Thanks for sharing, this is trickier than expected.

@bmeredyk

This comment has been minimized.

Copy link

@bmeredyk bmeredyk commented Sep 24, 2019

Line32 has a parenthesis in the wrong spot - should be
colors.append(generate_new_color(colors, pastel_factor = 0.9))

@lifenautjoe

This comment has been minimized.

Copy link

@lifenautjoe lifenautjoe commented Nov 29, 2019

What kind of colors does this generate and how could we convert them to hex? 🤔

@Jean-Zombie

This comment has been minimized.

Copy link

@Jean-Zombie Jean-Zombie commented Dec 14, 2019

@lifenautjoe

What kind of colors does this generate and how could we convert them to hex? 🤔

I believe you get RGB colors. I recommend python-colormath for conversion.

@giacomomarchioro

This comment has been minimized.

Copy link

@giacomomarchioro giacomomarchioro commented Mar 29, 2021

One thing to consider is that there is always the background colour of the plot which is very important a line should not be equal to!

@thomashastings

This comment has been minimized.

Copy link

@thomashastings thomashastings commented Apr 13, 2021

Hi,
This solution works really well, much appreciated. I've taken the liberty to include it in my project concerned with analyzing cardiac action potentials, credited in the readme and in comments as The color generator script has been originally written by Andreas Dewes: https://gist.github.com/adewes/5884820

With a stable release on the horizon, I would like to ask if this is okay with you.

@adewes

This comment has been minimized.

Copy link
Owner Author

@adewes adewes commented Apr 13, 2021

Hi @thomashastings, this code is public domain so you can freely reuse it, no attribution necessary!

@thomashastings

This comment has been minimized.

Copy link

@thomashastings thomashastings commented Apr 13, 2021

Thank you for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment