diff --git a/docs/syntax.md b/docs/syntax.md index c1392dfe..ad4d96be 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -309,6 +309,9 @@ The following colors are understood: +Unless the color also is displayed as a non-hexadecimal text in the diagram, +it is possible to specify colors as hexadecimal RGB values, e.g. `#112233`. + ## Cable color codes Supported color codes: diff --git a/src/wireviz/wv_colors.py b/src/wireviz/wv_colors.py index eb3b106e..4aa65422 100644 --- a/src/wireviz/wv_colors.py +++ b/src/wireviz/wv_colors.py @@ -107,23 +107,37 @@ color_default = '#ffffff' +_hex_digits = set('0123456789abcdefABCDEF') def get_color_hex(input, pad=False): + """Return list of hex colors from either a string of color names or :-separated hex colors.""" if input is None or input == '': return [color_default] + elif input[0] == '#': # Hex color(s) + output = input.split(':') + for i, c in enumerate(output): + if c[0] != '#' or not all(d in _hex_digits for d in c[1:]): + if c != input: + c += f' in input: {input}' + print(f'Invalid hex color: {c}') + output[i] = color_default + else: # Color name(s) + def lookup(c: str) -> str: + try: + return _color_hex[c] + except KeyError: + if c != input: + c += f' in input: {input}' + print(f'Unknown color name: {c}') + return color_default - if len(input) == 4: # give wires with EXACTLY 2 colors that striped/banded look - padded = input + input[:2] - elif pad and len(input) == 2: # hacky style fix: give single color wires a triple-up so that wires are the same size - padded = input + input + input - else: - padded = input + output = [lookup(input[i:i + 2]) for i in range(0, len(input), 2)] + + if len(output) == 2: # Give wires with EXACTLY 2 colors that striped look. + output += output[:1] + elif pad and len(output) == 1: # Hacky style fix: Give single color wires + output *= 3 # a triple-up so that wires are the same size. - try: - output = [_color_hex[padded[i:i + 2]] for i in range(0, len(padded), 2)] - except KeyError: - print(f'Unknown color specified: {input}') - output = [color_default] return output