Created
May 27, 2021 23:46
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import random | |
from colormath.color_objects import LabColor, sRGBColor | |
from colormath.color_conversions import convert_color | |
from colormath.color_diff import delta_e_cie2000 | |
print("<style>body { font-size: 24pt; font-family: monospace; }</style>"); | |
def random_srgb(): | |
return sRGBColor( | |
rgb_r=random.randint(0, 255), | |
rgb_g=random.randint(0, 255), | |
rgb_b=random.randint(0, 255), | |
is_upscaled = True) | |
def distance(a, b): | |
lab_a = convert_color(a, LabColor) | |
lab_b = convert_color(b, LabColor) | |
return delta_e_cie2000(lab_a, lab_b) | |
white = sRGBColor(rgb_r=1, rgb_g=1, rgb_b=1) | |
black = sRGBColor(rgb_r=0, rgb_g=0, rgb_b=0) | |
def output(c): | |
text_color = 'white' if distance(white, c) > distance(black, c) else 'black' | |
print("<div style='background-color: {0}; color: {1};'>{0}</div>".format(c.get_rgb_hex(), text_color), flush=True) | |
colors = [random_srgb()] | |
output(colors[0]) | |
num_colors = 20 | |
num_candidates = 100 | |
while len(colors) < num_colors: | |
candidates = [] | |
for _ in range(num_candidates): | |
candidate = random_srgb() | |
min_diff = min(distance(candidate, c) for c in colors) | |
candidates.append((candidate, min_diff)) | |
candidates.sort(key = lambda x: x[1]) | |
pick = candidates[-1][0] | |
output(pick) | |
colors.append(pick) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment