Skip to content

Instantly share code, notes, and snippets.

@MikeUdin
Forked from adewes/generate_random_color.py
Created August 30, 2022 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeUdin/083c96bf5140350fd75290efe8453ec8 to your computer and use it in GitHub Desktop.
Save MikeUdin/083c96bf5140350fd75290efe8453ec8 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment