|
3 | 3 | import numpy as np |
4 | 4 |
|
5 | 5 | from larray_editor.utils import (get_font, from_qvariant, to_qvariant, to_text_string, |
6 | | - is_float, is_number, SUPPORTED_FORMATS) |
| 6 | + is_float, is_number, LinearGradient, SUPPORTED_FORMATS) |
7 | 7 |
|
8 | 8 | from qtpy.QtCore import Qt, QModelIndex, QAbstractTableModel |
9 | 9 | from qtpy.QtGui import QColor |
@@ -217,19 +217,10 @@ def __init__(self, parent=None, data=None, readonly=False, format="%.3f", font=N |
217 | 217 | AbstractArrayModel.__init__(self, parent, data, readonly, font) |
218 | 218 | self._format = format |
219 | 219 |
|
220 | | - # Backgroundcolor settings |
221 | | - # TODO: use LinearGradient |
222 | | - # self.bgfunc = bgfunc |
223 | | - huerange = [.66, .99] # Hue |
224 | | - self.sat = .7 # Saturation |
225 | | - self.val = 1. # Value |
226 | | - self.alp = .6 # Alpha-channel |
227 | | - self.hue0 = huerange[0] |
228 | | - self.dhue = huerange[1] - huerange[0] |
| 220 | + # Backgroundcolor settings (HSV --> Hue, Saturation, Value, Alpha-channel) |
| 221 | + self.hsv_min = [0.66, 0.7, 1.0, 0.6] |
| 222 | + self.hsv_max = [0.99, 0.7, 1.0, 0.6] |
229 | 223 | self.bgcolor_enabled = True |
230 | | - # hue = self.hue0 |
231 | | - # color = QColor.fromHsvF(hue, self.sat, self.val, self.alp) |
232 | | - # self.color = to_qvariant(color) |
233 | 224 |
|
234 | 225 | self.minvalue = minvalue |
235 | 226 | self.maxvalue = maxvalue |
@@ -293,11 +284,14 @@ def reset_minmax(self): |
293 | 284 | if self.vmax == self.vmin: |
294 | 285 | self.vmin -= 1 |
295 | 286 | self.bgcolor_enabled = True |
| 287 | + self.bg_gradient = LinearGradient([(self.vmin, self.hsv_min), (self.vmax, self.hsv_max)]) |
| 288 | + |
296 | 289 | # ValueError for empty arrays |
297 | 290 | except (TypeError, ValueError): |
298 | 291 | self.vmin = None |
299 | 292 | self.vmax = None |
300 | 293 | self.bgcolor_enabled = False |
| 294 | + self.bg_gradient = None |
301 | 295 |
|
302 | 296 | def set_format(self, format): |
303 | 297 | """Change display format""" |
@@ -352,12 +346,8 @@ def data(self, index, role=Qt.DisplayRole): |
352 | 346 | return to_qvariant(self._format % value) |
353 | 347 | elif role == Qt.BackgroundColorRole: |
354 | 348 | if self.bgcolor_enabled and value is not np.ma.masked: |
355 | | - if self.bg_gradient is None: |
356 | | - maxdiff = self.vmax - self.vmin |
357 | | - color_val = float(self.color_func(value)) |
358 | | - hue = self.hue0 + self.dhue * (self.vmax - color_val) / maxdiff |
359 | | - color = QColor.fromHsvF(hue, self.sat, self.val, self.alp) |
360 | | - return to_qvariant(color) |
| 349 | + if self.bg_value is None: |
| 350 | + return self.bg_gradient[float(self.color_func(value))] |
361 | 351 | else: |
362 | 352 | bg_value = self.bg_value |
363 | 353 | x, y = index.row(), index.column() |
|
0 commit comments