Engauge Digitizer 2
Loading...
Searching...
No Matches
ViewSegmentFilter.cpp
1/******************************************************************************************************
2 * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3 * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4 * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5 ******************************************************************************************************/
6
7#include "ColorConstants.h"
8#include "ColorFilter.h"
9#include "ColorFilterSettings.h"
10#include "EngaugeAssert.h"
11#include "Logger.h"
12#include <QPainter>
13#include <QPixmap>
14#include "ViewSegmentFilter.h"
15
16const QColor COLOR_FOR_BRUSH_DISABLED (Qt::gray);
17
19 QLabel (parent),
20 m_filterIsDefined (false),
21 m_rgbBackground (QColor (Qt::white)),
22 m_enabled (true)
23{
24 // Note the size is set externally by the layout engine
25}
26
27QColor ViewSegmentFilter::colorFromSetting (ColorFilterMode coloFilterMode,
28 int foreground,
29 int hue,
30 int intensity,
31 int saturation,
32 int value) const
33{
34 int r = 0, g = 0, b = 0;
35
36 switch (coloFilterMode)
37 {
38 case COLOR_FILTER_MODE_FOREGROUND:
39 {
40 double s = (double) (foreground - FOREGROUND_MIN) / (double) (FOREGROUND_MAX - FOREGROUND_MIN);
41 if (qGray (m_rgbBackground.rgb ()) < 127) {
42 // Go from blackish to white
43 r = s * 255;
44 g = s * 255;
45 b = s * 255;
46 } else {
47 // Go from whitish to black
48 r = (1.0 - s) * 255;
49 g = (1.0 - s) * 255;
50 b = (1.0 - s) * 255;
51 }
52 }
53 break;
54
55 case COLOR_FILTER_MODE_HUE:
56 {
57 // red-green and green-blue like ViewProfileScale::paintHue
58
59 int HUE_THRESHOLD_LOW = 0.666 * HUE_MIN + 0.333 * HUE_MAX;
60 int HUE_THRESHOLD_HIGH = 0.333 * HUE_MIN + 0.666 * HUE_MAX;
61
62 if (hue < HUE_THRESHOLD_LOW) {
63 // 0-0.333 is red-green
64 double s = (double) (hue - HUE_MIN) / (double) (HUE_THRESHOLD_LOW - HUE_MIN);
65 r = (1.0 - s) * 255;
66 g = s * 255;
67 } else if (hue < HUE_THRESHOLD_HIGH) {
68 // 0.333-0.666 is green-blue
69 double s = (double) (hue - HUE_THRESHOLD_LOW) / (double) (HUE_THRESHOLD_HIGH - HUE_THRESHOLD_LOW);
70 g = (1.0 - s) * 255;
71 b = s * 255;
72 } else {
73 // 0.666-1 is blue-red
74 double s = (double) (hue - HUE_THRESHOLD_HIGH) / (double) (HUE_MAX - HUE_THRESHOLD_HIGH);
75 b = (1.0 - s) * 255;
76 r = s * 255;
77 }
78 }
79 break;
80
81 case COLOR_FILTER_MODE_INTENSITY:
82 {
83 // black-white like ViewProfileScale::paintIntensity
84
85 double s = (double) (intensity - INTENSITY_MIN) / (double) (INTENSITY_MAX - INTENSITY_MIN);
86 r = s * 255;
87 g = s * 255;
88 b = s * 255;
89 }
90 break;
91
92 case COLOR_FILTER_MODE_SATURATION:
93 {
94 // white-red like ViewProfileScale::paintSaturation
95
96 double s = (double) (saturation - SATURATION_MIN) / (double) (SATURATION_MAX - SATURATION_MIN);
97 r = 255;
98 g = (1.0 - s) * 255;
99 b = (1.0 - s) * 255;
100 }
101 break;
102
103 case COLOR_FILTER_MODE_VALUE:
104 {
105 // black-red like ViewProfileScale::paintValue
106
107 double s = (double) (value - VALUE_MIN) / (double) (VALUE_MAX - VALUE_MIN);
108 r = s * 255;
109 g = 0;
110 b = 0;
111 }
112 break;
113
114 default:
115 ENGAUGE_ASSERT (false);
116 }
117
118 if (!m_enabled) {
119
120 // Change to gray scale
121 int rgbAverage = (r + g + b) / 3;
122 r = rgbAverage;
123 g = rgbAverage;
124 b = rgbAverage;
125 }
126
127 return QColor (r, g, b);
128}
129
130QColor ViewSegmentFilter::colorHigh () const
131{
132 if (m_enabled) {
133 return colorFromSetting (m_colorFilterSettings.colorFilterMode (),
134 m_colorFilterSettings.foregroundHigh (),
135 m_colorFilterSettings.hueHigh (),
136 m_colorFilterSettings.intensityHigh(),
137 m_colorFilterSettings.saturationHigh(),
138 m_colorFilterSettings.valueHigh());
139 } else {
140 return QColor (COLOR_FOR_BRUSH_DISABLED);
141 }
142}
143
144QColor ViewSegmentFilter::colorLow () const
145{
146 if (m_enabled) {
147 return colorFromSetting (m_colorFilterSettings.colorFilterMode (),
148 m_colorFilterSettings.foregroundLow (),
149 m_colorFilterSettings.hueLow (),
150 m_colorFilterSettings.intensityLow(),
151 m_colorFilterSettings.saturationLow(),
152 m_colorFilterSettings.valueLow());
153 } else {
154 return QColor (COLOR_FOR_BRUSH_DISABLED);
155 }
156}
157
158void ViewSegmentFilter::paintEvent(QPaintEvent * /* event */)
159{
160 QPainter painter(this);
161
162 if (m_filterIsDefined) {
163
164 // Start and end points are midway up on both sides
165 QLinearGradient gradient (0, height()/2, width(), height()/2);
166
167 // One color at either end
168 gradient.setColorAt (0.0, colorLow ());
169 gradient.setColorAt (1.0, colorHigh ());
170 painter.setBrush (gradient);
171
172 // No border, which is consistent with ViewPointStyle and cleaner
173 painter.setPen (Qt::NoPen);
174
175 painter.drawRect (0, 0, width(), height());
176
177 } else {
178
179 painter.fillRect (0, 0, width (), height (), QBrush (COLOR_FOR_BRUSH_DISABLED));
180
181 }
182}
183
185 const QPixmap &pixmap)
186{
187 LOG4CPP_INFO_S ((*mainCat)) << "ViewSegmentFilter::setColorFilterSettings";
188
189 m_colorFilterSettings = colorFilterSettings;
190 m_filterIsDefined = true;
191
192 // Compute background color
193 ColorFilter filter;
194 QImage img = pixmap.toImage();
195 m_rgbBackground = filter.marginColor(&img);
196
197 // Force a redraw
198 update();
199}
200
202{
203 LOG4CPP_INFO_S ((*mainCat)) << "ViewSegmentFilter::setEnabled"
204 << " enabled=" << (enabled ? "true" : "false");
205
206 m_enabled = enabled;
207
208 // Force a redraw
209 update();
210}
211
213{
214 m_filterIsDefined = false;
215
216 // Force a redraw
217 update();
218}
Color filter parameters for one curve. For a class, this is handled the same as LineStyle and PointSt...
int foregroundLow() const
Get method for foreground lower bound.
int saturationLow() const
Get method for saturation lower bound.
int intensityHigh() const
Get method for intensity higher bound.
int saturationHigh() const
Get method for saturation higher bound.
ColorFilterMode colorFilterMode() const
Get method for filter mode.
int valueLow() const
Get method for value low.
int hueHigh() const
Get method for hue higher bound.
int foregroundHigh() const
Get method for foreground higher bound.
int intensityLow() const
Get method for intensity lower bound.
int hueLow() const
Get method for hue lower bound.
int valueHigh() const
Get method for value high.
Class for filtering image to remove unimportant information.
Definition ColorFilter.h:19
QRgb marginColor(const QImage *image) const
Identify the margin color of the image, which is defined as the most common color in the four margins...
void unsetColorFilterSettings()
Apply no color filter.
void setColorFilterSettings(const ColorFilterSettings &colorFilterSettings, const QPixmap &pixmap)
Apply the color filter of the currently selected curve. The pixmap is included so the background colo...
void setEnabled(bool enabled)
Show the style with semi-transparency or full-transparency to indicate if associated Curve is active ...
virtual void paintEvent(QPaintEvent *event)
Paint with a horizontal linear gradient.
ViewSegmentFilter(QWidget *parent=0)
Single constructor.