Last active
July 2, 2025 22:50
-
-
Save philipprochazka/6ed2f4d6048055f8165e8b39aad40a7f to your computer and use it in GitHub Desktop.
extract svg or css colors to a markdown colorwriteout
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
import re | |
import sys | |
from pathlib import Path | |
# Regex patterns for color formats | |
HEX_COLOR_RE = re.compile(r'#(?:[0-9a-fA-F]{3,4}){1,2}') | |
RGB_COLOR_RE = re.compile(r'rgb(?:a)?\(\s*[\d\s,\.%]+\)') | |
HSL_COLOR_RE = re.compile(r'hsl(?:a)?\(\s*[\d\s,\.%]+\)') | |
NAMED_COLOR_RE = re.compile(r':\s*([a-zA-Z]+)\s*;') | |
CSS_VAR_RE = re.compile(r'--([\w-]+)\s*:\s*([^;]+);') | |
def extract_colors_from_text(text): | |
"""Extracts all colors from the input text.""" | |
colors = set() | |
colors.update(HEX_COLOR_RE.findall(text)) | |
colors.update(RGB_COLOR_RE.findall(text)) | |
colors.update(HSL_COLOR_RE.findall(text)) | |
# Named colors (less reliable, but sometimes useful) | |
colors.update([m.group(1) for m in NAMED_COLOR_RE.finditer(text) if m.group(1) not in ('none',)]) | |
return colors | |
def extract_css_vars(text): | |
"""Extracts CSS variable assignments (name, value).""" | |
return CSS_VAR_RE.findall(text) | |
def parse_svg_colors(svg_text): | |
"""Extracts color codes from SVG fill/stroke/styles.""" | |
style_colors = extract_colors_from_text(svg_text) | |
# Also try extracting colors from <style> tags or attributes | |
return style_colors | |
def parse_css_colors(css_text): | |
"""Extracts color codes and CSS variables from CSS file.""" | |
color_set = extract_colors_from_text(css_text) | |
css_vars = extract_css_vars(css_text) | |
return color_set, css_vars | |
def color_md_row(color, where, varname=None): | |
"""Returns a Markdown table row for a color.""" | |
# Generate color swatch using shields.io (alternative: via.placeholder) | |
swatch = f'}?style=flat-square&labelColor={color.lstrip("#")})' | |
var_display = varname if varname else "" | |
return f'| `{color}` | {swatch} | {var_display} | {where} |' | |
def main(filepath): | |
path = Path(filepath) | |
text = path.read_text(encoding='utf-8') | |
ext = path.suffix.lower() | |
colors_found = set() | |
variables = [] | |
if ext == '.svg': | |
colors_found = parse_svg_colors(text) | |
where = "SVG" | |
elif ext == '.css': | |
colors_found, css_vars = parse_css_colors(text) | |
variables.extend(css_vars) | |
where = "CSS" | |
else: | |
print("Supported file types: .svg, .css") | |
sys.exit(1) | |
# Start Markdown output | |
md_lines = [ | |
"# Color Palette Extracted", | |
"", | |
"Colors extracted from file:", | |
f"**{path.name}**", | |
"", | |
"| Color | Swatch | Variable Name | Found In |", | |
"|-------|--------|--------------|----------|" | |
] | |
for color in sorted(colors_found): | |
md_lines.append(color_md_row(color, where)) | |
# List CSS custom properties, if any | |
if variables: | |
md_lines.append("\n## CSS Variables") | |
md_lines.append("| Variable Name | Value (Color) |") | |
md_lines.append("|---------------|--------------|") | |
for varname, value in variables: | |
md_lines.append(f"| `{varname}` | `{value.strip()}` |") | |
# Append a verbose "variable = value" section | |
md_lines.append("\n### Color Variable = Value List") | |
for varname, value in variables: | |
md_lines.append(f"`{varname} = {value.strip()}`") | |
# Save as Markdown file | |
md_path = path.with_suffix('.colors.md') | |
md_path.write_text("\n".join(md_lines), encoding='utf-8') | |
print(f"Extracted color palette written to: {md_path}") | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print("Usage: python extract_colors_to_markdown.py <file.svg|file.css>") | |
sys.exit(1) | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment