Handle also int colors in colon separated string

Bug: 0x112233:0x445566 in YAML input didn't convert such colors
to #112233:#445566 and the strings where just passed as uppercase
to the .gv file. Hence Graphviz printed warnings about unknown
colors and used black as color instead.

Add test for int as string. Re-ordered if statements to give an
exception when a color has an unknown type.
This commit is contained in:
KV 2023-09-16 15:26:18 +02:00
parent f122278b07
commit ca7b134e1e

View File

@ -88,14 +88,21 @@ class SingleColor:
elif isinstance(inp, int): elif isinstance(inp, int):
hex_str = f"#{inp:06x}" hex_str = f"#{inp:06x}"
self._html = hex_str self._html = hex_str
self._code_en = hex_str # do not perform reverse lookup self._code_en = hex_str # do not perform reverse lookup - why not?
elif inp.upper() in known_colors.keys(): elif not isinstance(inp, str):
raise Exception(f"Unknown single color {inp}!")
else:
inp_upper = inp.upper() inp_upper = inp.upper()
self._code_en = inp_upper if inp_upper in known_colors.keys():
self._html = known_colors[inp_upper].html self._code_en = inp_upper
else: # assume it's a valid HTML color name self._html = known_colors[inp_upper].html
self._html = inp else:
self._code_en = inp try: # Maybe inp is an int as string?
inp = f"#{int(inp, 0):06x}"
except ValueError:
pass # assume it's a valid HTML color name
self._html = inp
self._code_en = inp
@property @property
def html_padded(self): def html_padded(self):
@ -129,25 +136,25 @@ class MultiColor:
pass pass
elif isinstance(item, SingleColor): elif isinstance(item, SingleColor):
self.colors.append(item) self.colors.append(item)
else: # string else: # string or integer (type check done inside)
self.colors.append(SingleColor(item)) self.colors.append(SingleColor(item))
elif isinstance(inp, SingleColor): # single color elif isinstance(inp, SingleColor): # single color
self.colors = [inp] self.colors = [inp]
else: # split input into list elif isinstance(inp, int):
if ":" in str(inp): self.colors = [SingleColor(inp)]
self.colors = [SingleColor(item) for item in inp.split(":")] elif not isinstance(inp, str):
else: raise Exception(f"Unknown multi-color {inp}!")
if isinstance(inp, int): elif ":" in inp: # split input into list
self.colors = [SingleColor(inp)] self.colors = [SingleColor(item) for item in inp.split(":")]
elif len(inp) % 2 == 0: else:
items = [inp[i : i + 2] for i in range(0, len(inp), 2)] if len(inp) % 2 == 0:
known = [item.upper() in known_colors.keys() for item in items] items = [inp[i : i + 2] for i in range(0, len(inp), 2)]
if all(known): known = [item.upper() in known_colors.keys() for item in items]
self.colors = [SingleColor(item) for item in items] if all(known):
else: # assume it's a valud HTML color name self.colors = [SingleColor(item) for item in items]
self.colors = [SingleColor(inp)] return
else: # assume it's a valid HTML color name # assume it's a valid HTML color name
self.colors = [SingleColor(inp)] self.colors = [SingleColor(inp)]
def __len__(self): def __len__(self):
return len(self.colors) return len(self.colors)