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 "EngaugeAssert.h"
8 #include "Logger.h"
9 #include "ViewProfileScale.h"
10 #include <QPainter>
11 
ViewProfileScale(int minimumWidth,QWidget * parent)12 ViewProfileScale::ViewProfileScale(int minimumWidth,
13                                    QWidget *parent) :
14   QLabel (parent),
15   m_colorFilterMode (COLOR_FILTER_MODE_FOREGROUND)
16 {
17   setMinimumWidth(minimumWidth);
18 }
19 
paintEvent(QPaintEvent * event)20 void ViewProfileScale::paintEvent (QPaintEvent *event)
21 {
22   switch (m_colorFilterMode) {
23     case COLOR_FILTER_MODE_FOREGROUND:
24       paintForeground ();
25       break;
26 
27     case COLOR_FILTER_MODE_HUE:
28       paintHue ();
29       break;
30 
31     case COLOR_FILTER_MODE_INTENSITY:
32       paintIntensity ();
33       break;
34 
35     case COLOR_FILTER_MODE_SATURATION:
36       paintSaturation ();
37       break;
38 
39     case COLOR_FILTER_MODE_VALUE:
40       paintValue ();
41       break;
42 
43     default:
44       LOG4CPP_ERROR_S ((*mainCat)) << "ViewProfileScale::paintEvent unexpected color filter mode " << m_colorFilterMode;
45       ENGAUGE_ASSERT (false);
46   }
47 
48   QLabel::paintEvent (event);
49 }
50 
paintForeground()51 void ViewProfileScale::paintForeground ()
52 {
53   if (qGray (m_rgbBackground) < 127) {
54     // Go from blackish to white
55     paintOneSpectrum (QColor (m_rgbBackground), QColor (Qt::white));
56   } else {
57     // Go from whitish to black
58     paintOneSpectrum (QColor (m_rgbBackground), QColor (Qt::black));
59   }
60 }
61 
paintHue()62 void ViewProfileScale::paintHue ()
63 {
64   // Create two spectrums:
65   // 1) one spectrum from red to green
66   // 2) another from green to blue
67   QLinearGradient gradient (QPointF (0.0,
68                                      height() / 2.0),
69                             QPointF (width (),
70                                      height () / 2.0));
71   gradient.setColorAt (0.0000, Qt::red);
72   gradient.setColorAt (0.3333, Qt::green);
73   gradient.setColorAt (0.6666, Qt::blue);
74   gradient.setColorAt (1.0000, Qt::red);
75 
76   QPainter painter (this);
77   painter.setPen (Qt::NoPen);
78 
79   QBrush brush (gradient);
80 
81   painter.setBrush (brush);
82   painter.drawRect (0,
83                     0,
84                     rect().width (),
85                     rect().height ());
86 }
87 
paintIntensity()88 void ViewProfileScale::paintIntensity ()
89 {
90   paintOneSpectrum (QColor (Qt::black), QColor (Qt::white));
91 }
92 
paintOneSpectrum(const QColor & colorStart,const QColor & colorStop)93 void ViewProfileScale::paintOneSpectrum (const QColor &colorStart,
94                                  const QColor &colorStop)
95 {
96   QLinearGradient gradient (QPointF (0.0,
97                                        height() / 2.0),
98                               QPointF (width (),
99                                        height () / 2.0));
100   gradient.setColorAt (0, colorStart);
101   gradient.setColorAt (1, colorStop);
102 
103   QPainter painter (this);
104   painter.setPen (Qt::NoPen);
105 
106   QBrush brush (gradient);
107 
108   painter.setBrush (brush);
109   painter.drawRect (0,
110                     0,
111                     rect().width (),
112                     rect().height ());
113 }
114 
paintSaturation()115 void ViewProfileScale::paintSaturation ()
116 {
117   paintOneSpectrum (QColor (Qt::white), QColor (Qt::red));
118 }
119 
paintValue()120 void ViewProfileScale::paintValue ()
121 {
122   paintOneSpectrum (QColor (Qt::black), QColor (Qt::red));
123 }
124 
setBackgroundColor(QRgb rgbBackground)125 void ViewProfileScale::setBackgroundColor (QRgb rgbBackground)
126 {
127   m_rgbBackground = rgbBackground;
128 }
129 
setColorFilterMode(ColorFilterMode colorFilterMode)130 void ViewProfileScale::setColorFilterMode (ColorFilterMode colorFilterMode)
131 {
132   m_colorFilterMode = colorFilterMode;
133   update ();
134 }
135