Skip to content

Commit

Permalink
Support specifying hex colors for any color attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kvid committed Aug 24, 2021
1 parent e8d29b5 commit 794c400
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ The following colors are understood:
<!-- color list generated with a helper script: -->
<!-- https://gist.github.com/formatc1702/3c93fb4c5e392364899283f78672b952 -->

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`.
It is also possible to specify colors as hexadecimal RGB values, e.g. `#112233` or `#FFFF00:#009900`.
Remember quoting strings containing a `#` in the YAML file.

## Cable color codes

Expand Down
21 changes: 18 additions & 3 deletions src/wireviz/wv_colors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
from typing import Dict, List

from wireviz.DataClasses import Color, Colors

COLOR_CODES = {
'DIN': ['WH', 'BN', 'GN', 'YE', 'GY', 'PK', 'BU', 'RD', 'BK', 'VT', 'GYPK', 'RDBU', 'WHGN', 'BNGN', 'WHYE', 'YEBN',
Expand Down Expand Up @@ -109,6 +111,7 @@

_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 == '':
Expand Down Expand Up @@ -141,6 +144,18 @@ def lookup(c: str) -> str:
return output


def get_color_translation(translate: Dict[Color, str], input: Colors) -> List[str]:
"""Return list of colors translations from either a string of color names or :-separated hex colors."""
def from_hex(hex_input: str) -> str:
for color, hex in _color_hex.items():
if hex == hex_input:
return translate[color]
return f'({",".join(str(int(hex_input[i:i+2], 16)) for i in range(1, 6, 2))})'

return [from_hex(h) for h in input.lower().split(':')] if input[0] == '#' else \
[translate.get(input[i:i+2], '??') for i in range(0, len(input), 2)]


def translate_color(input, color_mode):
if input == '' or input is None:
return ''
Expand All @@ -150,11 +165,11 @@ def translate_color(input, color_mode):

color_mode = color_mode.lower()
if color_mode == 'full':
output = "/".join([_color_full[input[i:i+2]] for i in range(0,len(input),2)])
output = "/".join(get_color_translation(_color_full, input))
elif color_mode == 'hex':
output = ':'.join(get_color_hex(input, pad=False))
elif color_mode == 'ger':
output = "".join([_color_ger[input[i:i+2]] for i in range(0,len(input),2)])
output = "".join(get_color_translation(_color_ger, input))
elif color_mode == 'short':
output = input
else:
Expand Down

0 comments on commit 794c400

Please sign in to comment.