diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | LICENSE | 21 | ||||
| -rwxr-xr-x | colour-negate.py | 49 |
3 files changed, 54 insertions, 18 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb176dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.mypy_cache/ +__pycache__/ @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Timo Wilken + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/colour-negate.py b/colour-negate.py index 6ab7820..b7f4fe3 100755 --- a/colour-negate.py +++ b/colour-negate.py @@ -1,43 +1,55 @@ -#! /bin/env python3 +#!/usr/bin/env python3 -import sys, os, re +'''Negate HTML colours in files. + +This is useful for converting light SVG icon themes to dark ones or vice versa. + +Currently only handles grayscale colours. +''' + +import sys +import os +import re from enum import Enum class Direction(Enum): - both = 'both' - light_to_dark = 'ltd' - dark_to_light = 'dtl' + '''Which colours to invert.''' + both = 'both' # invert all colours + light_to_dark = 'ltd' # invert light colours only + dark_to_light = 'dtl' # invert dark colours only def should_negate_colour(grayscale, negate_mode): - if direction == Direction.both: + '''Whether a grayscale colour should be negated according to negate_mode.''' + if negate_mode == Direction.both: return True - elif direction == Direction.dark_to_light: + if negate_mode == Direction.dark_to_light: return grayscale < 0x7F - elif direction == Direction.light_to_dark: + if negate_mode == Direction.light_to_dark: return grayscale > 0x7F raise ValueError('negate_mode = {!r}, but must be member of enum Direction' .format(negate_mode)) def negate_colour(colour, direction=Direction.both): + '''Return colour negated or not, according to direction.''' colour = colour.strip() - m = re.fullmatch('#([0-9a-fA-F]{2}){3}', colour) - if m: - grayscale = int(m.group(1), 16) + match = re.fullmatch('#([0-9a-fA-F]{2}){3}', colour) + if match: + grayscale = int(match.group(1), 16) if should_negate_colour(grayscale, direction): negated = '#' + 3 * hex(0xFF - grayscale).replace('0x', '') print('\tnegated %s to %s' % (colour, negated)) return negated - else: - print('\tskipping ignored colour: %s' % colour) + print('\tskipping ignored colour: %s' % colour) else: print('\tskipping non-colour "%s"' % colour) return colour -def process_file(contents, direction=DIR_BOTH): +def process_file(contents, direction=Direction.both): + '''Replace colours in contents with their negations (in given direction).''' hash_replace = '<<HASH>>' match = True while match: @@ -50,7 +62,8 @@ def process_file(contents, direction=DIR_BOTH): return contents.replace(hash_replace, '#') -def process_directory(dir_path): +def process_directory(dir_path, direction=Direction.both): + '''Invert colours in all files in the given directory.''' for name, dirs, files in os.walk(dir_path): print(name, dirs, files, sep='\n', end='\n\n') for fname in files: @@ -58,17 +71,17 @@ def process_directory(dir_path): print('processing: %s' % fname) with open(fname, 'r') as read_file: contents = read_file.read() - new_contents = process_file(contents, DIR_DARK_TO_LIGHT) + new_contents = process_file(contents, direction) with open(fname, 'w') as write_file: write_file.write(new_contents) def main(): + '''Main entry point.''' process_subdirs = sys.argv[1:] if len(sys.argv) > 1 else ['actions'] for subdir in process_subdirs: - process_directory(subdir) + process_directory(subdir, Direction.dark_to_light) if __name__ == '__main__': main() - |
