Engauge Digitizer  2
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"
9 #include "ColorFilterSettings.h"
10 #include "EngaugeAssert.h"
11 #include "Logger.h"
12 #include <qmath.h>
13 #include <QPainter>
14 #include <QPixmap>
15 #include "ViewSegmentFilter.h"
16 
17 const 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 
28 QColor 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  {
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  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 
131 QColor 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 {
141  return QColor (COLOR_FOR_BRUSH_DISABLED);
142  }
143 }
144 
145 QColor 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 {
155  return QColor (COLOR_FOR_BRUSH_DISABLED);
156  }
157 }
158 
159 void ViewSegmentFilter::paintEvent(QPaintEvent * /* event */)
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 
202 void ViewSegmentFilter::setEnabled (bool enabled)
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 }
ViewSegmentFilter::setEnabled
void setEnabled(bool enabled)
Show the style with semi-transparency or full-transparency to indicate if associated Curve is active ...
Definition: ViewSegmentFilter.cpp:202
ColorFilterSettings::saturationHigh
int saturationHigh() const
Get method for saturation higher bound.
Definition: ColorFilterSettings.cpp:251
ViewSegmentFilter::ViewSegmentFilter
ViewSegmentFilter(QWidget *parent=0)
Single constructor.
Definition: ViewSegmentFilter.cpp:19
ColorFilterSettings::intensityLow
int intensityLow() const
Get method for intensity lower bound.
Definition: ColorFilterSettings.cpp:171
ColorFilterSettings::hueHigh
int hueHigh() const
Get method for hue higher bound.
Definition: ColorFilterSettings.cpp:156
ColorFilterSettings::hueLow
int hueLow() const
Get method for hue lower bound.
Definition: ColorFilterSettings.cpp:161
INTENSITY_MIN
const int INTENSITY_MIN
Constants for use by CurveFilter and other curve-related classes.
Definition: ColorConstants.h:10
ColorFilterSettings::saturationLow
int saturationLow() const
Get method for saturation lower bound.
Definition: ColorFilterSettings.cpp:256
ColorFilter.h
ColorFilterSettings::valueHigh
int valueHigh() const
Get method for value high.
Definition: ColorFilterSettings.cpp:370
EngaugeAssert.h
ColorFilterSettings::intensityHigh
int intensityHigh() const
Get method for intensity higher bound.
Definition: ColorFilterSettings.cpp:166
VALUE_MIN
const int VALUE_MIN
Definition: ColorConstants.h:26
ColorConstants.h
ColorFilterSettings
Color filter parameters for one curve. For a class, this is handled the same as LineStyle and PointSt...
Definition: ColorFilterSettings.h:18
INTENSITY_MAX
const int INTENSITY_MAX
Definition: ColorConstants.h:11
ColorFilterSettings::colorFilterMode
ColorFilterMode colorFilterMode() const
Get method for filter mode.
Definition: ColorFilterSettings.cpp:112
COLOR_FOR_BRUSH_DISABLED
const QColor COLOR_FOR_BRUSH_DISABLED(Qt::gray)
VALUE_MAX
const int VALUE_MAX
Definition: ColorConstants.h:27
ColorFilter
Class for filtering image to remove unimportant information.
Definition: ColorFilter.h:19
ColorFilter::marginColor
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...
Definition: ColorFilter.cpp:78
Logger.h
ViewSegmentFilter::unsetColorFilterSettings
void unsetColorFilterSettings()
Apply no color filter.
Definition: ViewSegmentFilter.cpp:213
COLOR_FILTER_MODE_SATURATION
Definition: ColorFilterMode.h:18
ColorFilterSettings.h
ColorFilterSettings::valueLow
int valueLow() const
Get method for value low.
Definition: ColorFilterSettings.cpp:375
LOG4CPP_INFO_S
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
COLOR_FILTER_MODE_HUE
Definition: ColorFilterMode.h:16
ViewSegmentFilter.h
SATURATION_MIN
const int SATURATION_MIN
Definition: ColorConstants.h:22
COLOR_FILTER_MODE_INTENSITY
Definition: ColorFilterMode.h:17
mainCat
log4cpp::Category * mainCat
Definition: Logger.cpp:14
ViewSegmentFilter::paintEvent
virtual void paintEvent(QPaintEvent *event)
Paint with a horizontal linear gradient.
Definition: ViewSegmentFilter.cpp:159
HUE_MAX
const int HUE_MAX
Definition: ColorConstants.h:19
COLOR_FILTER_MODE_VALUE
Definition: ColorFilterMode.h:19
ColorFilterMode
ColorFilterMode
Definition: ColorFilterMode.h:11
FOREGROUND_MAX
const int FOREGROUND_MAX
Definition: ColorConstants.h:15
ViewSegmentFilter::setColorFilterSettings
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...
Definition: ViewSegmentFilter.cpp:185
COLOR_FILTER_MODE_FOREGROUND
Definition: ColorFilterMode.h:15
FOREGROUND_MIN
const int FOREGROUND_MIN
Definition: ColorConstants.h:14
ENGAUGE_ASSERT
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS) define ENGAUGE...
Definition: EngaugeAssert.h:19
ColorFilterSettings::foregroundLow
int foregroundLow() const
Get method for foreground lower bound.
Definition: ColorFilterSettings.cpp:138
SATURATION_MAX
const int SATURATION_MAX
Definition: ColorConstants.h:23
HUE_MIN
const int HUE_MIN
Definition: ColorConstants.h:18
ColorFilterSettings::foregroundHigh
int foregroundHigh() const
Get method for foreground higher bound.
Definition: ColorFilterSettings.cpp:133