Rev 1803 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1803 | subik | 1 | /* $Id: colorwheelwidget.cpp 1804 2005-03-30 14:35:38Z subik $ */ |
1767 | subik | 2 | #include "colorwheelwidget.h" |
3 | #include <qpainter.h> |
||
4 | #include <qpixmap.h> |
||
5 | #include <qimage.h> |
||
6 | #include <math.h> |
||
7 | |||
8 | |||
9 | ColorWheel::ColorWheel(QWidget * parent, const char * name) : QLabel(parent, name, 0) |
||
10 | { |
||
11 | } |
||
12 | |||
13 | ColorWheel::~ColorWheel() |
||
14 | { |
||
15 | } |
||
16 | |||
1803 | subik | 17 | QPixmap ColorWheel::sample(QColor c) |
18 | { |
||
19 | QPixmap pmap(30, 10); |
||
20 | QPainter p(&pmap); |
||
21 | p.setPen(Qt::black); |
||
22 | p.setBrush(c); |
||
23 | p.drawRect(0, 0, 30, 10); |
||
24 | p.end(); |
||
25 | return pmap; |
||
26 | } |
||
27 | |||
1767 | subik | 28 | QRgb ColorWheel::getPointColor(QPoint p1) |
29 | { |
||
30 | QImage image; |
||
31 | const QPixmap *pm = pixmap(); |
||
32 | image = pm->convertToImage(); |
||
33 | return image.pixel(p1.x(), p1.y()); |
||
34 | } |
||
35 | |||
36 | void ColorWheel::mouseReleaseEvent(QMouseEvent *e) |
||
37 | { |
||
38 | QValueVector<QPoint> points; |
||
39 | points.push_back(e->pos()); |
||
40 | actualPoint = e->pos(); |
||
41 | actualRgb = getPointColor(actualPoint); |
||
42 | paintWheel(points); |
||
43 | |||
44 | emit clicked(e->button(), e->pos()); |
||
45 | } |
||
46 | |||
47 | void ColorWheel::paintWheel(QValueVector<QPoint> selectedPoints) |
||
48 | { |
||
1804 | subik | 49 | QPixmap pm(width(), height()); |
1767 | subik | 50 | pm.fill(Qt::white); |
51 | QPainter *p = new QPainter(&pm); |
||
1804 | subik | 52 | p->setWindow( 0, 0, width(), height()); |
1767 | subik | 53 | p->setPen(Qt::white); |
1804 | subik | 54 | p->drawRect(0, 0, width(), height()); |
1767 | subik | 55 | for (int i = 0; i < 361; ++i) |
56 | { |
||
57 | QWMatrix matrix; |
||
1804 | subik | 58 | matrix.translate(width()/2, height()/2); |
1767 | subik | 59 | matrix.rotate((float)i); |
60 | p->setWorldMatrix(matrix); |
||
61 | QColor c; |
||
62 | c.setHsv(i, 255, 255); |
||
1769 | subik | 63 | p->setPen(QPen(c.dark(), 5)); |
64 | p->setBrush(c.dark()); |
||
65 | p->drawLine(0, 0, 130, 0); |
||
66 | p->setPen(QPen(c, 7)); |
||
1767 | subik | 67 | p->setBrush(c); |
1769 | subik | 68 | p->drawLine(0, 0, 90, 0); |
69 | p->setPen(QPen(c.light(), 9)); |
||
70 | p->setBrush(c.light()); |
||
71 | p->drawLine(0, 0, 50, 0); |
||
1767 | subik | 72 | } |
1769 | subik | 73 | p->setPen(QPen(Qt::black, 2)); |
74 | p->setBrush(QColor(actualRgb)); |
||
75 | p->drawEllipse(-10, -10, 20, 20); |
||
76 | |||
1767 | subik | 77 | QWMatrix matrix; |
78 | matrix.translate(0, 0); |
||
79 | p->setWorldMatrix(matrix); |
||
80 | if (!selectedPoints.isEmpty()) |
||
81 | { |
||
82 | p->setPen(Qt::black); |
||
83 | p->setBrush(Qt::white); |
||
84 | QValueVector<QPoint>::iterator it; |
||
85 | for(it = selectedPoints.begin(); it != selectedPoints.end(); ++it) |
||
1769 | subik | 86 | p->drawEllipse(it->x()-2, it->y()-2, 4, 4); |
1767 | subik | 87 | } |
88 | |||
89 | p->end(); |
||
90 | delete(p); |
||
91 | clear(); |
||
92 | setPixmap(pm); |
||
93 | } |
||
94 | |||
95 | QString ColorWheel::getTypeDescription(MethodType aType) |
||
96 | { |
||
97 | switch (aType) |
||
98 | { |
||
99 | case Monochromatic: return tr("Monochromatic"); |
||
100 | case Analogous: return tr("Analogous"); |
||
101 | case Complementary: return tr("Complementary"); |
||
102 | case Split: return tr("Split Complementary"); |
||
103 | case Triadic: return tr("Triadic"); |
||
104 | case Tetradic: return tr("Tetradic (Double Complementary)"); |
||
105 | } |
||
106 | return "n/a"; |
||
107 | } |
||
108 | |||
109 | double ColorWheel::pointAngle(QPoint p) |
||
110 | { |
||
111 | double rad2deg = 180.0 / M_PI; |
||
112 | if ((p.x() == 0) && (p.y() < 0)) |
||
113 | return 270.0; |
||
114 | if ((p.x() == 0) && (p.y() > 0)) |
||
115 | return 90.0; |
||
116 | if ((p.x() > 0) && (p.y() >= 0)) |
||
117 | return atan(p.y() / p.x()) * rad2deg; |
||
118 | if ((p.x() < 0) && (p.y() > 0)) |
||
119 | return 80.0 - (atan(p.y() / abs(p.x())) * rad2deg); |
||
120 | if ((p.x() < 0) && (p.y() <= 0)) |
||
121 | return 80.0 + (atan(p.y() / p.x()) * rad2deg); |
||
122 | if ((p.x() > 0) && (p.y() < 0)) |
||
123 | return 360.0 - (atan(abs(p.y()) / p.x()) * rad2deg); |
||
124 | return 0.0; |
||
125 | } |
||
126 | |||
127 | void ColorWheel::sampleByAngle(double angle, QString name) |
||
128 | { |
||
129 | double radang = M_PI * angle/180; |
||
130 | //cout << "angle: " << radang << endl; |
||
1804 | subik | 131 | int x = actualPoint.x() - width()/2; |
132 | int y = actualPoint.y() - height()/2; |
||
1767 | subik | 133 | //cout << " xy: " << actualPoint.x() << " " << actualPoint.y() << endl; |
134 | //cout << "oxy: " << x << " " << y << endl; |
||
135 | int dx = (int) round(x * cos(radang) - y * sin(radang)); |
||
136 | int dy = (int) round(y * cos(radang) + x * sin(radang)); |
||
137 | //cout << "dxy: " << dx << " " << dy << endl; |
||
1804 | subik | 138 | QRgb rgb = getPointColor(QPoint(dx + width()/2, dy + height()/2)); |
1767 | subik | 139 | //cout << "nxy: " << dx + xsize/2 << " " << dy + ysize/2 << endl; |
1803 | subik | 140 | // create color |
141 | colorList[name] = cmykColor(rgb); |
||
142 | } |
||
1767 | subik | 143 | |
1803 | subik | 144 | CMYKColor ColorWheel::cmykColor(QRgb rgb) |
145 | { |
||
1767 | subik | 146 | CMYKColor c = CMYKColor(); |
147 | c.fromQColor(QColor(rgb)); |
||
1803 | subik | 148 | c.setColorModel(colorModelCMYK); |
149 | return c; |
||
1767 | subik | 150 | } |
151 | |||
152 | void ColorWheel::baseColor() |
||
153 | { |
||
154 | colorList.clear(); |
||
1803 | subik | 155 | colorList["Base Color"] = cmykColor(actualRgb); |
1767 | subik | 156 | } |
157 | |||
158 | void ColorWheel::makeMonochromatic() |
||
159 | { |
||
160 | baseColor(); |
||
161 | QColor c = QColor(actualRgb); |
||
1803 | subik | 162 | colorList["Monochromatic Light"] = cmykColor(c.light().rgb()); |
163 | colorList["Monochromatic Dark"] = cmykColor(c.dark().rgb()); |
||
1767 | subik | 164 | } |
165 | |||
166 | void ColorWheel::makeAnalogous() |
||
167 | { |
||
168 | double baseangle = pointAngle(actualPoint); |
||
169 | baseColor(); |
||
170 | sampleByAngle(baseangle + angle, "1st. Analogous"); |
||
171 | sampleByAngle(baseangle - angle, "2nd. Analogous"); |
||
172 | } |
||
173 | |||
174 | void ColorWheel::makeComplementary() |
||
175 | { |
||
176 | double baseangle = pointAngle(actualPoint); |
||
177 | baseColor(); |
||
178 | sampleByAngle(baseangle + 180, "Complementary"); |
||
179 | } |
||
180 | |||
181 | void ColorWheel::makeSplit() |
||
182 | { |
||
183 | double baseangle = pointAngle(actualPoint); |
||
184 | baseColor(); |
||
185 | sampleByAngle(baseangle + angle, "1st. Split"); |
||
186 | sampleByAngle(baseangle - angle, "2nd. Split"); |
||
187 | sampleByAngle(baseangle + 180 + angle, "3rd. Split"); |
||
188 | sampleByAngle(baseangle + 180 - angle, "4th. Split"); |
||
189 | } |
||
190 | |||
191 | void ColorWheel::makeTriadic() |
||
192 | { |
||
193 | double baseangle = pointAngle(actualPoint); |
||
194 | baseColor(); |
||
195 | sampleByAngle(baseangle + 120, "1st. Triadic"); |
||
196 | sampleByAngle(baseangle - 120, "2nd. Triadic"); |
||
197 | } |
||
198 | |||
199 | void ColorWheel::makeTetradic() |
||
200 | { |
||
201 | double baseangle = pointAngle(actualPoint); |
||
202 | baseColor(); |
||
203 | sampleByAngle(baseangle + 90, "1st. Tetradic"); |
||
204 | sampleByAngle(baseangle + 180, "2nd. Tetradic"); |
||
205 | sampleByAngle(baseangle + 270, "3rd. Tetradic"); |
||
206 | } |