Fixed NetSend so it can be used in debug mode.
[qt/compvision.git] / ColorPicker.cpp
1 #include "ColorPicker.h"
2 #include <QChar>
3 #include <QColor>
4
5 ColorPicker::ColorPicker(QWidget *parent) : QDialog(parent)
6 {
7         setupUi(this);
8
9         QObject::connect(slideR, SIGNAL(sliderMoved(int)), this, SLOT(setR(int)));
10         QObject::connect(slideG, SIGNAL(sliderMoved(int)), this, SLOT(setG(int)));
11         QObject::connect(slideB, SIGNAL(sliderMoved(int)), this, SLOT(setB(int)));
12
13         QObject::connect(slideH, SIGNAL(sliderMoved(int)), this, SLOT(setH(int)));
14         QObject::connect(slideS, SIGNAL(sliderMoved(int)), this, SLOT(setS(int)));
15         QObject::connect(slideV, SIGNAL(sliderMoved(int)), this, SLOT(setV(int)));\r
16         \r
17         QObject::connect(okButton, SIGNAL(clicked()), this, SLOT(setDefault()));\r
18         QObject::connect(cancelButton, SIGNAL(clicked()), this, SLOT(restoreDefault()));
19
20         preview=new QPixmap(labelPreview->width(), labelPreview->height());
21 \r
22         lastColor.setRgb(0,0,0);
23         r=g=b=0;
24         h=s=v=0;\r
25         refresh(0);
26         \r
27         // always stay on top\r
28         this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);
29 }\r
30 \r
31 void ColorPicker::setColor(int color)\r
32 {\r
33         QColor c(color);\r
34         \r
35         r=c.red();\r
36         g=c.green();\r
37         b=c.blue();\r
38         slideR->setValue(r);
39         slideG->setValue(g);
40         slideB->setValue(b);
41         \r
42         c.getHsv(&h, &s, &v);\r
43         h=h*256/360;
44         slideH->setValue(h);
45         slideS->setValue(s);
46         slideV->setValue(v);
47 \r
48         refresh(color);
49 }
50
51 void ColorPicker::setR(int r)
52 {
53         this->r=r;
54         QColor c(r,g,b);
55
56         preview->fill(c);
57         labelPreview->setPixmap(*preview);
58
59         c.getHsv(&h, &s, &v);
60         h=h*256/360;\r
61         slideH->setValue(h);
62         slideS->setValue(s);
63         slideV->setValue(v);
64
65         refresh(c.rgb());
66 }
67
68 void ColorPicker::setG(int g)
69 {
70         this->g=g;
71         QColor c(r,g,b);
72
73         preview->fill(c);
74         labelPreview->setPixmap(*preview);
75
76         c.getHsv(&h, &s, &v);\r
77         h=h*256/360;
78         slideH->setValue(h);
79         slideS->setValue(s);
80         slideV->setValue(v);
81
82         refresh(c.rgb());
83 }
84
85 void ColorPicker::setB(int b)
86 {
87         this->b=b;
88         QColor c(r,g,b);
89
90         preview->fill(c);
91         labelPreview->setPixmap(*preview);
92
93         c.getHsv(&h, &s, &v);
94         h=h*256/360;\r
95         slideH->setValue(h);
96         slideS->setValue(s);
97         slideV->setValue(v);
98
99         refresh(c.rgb());
100 }
101
102 void ColorPicker::setH(int h)
103 {
104         this->h=h;
105         QColor c=QColor::fromHsv(h*360/256,s,v);
106
107         preview->fill(c);
108         labelPreview->setPixmap(*preview);
109
110         c.getRgb(&r, &g, &b);
111         slideR->setValue(r);
112         slideG->setValue(g);
113         slideB->setValue(b);
114
115         refresh(c.rgb());
116 }
117
118 void ColorPicker::setS(int s)
119 {
120         this->s=s;
121         QColor c=QColor::fromHsv(h*360/256,s,v);
122
123         c.getRgb(&r, &g, &b);
124         slideR->setValue(r);
125         slideG->setValue(g);
126         slideB->setValue(b);\r
127         \r
128         refresh(c.rgb());
129 }
130
131 void ColorPicker::setV(int v)
132 {
133         this->v=v;
134         QColor c=QColor::fromHsv(h*360/256,s,v);
135
136         c.getRgb(&r, &g, &b);
137         slideR->setValue(r);
138         slideG->setValue(g);
139         slideB->setValue(b);\r
140         \r
141         refresh(c.rgb());
142 }\r
143 \r
144 void ColorPicker::setDefault()\r
145 {\r
146         lastColor.setRgb(r,g,b);\r
147 }\r
148 \r
149 void ColorPicker::restoreDefault()\r
150 {\r
151         lastColor.getRgb(&r, &g, &b);
152         slideR->setValue(r);
153         slideG->setValue(g);
154         slideB->setValue(b);\r
155         \r
156         lastColor.getHsv(&h, &s, &v);
157         slideH->setValue(h*256/360);
158         slideS->setValue(s);
159         slideV->setValue(v);\r
160 \r
161         refresh(lastColor.rgb());\r
162 }\r
163 \r
164 void ColorPicker::refresh(int color)\r
165 {\r
166         QColor c(color);
167
168         preview->fill(c);
169         labelPreview->setPixmap(*preview);
170
171         lineHTML->setText(QString("#%1%2%3").arg(r,2,16,QChar('0')).arg(g,2,16,QChar('0')).arg(b,2,16,QChar('0')));
172         lineMem->setText(QString("0x%1").arg((uint)color,8,16,QChar('0')));
173 }
174 \r
175 #ifdef COLOR_PICKER_MAIN
176 int main(int argc, char **argv)
177 {
178         QApplication app(argc, argv);
179         ColorPicker cp;
180
181         return cp.exec();
182 }
183 #endif\r