Skip to content

Commit

Permalink
Add option to add colors to connector pins
Browse files Browse the repository at this point in the history
  • Loading branch information
formatc1702 committed Jul 30, 2020
1 parent cc05ef0 commit 127c720
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/wireviz/DataClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Connector:
notes: Optional[str] = None
pinlabels: List[Any] = field(default_factory=list)
pins: List[Any] = field(default_factory=list)
pincolors: List[Any] = field(default_factory=list)
color: Optional[str] = None
show_name: bool = None
show_pincount: bool = None
Expand Down Expand Up @@ -50,11 +51,15 @@ def __post_init__(self):
if len(self.pinlabels) != len(self.pins):
raise Exception('Given pins and pinlabels size mismatch')

# TODO: Check pincolor length

# create default lists for pins (sequential) and pinlabels (blank) if not specified
if not self.pins:
self.pins = list(range(1, self.pincount + 1))
if not self.pinlabels:
self.pinlabels = [''] * self.pincount
if not self.pincolors:
self.pincolors = [None] * self.pincount

if len(self.pins) != len(set(self.pins)):
raise Exception('Pins are not unique')
Expand Down
12 changes: 8 additions & 4 deletions src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from wireviz.DataClasses import Connector, Cable
from graphviz import Graph
from wireviz import wv_colors, wv_helper
from wireviz.wv_colors import get_color_hex
from wireviz.wv_colors import get_color_hex, html_color_icon
from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, \
nested_html_table, flatten2d, index_if_list, html_line_breaks, \
graphviz_line_breaks, remove_line_breaks, open_file_read, open_file_write, \
Expand Down Expand Up @@ -105,11 +105,15 @@ def create_graph(self) -> Graph:

if connector.style != 'simple':
pinlist = []
for pin, pinlabel in zip(connector.pins, connector.pinlabels):
for counter, pin, pinlabel in zip(range(0, len(connector.pins)), connector.pins, connector.pinlabels):
if connector.hide_disconnected_pins and not connector.visible_pins.get(pin, False):
continue
pinlist.append([f'<td port="p{pin}l">{pin}</td>' if connector.ports_left else None,
f'<td>{pinlabel}</td>' if pinlabel else '',
f'<td>{pinlabel}</td>' if pinlabel else None,
# f'<td>{counter}</td>', # for debugging only
f'<td sides="tbl">{connector.pincolors[counter].replace("__","")}</td>' if connector.pincolors and connector.pincolors[counter] else None,
# f'<td bgcolor="{wv_colors.translate_color(connector.pincolors[counter], "HEX")}" width="4"></td>' if connector.pincolors else None,
f'<td sides="tbr">{html_color_icon(connector.pincolors[counter])}</td>' if connector.pincolors and connector.pincolors[counter] else None,
f'<td port="p{pin}r">{pin}</td>' if connector.ports_right else None])

pinhtml = '<table border="0" cellspacing="0" cellpadding="3" cellborder="1">'
Expand Down Expand Up @@ -172,7 +176,7 @@ def create_graph(self) -> Graph:
if cable.color: # add color bar next to color info, if present
colorbar = f' bgcolor="{wv_colors.translate_color(cable.color, "HEX")}" width="4"></td>' # leave out '<td' from string to preserve any existing attributes of the <td> tag
html = html.replace('><!-- colorbar --></td>', colorbar)

wirehtml = '<table border="0" cellspacing="0" cellborder="0">' # conductor table
wirehtml = f'{wirehtml}<tr><td>&nbsp;</td></tr>'

Expand Down
9 changes: 9 additions & 0 deletions src/wireviz/wv_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,12 @@ def translate_color(input, color_mode):
return output.upper()
else:
return output.lower()


def html_color_icon(inp):
if inp == None: # no color specified
return ''
elif inp == "WH": # color matches background color
return '&#x25A1;' # square outline in default text color
else:
return f'<font color="{translate_color(inp, "HEX")}">&#x25A0;</font>' # filled square in requested color

0 comments on commit 127c720

Please sign in to comment.