From 0c826090d251e06a2eb6c2064c436718f923a20e Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Fri, 8 Sep 2017 10:20:30 +0200 Subject: [PATCH 1/2] create and use a LinearGradient object in DataArrayModel.data() when self.bg_gradient is None --- larray_editor/arraymodel.py | 28 +++++++++------------------- larray_editor/utils.py | 6 ++++++ 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/larray_editor/arraymodel.py b/larray_editor/arraymodel.py index 7711656..bfceb34 100644 --- a/larray_editor/arraymodel.py +++ b/larray_editor/arraymodel.py @@ -3,7 +3,7 @@ import numpy as np from larray_editor.utils import (get_font, from_qvariant, to_qvariant, to_text_string, - is_float, is_number, SUPPORTED_FORMATS) + is_float, is_number, LinearGradient, SUPPORTED_FORMATS) from qtpy.QtCore import Qt, QModelIndex, QAbstractTableModel from qtpy.QtGui import QColor @@ -217,19 +217,10 @@ def __init__(self, parent=None, data=None, readonly=False, format="%.3f", font=N AbstractArrayModel.__init__(self, parent, data, readonly, font) self._format = format - # Backgroundcolor settings - # TODO: use LinearGradient - # self.bgfunc = bgfunc - huerange = [.66, .99] # Hue - self.sat = .7 # Saturation - self.val = 1. # Value - self.alp = .6 # Alpha-channel - self.hue0 = huerange[0] - self.dhue = huerange[1] - huerange[0] + # Backgroundcolor settings (HSV --> Hue, Saturation, Value, Alpha-channel) + self.hsv_min = [0.66, 0.7, 1.0, 0.6] + self.hsv_max = [0.99, 0.7, 1.0, 0.6] self.bgcolor_enabled = True - # hue = self.hue0 - # color = QColor.fromHsvF(hue, self.sat, self.val, self.alp) - # self.color = to_qvariant(color) self.minvalue = minvalue self.maxvalue = maxvalue @@ -293,11 +284,14 @@ def reset_minmax(self): if self.vmax == self.vmin: self.vmin -= 1 self.bgcolor_enabled = True + self.bg_gradient = LinearGradient([(self.vmin, self.hsv_min), (self.vmax, self.hsv_max)]) + # ValueError for empty arrays except (TypeError, ValueError): self.vmin = None self.vmax = None self.bgcolor_enabled = False + self.bg_gradient = None def set_format(self, format): """Change display format""" @@ -352,12 +346,8 @@ def data(self, index, role=Qt.DisplayRole): return to_qvariant(self._format % value) elif role == Qt.BackgroundColorRole: if self.bgcolor_enabled and value is not np.ma.masked: - if self.bg_gradient is None: - maxdiff = self.vmax - self.vmin - color_val = float(self.color_func(value)) - hue = self.hue0 + self.dhue * (self.vmax - color_val) / maxdiff - color = QColor.fromHsvF(hue, self.sat, self.val, self.alp) - return to_qvariant(color) + if self.bg_value is None: + return self.bg_gradient[float(self.color_func(value))] else: bg_value = self.bg_value x, y = index.row(), index.column() diff --git a/larray_editor/utils.py b/larray_editor/utils.py index f33b8ce..8b93f6b 100644 --- a/larray_editor/utils.py +++ b/larray_editor/utils.py @@ -170,6 +170,12 @@ class LinearGradient(object): I cannot believe I had to roll my own class for this when PyQt already contains QLinearGradient... but you cannot get intermediate values out of QLinearGradient! + + Parameters + ---------- + stop_points: list/tuple, optional + List containing pairs (stop_position, colors_HsvF). + `colors` is a 4 elements list containing `hue`, `saturation`, `value` and `alpha-channel` """ def __init__(self, stop_points=None): if stop_points is None: From 5eb47b8d5c26c068afebf840f0ab411f8f792bef Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Fri, 8 Sep 2017 10:23:59 +0200 Subject: [PATCH 2/2] fix #18 : inverted background colors --- larray_editor/arraymodel.py | 4 ++-- larray_editor/comparator.py | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/larray_editor/arraymodel.py b/larray_editor/arraymodel.py index bfceb34..781335b 100644 --- a/larray_editor/arraymodel.py +++ b/larray_editor/arraymodel.py @@ -218,8 +218,8 @@ def __init__(self, parent=None, data=None, readonly=False, format="%.3f", font=N self._format = format # Backgroundcolor settings (HSV --> Hue, Saturation, Value, Alpha-channel) - self.hsv_min = [0.66, 0.7, 1.0, 0.6] - self.hsv_max = [0.99, 0.7, 1.0, 0.6] + self.hsv_min = [0.99, 0.7, 1.0, 0.6] + self.hsv_max = [0.66, 0.7, 1.0, 0.6] self.bgcolor_enabled = True self.minvalue = minvalue diff --git a/larray_editor/comparator.py b/larray_editor/comparator.py index 7017469..a8380eb 100644 --- a/larray_editor/comparator.py +++ b/larray_editor/comparator.py @@ -59,11 +59,11 @@ def setup_and_check(self, arrays, names, title=''): else: # all 0.5 (white) bg_value = full_like(diff, 0.5) - gradient = LinearGradient([(0, [.66, .85, 1., .6]), - (0.5 - 1e-16, [.66, .15, 1., .6]), + gradient = LinearGradient([(0, [.99, .85, 1., .6]), + (0.5 - 1e-16, [.99, .15, 1., .6]), (0.5, [1., 0., 1., 1.]), - (0.5 + 1e-16, [.99, .15, 1., .6]), - (1, [.99, .85, 1., .6])]) + (0.5 + 1e-16, [.66, .15, 1., .6]), + (1, [.66, .85, 1., .6])]) self.arraywidget = ArrayEditorWidget(self, self.array, readonly=True, bg_value=bg_value, @@ -105,11 +105,11 @@ def __init__(self, parent=None): self.names = None self.arraywidget = None self.maxdiff_label = None - self.gradient = LinearGradient([(0, [.66, .85, 1., .6]), - (0.5 - 1e-16, [.66, .15, 1., .6]), + self.gradient = LinearGradient([(0, [.99, .85, 1., .6]), + (0.5 - 1e-16, [.99, .15, 1., .6]), (0.5, [1., 0., 1., 1.]), - (0.5 + 1e-16, [.99, .15, 1., .6]), - (1, [.99, .85, 1., .6])]) + (0.5 + 1e-16, [.66, .15, 1., .6]), + (1, [.66, .85, 1., .6])]) def setup_and_check(self, sessions, names, title=''): """