Support specifying hex colors for any color attributes
This commit is contained in:
parent
b3fdd48a83
commit
c34946183e
@ -399,8 +399,8 @@ The following colors are understood:
|
|||||||
<!-- color list generated with a helper script: -->
|
<!-- color list generated with a helper script: -->
|
||||||
<!-- https://gist.github.com/formatc1702/3c93fb4c5e392364899283f78672b952 -->
|
<!-- https://gist.github.com/formatc1702/3c93fb4c5e392364899283f78672b952 -->
|
||||||
|
|
||||||
Unless the color also is displayed as a non-hexadecimal text in the diagram,
|
It is also possible to specify colors as hexadecimal RGB values, e.g. `#112233` or `#FFFF00:#009900`.
|
||||||
it is possible to specify colors as hexadecimal RGB values, e.g. `#112233`.
|
Remember quoting strings containing a `#` in the YAML file.
|
||||||
|
|
||||||
## Cable color codes
|
## Cable color codes
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import sys
|
from typing import Dict, List
|
||||||
|
|
||||||
|
from wireviz.DataClasses import Color, Colors
|
||||||
|
|
||||||
COLOR_CODES = {
|
COLOR_CODES = {
|
||||||
'DIN': ['WH', 'BN', 'GN', 'YE', 'GY', 'PK', 'BU', 'RD', 'BK', 'VT', 'GYPK', 'RDBU', 'WHGN', 'BNGN', 'WHYE', 'YEBN',
|
'DIN': ['WH', 'BN', 'GN', 'YE', 'GY', 'PK', 'BU', 'RD', 'BK', 'VT', 'GYPK', 'RDBU', 'WHGN', 'BNGN', 'WHYE', 'YEBN',
|
||||||
@ -109,6 +111,7 @@ color_default = '#ffffff'
|
|||||||
|
|
||||||
_hex_digits = set('0123456789abcdefABCDEF')
|
_hex_digits = set('0123456789abcdefABCDEF')
|
||||||
|
|
||||||
|
|
||||||
def get_color_hex(input, pad=False):
|
def get_color_hex(input, pad=False):
|
||||||
"""Return list of hex colors from either a string of color names or :-separated hex colors."""
|
"""Return list of hex colors from either a string of color names or :-separated hex colors."""
|
||||||
if input is None or input == '':
|
if input is None or input == '':
|
||||||
@ -141,6 +144,18 @@ def get_color_hex(input, pad=False):
|
|||||||
return output
|
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):
|
def translate_color(input, color_mode):
|
||||||
if input == '' or input is None:
|
if input == '' or input is None:
|
||||||
return ''
|
return ''
|
||||||
@ -150,11 +165,11 @@ def translate_color(input, color_mode):
|
|||||||
|
|
||||||
color_mode = color_mode.lower()
|
color_mode = color_mode.lower()
|
||||||
if color_mode == 'full':
|
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':
|
elif color_mode == 'hex':
|
||||||
output = ':'.join(get_color_hex(input, pad=False))
|
output = ':'.join(get_color_hex(input, pad=False))
|
||||||
elif color_mode == 'ger':
|
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':
|
elif color_mode == 'short':
|
||||||
output = input
|
output = input
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user