Skip to content

Instantly share code, notes, and snippets.

@jamesacklin
Created November 21, 2024 16:25
Show Gist options
  • Save jamesacklin/b302f457d8d1fc7b3b26eac69f8a17b9 to your computer and use it in GitHub Desktop.
Save jamesacklin/b302f457d8d1fc7b3b26eac69f8a17b9 to your computer and use it in GitHub Desktop.
import colorsys
import re
def hex_to_rgb(hex_color):
"""Convert hex color to RGB values (0-1 range)"""
hex_color = hex_color.lstrip('#')
rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
return tuple(x/255 for x in rgb)
def rgb_to_hsl(rgb):
"""Convert RGB to HSL values"""
h, l, s = colorsys.rgb_to_hls(*rgb) # Note: colorsys uses HLS order
return (h, s, l) # Return in HSL order
def analyze_color_scheme(colors):
"""Analyze a color scheme and return HSL values with luminance groupings"""
color_analysis = {}
for name, hex_color in colors.items():
rgb = hex_to_rgb(hex_color)
hsl = rgb_to_hsl(rgb)
# Group by luminance ranges
luminance = hsl[2] # L value from HSL
if luminance > 0.8:
luminance_group = "very_light"
elif luminance > 0.6:
luminance_group = "light"
elif luminance > 0.4:
luminance_group = "medium"
elif luminance > 0.2:
luminance_group = "dark"
else:
luminance_group = "very_dark"
color_analysis[name] = {
"hex": hex_color,
"hsl": hsl,
"luminance_group": luminance_group
}
return color_analysis
def suggest_theme_mappings(base16_colors, theme_colors):
"""Suggest mappings between base16 colors and theme colors based on HSL properties"""
base16_analysis = analyze_color_scheme(base16_colors)
theme_analysis = analyze_color_scheme(theme_colors)
mappings = {}
# Example mapping logic (can be customized based on your needs)
for theme_name, theme_data in theme_analysis.items():
best_match = None
best_score = float('inf')
for base_name, base_data in base16_analysis.items():
# Calculate similarity score based on HSL values
h_diff = min(abs(theme_data["hsl"][0] - base_data["hsl"][0]),
1 - abs(theme_data["hsl"][0] - base_data["hsl"][0]))
s_diff = abs(theme_data["hsl"][1] - base_data["hsl"][1])
l_diff = abs(theme_data["hsl"][2] - base_data["hsl"][2])
# Weight the differences (can be adjusted)
score = (h_diff * 2) + s_diff + (l_diff * 3)
if score < best_score:
best_score = score
best_match = base_name
mappings[theme_name] = best_match
return mappings
if __name__ == "__main__":
base16_colors = {
"base00": "#282a36",
"base01": "#3a3c4e",
"base02": "#44475a",
"base03": "#6272a4",
"base04": "#62d6e8",
"base05": "#f8f8f2",
"base06": "#f8f8f2",
"base07": "#ffffff",
"base08": "#ff5555",
"base09": "#ffb86c",
"base0A": "#f1fa8c",
"base0B": "#50fa7b",
"base0C": "#8be9fd",
"base0D": "#80bfff",
"base0E": "#ff79c6",
"base0F": "#bd93f9",
}
dark_theme = {
'primaryText': '#FFFFFF',
'color': '#FFFFFF',
'secondaryText': '#B3B3B3',
'background': '#1A1818',
'secondaryBackground': '#322E2E',
'tertiaryText': '#808080',
'border': '#333333',
'secondaryBorder': '#4C4C4C',
'activeBorder': '#4C4C4C',
'positiveActionText': '#4E91F5',
'positiveBackground': '#143A5E',
'positiveBorder': '#3D567C',
'negativeActionText': '#E96A6A',
'negativeBackground': '#4B2525',
'negativeBorder': '#814444',
'darkBackground': '#4C4C4C',
'overlayBackground': '#FFFFFF',
'neutralUnreadDot': '#808080',
}
analysis = analyze_color_scheme(dark_theme)
for name, data in analysis.items():
print(f"\nColor: {name}")
print(f"Hex: {data['hex']}")
print(f"HSL: {[round(x, 3) for x in data['hsl']]}")
print(f"Luminance group: {data['luminance_group']}")
mappings = suggest_theme_mappings(base16_colors, dark_theme)
print("\nSuggested mappings:")
for theme_color, base16_color in mappings.items():
print(f"{theme_color}: '{base16_colors[base16_color]}'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment