Skip to content

Commit 55927b5

Browse files
committed
add get_highlight_colo method to better select color for highlights
1 parent 100684b commit 55927b5

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

wxmplot/imageconf.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ def __init__(self, axes=None, fig=None, canvas=None):
123123
self._yfmt = None
124124
self.set_formatters()
125125

126-
127126
def set_colormap(self, name, reverse=False, icol=0):
128127
self.cmap_reverse = reverse
129128
if reverse and not name.endswith('_r'):
@@ -150,13 +149,31 @@ def set_colormap(self, name, reverse=False, icol=0):
150149
self.image.set_cmap(curr_cmap)
151150

152151
if hasattr(self, 'highlight_areas') and hasattr(curr_cmap, '_lut'):
153-
rgb = [int(i*240)^255 for i in curr_cmap._lut[0][:3]]
154-
col = '#%02x%02x%02x' % (rgb[0], rgb[1], rgb[2])
155-
for area in self.highlight_areas:
152+
for area, mask in self.highlight_areas:
153+
col = self.get_highlight_color(mask, curr_cmap)
156154
for w in area.labelTexts:
157155
w.set_color(col)
158-
for w in area.collections:
159-
w.set_edgecolor(col)
156+
try:
157+
for w in area.collections:
158+
w.set_edgecolor(col)
159+
except AttributeError:
160+
pass
161+
area.set(edgecolor=col)
162+
163+
def get_highlight_color(self, mask, cmap):
164+
"""get color for highlight to provide decent contrast with data"""
165+
rgb = [210, 190, 50]
166+
if self.data is not None:
167+
drange = np.ptp(self.data) + 1.e-9
168+
dmask = np.where(abs(mask * self.data) > (drange*1.e-9))
169+
dcolor = cmap(self.data[dmask].mean()/(drange))
170+
rgb = [int(i*240)^255 for i in dcolor[:3]]
171+
if rgb[0] == rgb[1] and rgb[1] == rgb[2]: # greyscale
172+
rgb = [int(rgb[0]+210)%255,
173+
int(rgb[0]+190)%255,
174+
int(rgb[0]+50)%255]
175+
return '#%02x%02x%02x' % (rgb[0], rgb[1], rgb[2])
176+
160177

161178
def flip_vert(self):
162179
"flip image along vertical axis (up/down)"

0 commit comments

Comments
 (0)