biscuide revised this gist . Go to revision
1 file changed, 54 insertions
get-css-colors.py(file created)
| @@ -0,0 +1,54 @@ | |||
| 1 | + | #!/usr/bin/python | |
| 2 | + | # -*- coding: utf-8 -*- | |
| 3 | + | import sys | |
| 4 | + | import re | |
| 5 | + | import argparse | |
| 6 | + | ||
| 7 | + | def extract_colors(css_file, output_file): | |
| 8 | + | color_pattern = re.compile(r'(#(?:[0-9a-fA-F]{3}){1,2})|' | |
| 9 | + | r'(rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(,\s*\d*\.?\d+\s*)?\))') | |
| 10 | + | colors = set() | |
| 11 | + | ||
| 12 | + | with open(css_file, 'r') as file: | |
| 13 | + | for line in file: | |
| 14 | + | matches = color_pattern.finditer(line) | |
| 15 | + | for match in matches: | |
| 16 | + | color = match.group(0) | |
| 17 | + | colors.add(color) | |
| 18 | + | ||
| 19 | + | with open(output_file, 'w') as file: | |
| 20 | + | for color in colors: | |
| 21 | + | file.write(color + '\n') | |
| 22 | + | ||
| 23 | + | def replace_colors(css_file, translation_file, output_file): | |
| 24 | + | with open(translation_file, 'r') as file: | |
| 25 | + | translations = dict(line.strip().split(' -> ') for line in file) | |
| 26 | + | ||
| 27 | + | with open(css_file, 'r') as file: | |
| 28 | + | css_content = file.read() | |
| 29 | + | ||
| 30 | + | for old_color, new_color in translations.items(): | |
| 31 | + | css_content = css_content.replace(old_color, new_color) | |
| 32 | + | ||
| 33 | + | with open(output_file, 'w') as file: | |
| 34 | + | file.write(css_content) | |
| 35 | + | ||
| 36 | + | def main(): | |
| 37 | + | parser = argparse.ArgumentParser(description="CSS Color Extractor and Replacer") | |
| 38 | + | parser.add_argument("command", choices=['extract', 'replace'], help="Command to execute (extract or replace)") | |
| 39 | + | parser.add_argument("css_file", help="Path to the CSS file") | |
| 40 | + | parser.add_argument("output_file", help="Path to the output file") | |
| 41 | + | parser.add_argument("--translation_file", help="Path to the translation file for color replacement", default="") | |
| 42 | + | ||
| 43 | + | args = parser.parse_args() | |
| 44 | + | ||
| 45 | + | if args.command == 'extract': | |
| 46 | + | extract_colors(args.css_file, args.output_file) | |
| 47 | + | elif args.command == 'replace': | |
| 48 | + | if not args.translation_file: | |
| 49 | + | print("Error: Translation file required for replace command") | |
| 50 | + | return | |
| 51 | + | replace_colors(args.css_file, args.translation_file, args.output_file) | |
| 52 | + | ||
| 53 | + | if __name__ == "__main__": | |
| 54 | + | main() | |
Newer
Older