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