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
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 coloFilterMode,
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 (coloFilterMode)
38 {
40 {
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
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
71 g = qFloor ((1.0 - s) * 255);
72 b = qFloor (s * 255);
73 } else {
74 // 0.666-1 is blue-red
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
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
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 ENGAUGE_ASSERT (false);
117 }
118
119 if (!m_enabled) {
120
121 // Change to gray scale
122 int rgbAverage = (r + g + b) / 3;
123 r = rgbAverage;
124 g = rgbAverage;
125 b = rgbAverage;
126 }
127
128 return QColor (r, g, b);
129}
130
131QColor ViewSegmentFilter::colorHigh () const
132{
133 if (m_enabled) {
134 return colorFromSetting (m_colorFilterSettings.colorFilterMode (),
135 m_colorFilterSettings.foregroundHigh (),
136 m_colorFilterSettings.hueHigh (),
137 m_colorFilterSettings.intensityHigh(),
138 m_colorFilterSettings.saturationHigh(),
139 m_colorFilterSettings.valueHigh());
140 } else {
142 }
143}
144
145QColor ViewSegmentFilter::colorLow () const
146{
147 if (m_enabled) {
148 return colorFromSetting (m_colorFilterSettings.colorFilterMode (),
149 m_colorFilterSettings.foregroundLow (),
150 m_colorFilterSettings.hueLow (),
151 m_colorFilterSettings.intensityLow(),
152 m_colorFilterSettings.saturationLow(),
153 m_colorFilterSettings.valueLow());
154 } else {
156 }
157}
158
160{
161 QPainter painter(this);
162
163 if (m_filterIsDefined) {
164
165 // Start and end points are midway up on both sides
166 QLinearGradient gradient (0, height()/2, width(), height()/2);
167
168 // One color at either end
169 gradient.setColorAt (0.0, colorLow ());
170 gradient.setColorAt (1.0, colorHigh ());
171 painter.setBrush (gradient);
172
173 // No border, which is consistent with ViewPointStyle and cleaner
174 painter.setPen (Qt::NoPen);
175
176 painter.drawRect (0, 0, width(), height());
177
178 } else {
179
180 painter.fillRect (0, 0, width (), height (), QBrush (COLOR_FOR_BRUSH_DISABLED));
181
182 }
183}
184
186 const QPixmap &pixmap)
187{
188 LOG4CPP_INFO_S ((*mainCat)) << "ViewSegmentFilter::setColorFilterSettings";
189
190 m_colorFilterSettings = colorFilterSettings;
191 m_filterIsDefined = true;
192
193 // Compute background color
194 ColorFilter filter;
195 QImage img = pixmap.toImage();
196 m_rgbBackground = filter.marginColor(&img);
197
198 // Force a redraw
199 update();
200}
201
203{
204 LOG4CPP_INFO_S ((*mainCat)) << "ViewSegmentFilter::setEnabled"
205 << " enabled=" << (enabled ? "true" : "false");
206
207 m_enabled = enabled;
208
209 // Force a redraw
210 update();
211}
212
214{
215 m_filterIsDefined = false;
216
217 // Force a redraw
218 update();
219}
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
const int INNER_RADIUS_MIN
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS) define ENGAUGE...
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...
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: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