Engauge Digitizer 2
Loading...
Searching...
No Matches
ViewSegmentFilter.cpp
Go to the documentation of this file.
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"
10#include "EngaugeAssert.h"
11#include "Logger.h"
12#include <qmath.h>
13#include <QPainter>
14#include <QPixmap>
15#include "ViewSegmentFilter.h"
16
17const QColor COLOR_FOR_BRUSH_DISABLED (Qt::gray);
18
20 QLabel (parent),
21 m_filterIsDefined (false),
22 m_rgbBackground (QColor (Qt::white)),
23 m_enabled (true)
24{
25 // Note the size is set externally by the layout engine
26}
27
28QColor ViewSegmentFilter::colorFromSetting (ColorFilterMode colorFilterMode,
29 int foreground,
30 int hue,
31 int intensity,
32 int saturation,
33 int value) const
34{
35 int r = 0, g = 0, b = 0;
36
37 switch (colorFilterMode)
38 {
40 {
41 double s = double (foreground - FOREGROUND_MIN) / double (FOREGROUND_MAX - FOREGROUND_MIN);
42 if (qGray (m_rgbBackground.rgb ()) < 127) {
43 // Go from blackish to white
44 r = qFloor (s * 255);
45 g = qFloor (s * 255);
46 b = qFloor (s * 255);
47 } else {
48 // Go from whitish to black
49 r = qFloor ((1.0 - s) * 255);
50 g = qFloor ((1.0 - s) * 255);
51 b = qFloor ((1.0 - s) * 255);
52 }
53 }
54 break;
55
57 {
58 // red-green and green-blue like ViewProfileScale::paintHue
59
60 int HUE_THRESHOLD_LOW = qFloor (0.666 * HUE_MIN + 0.333 * HUE_MAX);
61 int HUE_THRESHOLD_HIGH = qFloor (0.333 * HUE_MIN + 0.666 * HUE_MAX);
62
63 if (hue < HUE_THRESHOLD_LOW) {
64 // 0-0.333 is red-green
65 double s = double (hue - HUE_MIN) / double (HUE_THRESHOLD_LOW - HUE_MIN);
66 r = qFloor ((1.0 - s) * 255);
67 g = qFloor (s * 255);
68 } else if (hue < HUE_THRESHOLD_HIGH) {
69 // 0.333-0.666 is green-blue
70 double s = double (hue - HUE_THRESHOLD_LOW) / double (HUE_THRESHOLD_HIGH - HUE_THRESHOLD_LOW);
71 g = qFloor ((1.0 - s) * 255);
72 b = qFloor (s * 255);
73 } else {
74 // 0.666-1 is blue-red
75 double s = double (hue - HUE_THRESHOLD_HIGH) / double (HUE_MAX - HUE_THRESHOLD_HIGH);
76 b = qFloor ((1.0 - s) * 255);
77 r = qFloor (s * 255);
78 }
79 }
80 break;
81
83 {
84 // black-white like ViewProfileScale::paintIntensity
85
86 double s = double (intensity - INTENSITY_MIN) / double (INTENSITY_MAX - INTENSITY_MIN);
87 r = qFloor (s * 255);
88 g = qFloor (s * 255);
89 b = qFloor (s * 255);
90 }
91 break;
92
94 {
95 // white-red like ViewProfileScale::paintSaturation
96
97 double s = double (saturation - SATURATION_MIN) / double (SATURATION_MAX - SATURATION_MIN);
98 r = qFloor (255);
99 g = qFloor ((1.0 - s) * 255);
100 b = qFloor ((1.0 - s) * 255);
101 }
102 break;
103
105 {
106 // black-red like ViewProfileScale::paintValue
107
108 double s = double (value - VALUE_MIN) / double (VALUE_MAX - VALUE_MIN);
109 r = qFloor (s * 255);
110 g = qFloor (0);
111 b = qFloor (0);
112 }
113 break;
114
115 default:
116 LOG4CPP_ERROR_S ((*mainCat)) << "ViewSegmentFilter::colorFromSetting unexpected color filter mode " << colorFilterMode;
117 ENGAUGE_ASSERT (false);
118 }
119
120 if (!m_enabled) {
121
122 // Change to gray scale
123 int rgbAverage = (r + g + b) / 3;
124 r = rgbAverage;
125 g = rgbAverage;
126 b = rgbAverage;
127 }
128
129 return QColor (r, g, b);
130}
131
132QColor ViewSegmentFilter::colorHigh () const
133{
134 if (m_enabled) {
135 return colorFromSetting (m_colorFilterSettings.colorFilterMode (),
136 m_colorFilterSettings.foregroundHigh (),
137 m_colorFilterSettings.hueHigh (),
138 m_colorFilterSettings.intensityHigh(),
139 m_colorFilterSettings.saturationHigh(),
140 m_colorFilterSettings.valueHigh());
141 } else {
142 return QColor (COLOR_FOR_BRUSH_DISABLED);
143 }
144}
145
146QColor ViewSegmentFilter::colorLow () const
147{
148 if (m_enabled) {
149 return colorFromSetting (m_colorFilterSettings.colorFilterMode (),
150 m_colorFilterSettings.foregroundLow (),
151 m_colorFilterSettings.hueLow (),
152 m_colorFilterSettings.intensityLow(),
153 m_colorFilterSettings.saturationLow(),
154 m_colorFilterSettings.valueLow());
155 } else {
156 return QColor (COLOR_FOR_BRUSH_DISABLED);
157 }
158}
159
160void ViewSegmentFilter::paintEvent(QPaintEvent * /* event */)
161{
162 QPainter painter(this);
163
164 if (m_filterIsDefined) {
165
166 // Start and end points are midway up on both sides
167 QLinearGradient gradient (0, height()/2, width(), height()/2);
168
169 // One color at either end
170 gradient.setColorAt (0.0, colorLow ());
171 gradient.setColorAt (1.0, colorHigh ());
172 painter.setBrush (gradient);
173
174 // No border, which is consistent with ViewPointStyle and cleaner
175 painter.setPen (Qt::NoPen);
176
177 painter.drawRect (0, 0, width(), height());
178
179 } else {
180
181 painter.fillRect (0, 0, width (), height (), QBrush (COLOR_FOR_BRUSH_DISABLED));
182
183 }
184}
185
187 const QPixmap &pixmap)
188{
189 LOG4CPP_INFO_S ((*mainCat)) << "ViewSegmentFilter::setColorFilterSettings";
190
191 m_colorFilterSettings = colorFilterSettings;
192 m_filterIsDefined = true;
193
194 // Compute background color
195 ColorFilter filter;
196 QImage img = pixmap.toImage();
197 m_rgbBackground = filter.marginColor(&img);
198
199 // Force a redraw
200 update();
201}
202
204{
205 LOG4CPP_INFO_S ((*mainCat)) << "ViewSegmentFilter::setEnabled"
206 << " enabled=" << (enabled ? "true" : "false");
207
208 m_enabled = enabled;
209
210 // Force a redraw
211 update();
212}
213
215{
216 m_filterIsDefined = false;
217
218 // Force a redraw
219 update();
220}
const int SATURATION_MAX
const int FOREGROUND_MAX
const int HUE_MAX
const int SATURATION_MIN
const int HUE_MIN
const int INTENSITY_MAX
const int FOREGROUND_MIN
const int VALUE_MAX
const int VALUE_MIN
const int INTENSITY_MIN
Constants for use by CurveFilter and other curve-related classes.
ColorFilterMode
@ COLOR_FILTER_MODE_FOREGROUND
@ COLOR_FILTER_MODE_VALUE
@ COLOR_FILTER_MODE_INTENSITY
@ COLOR_FILTER_MODE_SATURATION
@ COLOR_FILTER_MODE_HUE
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT.
log4cpp::Category * mainCat
Definition Logger.cpp:14
const QColor COLOR_FOR_BRUSH_DISABLED(Qt::gray)
const QColor COLOR_FOR_BRUSH_DISABLED(Qt::gray)
Color filter parameters for one curve. For a class, this is handled the same as LineStyle and PointSt...
Class for filtering image to remove unimportant information.
Definition ColorFilter.h:21
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.
#define LOG4CPP_INFO_S(logger)
Definition convenience.h:18
#define LOG4CPP_ERROR_S(logger)
Definition convenience.h:12