Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
colorinspector.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2012-2017 Matthew Chiawen Chang
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 */
16 #include "colorinspector.h"
17 #include "ui_colorinspector.h"
18 
19 #include <QDebug>
20 
21 ColorInspector::ColorInspector(QWidget *parent) :
22  QWidget(parent),
23  ui(new Ui::ColorInspector),
24  isRgbColors(true),
25  noColorUpdate(false)
26 {
27  ui->setupUi(this);
28 
29  //We constrain the widget, as the layout should have a stretch limit.
30  parent->setMaximumSize(500,height());
31 
32  connect(ui->RedspinBox, SIGNAL(valueChanged(int)),
33  this, SLOT(onColorChanged()));
34  connect(ui->GreenspinBox, SIGNAL(valueChanged(int)),
35  this, SLOT(onColorChanged()));
36  connect(ui->BluespinBox, SIGNAL(valueChanged(int)),
37  this, SLOT(onColorChanged()));
38  connect(ui->AlphaspinBox, SIGNAL(valueChanged(int)),
39  this, SLOT(onColorChanged()));
40  connect(ui->rgb, SIGNAL(toggled(bool)),
41  this, SLOT(onModeChanged()));
42 }
43 
44 ColorInspector::~ColorInspector()
45 {
46  delete ui;
47 }
48 
49 void ColorInspector::setColor(const QColor &newColor)
50 {
51  if (newColor == m_color)
52  {
53  return;
54  }
55  noColorUpdate = true;
56 
57  if(isRgbColors)
58  {
59  ui->RedspinBox->setValue(newColor.red());
60  ui->GreenspinBox->setValue(newColor.green());
61  ui->BluespinBox->setValue(newColor.blue());
62  ui->AlphaspinBox->setValue(newColor.alpha());
63  }
64  else
65  {
66  ui->RedspinBox->setValue( qBound(0.0, newColor.hsvHueF() * 359, 359.0) );
67  ui->GreenspinBox->setValue( qBound(0.0, newColor.hsvSaturationF() * 100, 100.0) );
68  ui->BluespinBox->setValue( qBound(0.0, newColor.valueF() * 100, 100.0) );
69  ui->AlphaspinBox->setValue( qBound(0.0, newColor.alphaF() * 100, 100.0) );
70  }
71  m_color = newColor;
72 
73  QPalette p1 = ui->colorWrapper->palette(), p2 = ui->color->palette();
74  p1.setBrush(QPalette::Background, QBrush(QImage(":/background/checkerboard.png")));
75  p2.setColor(QPalette::Background, m_color);
76  ui->colorWrapper->setPalette(p1);
77  ui->color->setPalette(p2);
78  //ui->color->setFixedSize(30,30);
79  noColorUpdate = false;
80 }
81 
82 QColor ColorInspector::color()
83 {
84  return m_color;
85 }
86 
87 void ColorInspector::onModeChanged()
88 {
89  bool newValue = ui->rgb->isChecked();
90  if (isRgbColors == newValue)
91  {
92  return;
93  }
94 
95  isRgbColors = newValue;
96  noColorUpdate = true;
97 
98  if (!isRgbColors)
99  {
100  ui->red->setText(tr("Hue"));
101  ui->green->setText(tr("Saturation"));
102  ui->blue->setText(tr("Value"));
103  ui->alpha->setText(tr("Alpha"));
104 
105  ui->RedspinBox->setRange(0,359);
106  ui->GreenspinBox->setRange(0,100);
107  ui->GreenspinBox->setSuffix("%");
108  ui->BluespinBox->setRange(0,100);
109  ui->BluespinBox->setSuffix("%");
110  ui->AlphaspinBox->setRange(0,100);
111  ui->AlphaspinBox->setSuffix("%");
112 
113  m_color = m_color.toHsv();
114  ui->RedspinBox->setValue( qBound(0.0, m_color.hsvHueF()*359, 359.0) );
115  ui->GreenspinBox->setValue( qBound(0.0, m_color.hsvSaturationF()*100, 100.0) );
116  ui->BluespinBox->setValue( qBound(0.0, m_color.valueF()*100, 100.0) );
117  ui->AlphaspinBox->setValue( qBound(0.0, m_color.alphaF()*100, 100.0) );
118  }
119  else
120  {
121  ui->red->setText(tr("Red"));
122  ui->green->setText(tr("Green"));
123  ui->blue->setText(tr("Blue"));
124  ui->alpha->setText(tr("Alpha"));
125 
126  ui->RedspinBox->setRange(0,255);
127  ui->GreenspinBox->setRange(0,255);
128  ui->GreenspinBox->setSuffix("");
129  ui->BluespinBox->setRange(0,255);
130  ui->BluespinBox->setSuffix("");
131  ui->AlphaspinBox->setRange(0,255);
132  ui->AlphaspinBox->setSuffix("");
133  m_color = m_color.toRgb();
134  ui->RedspinBox->setValue(m_color.red());
135  ui->GreenspinBox->setValue(m_color.green());
136  ui->BluespinBox->setValue(m_color.blue());
137  ui->AlphaspinBox->setValue(m_color.alpha());
138  }
139  noColorUpdate = false;
140  emit modeChange(isRgbColors);
141 }
142 
143 void ColorInspector::onColorChanged()
144 {
145  if(noColorUpdate) return;
146 
147  QColor c;
148  if(isRgbColors){
149  c = QColor::fromRgb(ui->RedspinBox->value(),
150  ui->GreenspinBox->value(),
151  ui->BluespinBox->value(),
152  ui->AlphaspinBox->value()
153  );
154  }else{
155  c = QColor::fromHsvF(qBound(0.0,
156  ui->RedspinBox->value()/359.0,
157  1.0),
158  qBound(0.0,
159  ui->GreenspinBox->value()/100.0,
160  1.0),
161  qBound(0.0,
162  ui->BluespinBox->value()/100.0,
163  1.0),
164  qBound(0.0,
165  ui->AlphaspinBox->value()/100.0,
166  1.0)
167  );
168  }
169 
170  setColor(c);
171  emit colorChanged(c);
172 }