Skip to content

Instantly share code, notes, and snippets.

@alexlenail
Created May 30, 2024 12:58
Show Gist options
  • Save alexlenail/5d335f287fe9f582c43734950053bc3d to your computer and use it in GitHub Desktop.
Save alexlenail/5d335f287fe9f582c43734950053bc3d to your computer and use it in GitHub Desktop.
import colorsys
def darken_and_vivid_hex_color(hex_color, darken_factor=0.2, vivid_factor=0.2):
# Remove the hash at the start if it's there
hex_color = hex_color.lstrip('#')
# Convert hex to RGB
rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
# Normalize RGB values to the range [0, 1]
norm_rgb = [x / 255.0 for x in rgb]
# Convert RGB to HSV
hsv = colorsys.rgb_to_hsv(*norm_rgb)
# Apply darkening and vivid factors
darkened_vivid_hsv = (
hsv[0], # Keep the hue the same
min(1, hsv[1] + vivid_factor), # Increase saturation
max(0, hsv[2] * (1 - darken_factor)) # Decrease brightness
)
# Convert back to RGB
darkened_vivid_rgb = colorsys.hsv_to_rgb(*darkened_vivid_hsv)
# Convert RGB back to hex
darkened_vivid_hex = '#{:02x}{:02x}{:02x}'.format(
int(darkened_vivid_rgb[0] * 255),
int(darkened_vivid_rgb[1] * 255),
int(darkened_vivid_rgb[2] * 255)
)
return darkened_vivid_hex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment