Engauge Digitizer  2
 All Classes Functions Variables Typedefs Enumerations Friends Pages
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 <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  {
39  case COLOR_FILTER_MODE_FOREGROUND:
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 
56  case COLOR_FILTER_MODE_HUE:
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 
82  case COLOR_FILTER_MODE_INTENSITY:
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 
93  case COLOR_FILTER_MODE_SATURATION:
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 
104  case COLOR_FILTER_MODE_VALUE:
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 }
int saturationLow() const
Get method for saturation lower bound.
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...
Color filter parameters for one curve. For a class, this is handled the same as LineStyle and PointSt...
int hueLow() const
Get method for hue lower bound.
int foregroundHigh() const
Get method for foreground higher bound.
void setEnabled(bool enabled)
Show the style with semi-transparency or full-transparency to indicate if associated Curve is active ...
int hueHigh() const
Get method for hue higher bound.
ViewSegmentFilter(QWidget *parent=0)
Single constructor.
int saturationHigh() const
Get method for saturation higher bound.
Class for filtering image to remove unimportant information.
Definition: ColorFilter.h:20
int foregroundLow() const
Get method for foreground lower bound.
ColorFilterMode colorFilterMode() const
Get method for filter mode.
int valueLow() const
Get method for value low.
int intensityHigh() const
Get method for intensity higher bound.
virtual void paintEvent(QPaintEvent *event)
Paint with a horizontal linear gradient.
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:73
int valueHigh() const
Get method for value high.
void unsetColorFilterSettings()
Apply no color filter.
int intensityLow() const
Get method for intensity lower bound.