Skip to content

Commit 0c82609

Browse files
committed
create and use a LinearGradient object in DataArrayModel.data() when self.bg_gradient is None
1 parent f9c38d3 commit 0c82609

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

larray_editor/arraymodel.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44

55
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)
77

88
from qtpy.QtCore import Qt, QModelIndex, QAbstractTableModel
99
from qtpy.QtGui import QColor
@@ -217,19 +217,10 @@ def __init__(self, parent=None, data=None, readonly=False, format="%.3f", font=N
217217
AbstractArrayModel.__init__(self, parent, data, readonly, font)
218218
self._format = format
219219

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]
229223
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)
233224

234225
self.minvalue = minvalue
235226
self.maxvalue = maxvalue
@@ -293,11 +284,14 @@ def reset_minmax(self):
293284
if self.vmax == self.vmin:
294285
self.vmin -= 1
295286
self.bgcolor_enabled = True
287+
self.bg_gradient = LinearGradient([(self.vmin, self.hsv_min), (self.vmax, self.hsv_max)])
288+
296289
# ValueError for empty arrays
297290
except (TypeError, ValueError):
298291
self.vmin = None
299292
self.vmax = None
300293
self.bgcolor_enabled = False
294+
self.bg_gradient = None
301295

302296
def set_format(self, format):
303297
"""Change display format"""
@@ -352,12 +346,8 @@ def data(self, index, role=Qt.DisplayRole):
352346
return to_qvariant(self._format % value)
353347
elif role == Qt.BackgroundColorRole:
354348
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))]
361351
else:
362352
bg_value = self.bg_value
363353
x, y = index.row(), index.column()

larray_editor/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ class LinearGradient(object):
170170
I cannot believe I had to roll my own class for this when PyQt already
171171
contains QLinearGradient... but you cannot get intermediate values out of
172172
QLinearGradient!
173+
174+
Parameters
175+
----------
176+
stop_points: list/tuple, optional
177+
List containing pairs (stop_position, colors_HsvF).
178+
`colors` is a 4 elements list containing `hue`, `saturation`, `value` and `alpha-channel`
173179
"""
174180
def __init__(self, stop_points=None):
175181
if stop_points is None:

0 commit comments

Comments
 (0)