Skip to content

Commit ad015bb

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

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

larray_editor/arraymodel.py

Lines changed: 6 additions & 17 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
@@ -353,11 +344,9 @@ def data(self, index, role=Qt.DisplayRole):
353344
elif role == Qt.BackgroundColorRole:
354345
if self.bgcolor_enabled and value is not np.ma.masked:
355346
if self.bg_gradient is None:
356-
maxdiff = self.vmax - self.vmin
357347
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)
348+
bg_gradient = LinearGradient([(self.vmin, self.hsv_min), (self.vmax, self.hsv_max)])
349+
return bg_gradient[color_val]
361350
else:
362351
bg_value = self.bg_value
363352
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)