Created
November 1, 2021 11:26
-
-
Save sampsonbryce/14a1742f011721fe1eb4102381ae4e56 to your computer and use it in GitHub Desktop.
A python script to generate a range of colors based on a single color by adjusting the brightness and darkness of the color
This file contains hidden or 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 sys | |
import argparse | |
def hex_to_rgb(hex_color): | |
if len(hex_color) != 7: | |
raise Exception("Passed %s into hex_to_rgb(), needs to be in #87c95f format." % hex_color) | |
rgb_hex = [hex_color[x:x+2] for x in [1, 3, 5]] | |
rgb = [int(hex_value, 16) for hex_value in rgb_hex] | |
return rgb | |
def rgb_to_hex(rgb): | |
r,g,b = rgb | |
return "#{0:02x}{1:02x}{2:02x}".format(r, g, b) | |
def get_steps_below(rgb, count): | |
steps = [round((255 - num) / count) for num in rgb] | |
return steps | |
def get_steps_above(rgb, count): | |
steps = [round(num / count) for num in rgb] | |
return steps | |
def get_range_above(rgb, count): | |
steps = get_steps_above(rgb, count) | |
new_rgbs = [] | |
last_rgb = rgb | |
for _ in range(count): | |
last_rgb = [max(0, color - steps[i]) for i, color in enumerate(last_rgb)] | |
new_rgbs.append(last_rgb) | |
return new_rgbs | |
def get_range_below(rgb, count): | |
steps = get_steps_below(rgb, count) | |
new_rgbs = [] | |
last_rgb = rgb | |
for _ in range(count): | |
last_rgb = [min(255, color + steps[i]) for i, color in enumerate(last_rgb)] | |
new_rgbs.append(last_rgb) | |
return new_rgbs | |
def get_range(rgb, count_above, count_below): | |
above = [] | |
below = [] | |
if count_above: | |
above = get_range_above(rgb, count_above) | |
if count_below: | |
below = get_range_below(rgb, count_below) | |
below.reverse() | |
return below + [rgb] + above | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--hex', '-hx', required=True) | |
parser.add_argument('--colors-darker', '-cd', default=0, type=int) | |
parser.add_argument('--colors-lighter', '-cl', default=0, type=int) | |
parser.add_argument('--css-var', '-var') | |
parser.add_argument('--include-black-white', '-ibw', default=False, action='store_true') | |
args = parser.parse_args(sys.argv[1:]) | |
hex = args.hex | |
count_above = args.colors_darker | |
count_below = args.colors_lighter | |
css_var = args.css_var | |
include_black_white = args.include_black_white | |
if include_black_white: | |
color_range = get_range(hex_to_rgb(hex), count_above, count_below) | |
color_range[0] = [255, 255, 255] | |
color_range[-1] = [0, 0, 0] | |
else: | |
color_range = get_range(hex_to_rgb(hex), count_above + 1, count_below + 1) | |
color_range = color_range[1:-1] | |
for rgb in color_range: | |
r,g,b = rgb | |
print(f'{r}, {g}, {b} {rgb_to_hex(rgb)}') | |
for rgb in color_range: | |
r,g,b = rgb | |
print(f'{r}, {g}, {b}') | |
for rgb in color_range: | |
r,g,b = rgb | |
print(rgb_to_hex(rgb)) | |
print(".class {") | |
for rgb in color_range: | |
r,g,b = rgb | |
print(f" color: {rgb_to_hex(rgb)};") | |
print("}") | |
if css_var: | |
for i, rgb in enumerate(color_range): | |
r,g,b = rgb | |
print(f" --{css_var}-{i+1}00: {r}, {g}, {b}; /* {rgb_to_hex(rgb)} */") | |
print("Dark mode") | |
for i, rgb in enumerate(reversed(color_range)): | |
r,g,b = rgb | |
print(f" --{css_var}-{i+1}00: {r}, {g}, {b}; /* {rgb_to_hex(rgb)} */") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment