Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_color_selector_simple.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2010 Adam Celarek <kdedev at xibo dot at>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8#include <QPainter>
9#include <QColor>
10#include <cmath>
11#include <kconfig.h>
12#include <kconfiggroup.h>
13#include <ksharedconfig.h>
14
17
18
24
26{
27 m_lastClickPos.setX(x/qreal(width()));
28 m_lastClickPos.setY(y/qreal(height()));
29
30 qreal xRel = x/qreal(width());
31 qreal yRel = 1.-y/qreal(height());
32 qreal relPos;
33 if(height()>width())
34 relPos = 1.-y/qreal(height());
35 else
36 relPos = x/qreal(width());
37
38 switch (m_parameter) {
40 Q_EMIT paramChanged(relPos, -1, -1, -1, -1, -1, -1, -1, -1);
41 break;
43 Q_EMIT paramChanged(relPos, -1, -1, -1, -1, -1, -1, -1, -1);
44 break;
46 Q_EMIT paramChanged(-1, relPos, -1, -1, -1, -1, -1, -1, -1);
47 break;
49 Q_EMIT paramChanged(-1, -1, -1, relPos, -1, -1, -1, -1, -1);
50 break;
52 Q_EMIT paramChanged(-1, -1, -1, -1, -1, relPos, -1, -1, -1);
53 break;
55 Q_EMIT paramChanged(-1, -1, -1, -1, -1, -1, -1, relPos, -1);
56 break;
58 Q_EMIT paramChanged(-1, -1, relPos, -1, -1, -1, -1, -1, -1);
59 break;
61 Q_EMIT paramChanged(-1, -1, -1, -1, relPos, -1, -1, -1, -1);
62 break;
64 Q_EMIT paramChanged(-1, -1, -1, -1, -1, -1, relPos, -1, -1);
65 break;
67 Q_EMIT paramChanged(-1, -1, -1, -1, -1, -1, -1, -1, relPos);
68 break;
70 Q_EMIT paramChanged(-1, -1, -1, xRel, yRel, -1, -1, -1, -1);
71 break;
73 Q_EMIT paramChanged(-1, -1, -1, -1, -1, xRel, yRel, -1, -1);
74 break;
76 Q_EMIT paramChanged(-1, -1, -1, -1, -1, -1, -1, xRel, yRel);
77 break;
80 Q_EMIT paramChanged(-1, xRel, yRel, -1, -1, -1, -1, -1, -1);
81 break;
83 Q_EMIT paramChanged(xRel, yRel, -1, -1, -1, -1, -1, -1, -1);
84 break;
86 Q_EMIT paramChanged(xRel, -1, -1, yRel, -1, -1, -1, -1, -1);
87 break;
89 Q_EMIT paramChanged(xRel, -1, -1, -1, -1, yRel, -1, -1, -1);
90 break;
92 Q_EMIT paramChanged(xRel, -1, -1, -1, -1, -1, -1, yRel, -1);
93 break;
95 Q_EMIT paramChanged(xRel, -1, yRel, -1, -1, -1, -1, -1, -1);
96 break;
98 Q_EMIT paramChanged(xRel, -1, -1, -1, yRel, -1, -1, -1, -1);
99 break;
101 Q_EMIT paramChanged(xRel, -1, -1, -1, -1, -1, yRel, -1, -1);
102 break;
104 Q_EMIT paramChanged(xRel, -1, -1, -1, -1, -1, -1, -1, yRel);
105 break;
106 }
107
108 Q_EMIT update();
109 return colorAt(x, y);
110}
111
113{
114 qreal hsvH, hsvS, hsvV;
115 qreal hslH, hslS, hslL;
116 qreal hsiH, hsiS, hsiI;
117 qreal hsyH, hsyS, hsyY;
118 KConfigGroup cfg = KSharedConfig::openConfig()->group("advancedColorSelector");
119 R = cfg.readEntry("lumaR", 0.2126);
120 G = cfg.readEntry("lumaG", 0.7152);
121 B = cfg.readEntry("lumaB", 0.0722);
122 Gamma = cfg.readEntry("gamma", 2.2);
123 m_parent->converter()->getHsvF(color, &hsvH, &hsvS, &hsvV);
124 m_parent->converter()->getHslF(color, &hslH, &hslS, &hslL);
125 //here we add our converter options
126 m_parent->converter()->getHsiF(color, &hsiH, &hsiS, &hsiI);
127 m_parent->converter()->getHsyF(color, &hsyH, &hsyS, &hsyY, R, G, B, Gamma);
128
129 switch (m_parameter) {
131 m_lastClickPos.setX(hslS);
132 m_lastClickPos.setY(1 - hslL);
133 Q_EMIT paramChanged(-1, -1, -1, hslS, hslL, -1, -1, -1, -1);
134 break;
136 m_lastClickPos.setX(hsiS);
137 m_lastClickPos.setY(1 - hsiI);
138 Q_EMIT paramChanged(-1, -1, -1, -1, -1, hsiS, hsiI, -1, -1);
139 break;
141 m_lastClickPos.setX(hsyS);
142 m_lastClickPos.setY(1 - hsyY);
143 Q_EMIT paramChanged(-1, -1, -1, -1, -1, -1, -1, hsyS, hsyY);
144 break;
146 m_lastClickPos.setX(qBound<qreal>(0., hslH, 1.));
147 m_lastClickPos.setY(1 - hslL);
148 Q_EMIT paramChanged(hslH, -1, -1, -1, hslL, -1, -1, -1, -1);
149 break;
151 m_lastClickPos.setX(hsvS);
152 m_lastClickPos.setY(1 - hsvV);
153 Q_EMIT paramChanged(-1, hsvS, hsvV, -1, -1, -1, -1, -1, -1);
154 break;
156 qreal xRel = hsvS;
157 qreal yRel = 0.5;
158
159 if(xRel != 1.0)
160 yRel = 1.0 - qBound<qreal>(0.0, (hsvV - xRel) / (1.0 - xRel), 1.0);
161
162 m_lastClickPos.setX(xRel);
163 m_lastClickPos.setY(yRel);
164 Q_EMIT paramChanged(-1, -1, -1, xRel, yRel, -1, -1, -1, -1);
165 break;
166 }
168 m_lastClickPos.setX(qBound<qreal>(0., hsvH, 1.));
169 m_lastClickPos.setY(1 - hsvV);
170 Q_EMIT paramChanged(hsvH, -1, hsvV, -1, -1, -1, -1, -1, -1);
171 break;
173 m_lastClickPos.setX(qBound<qreal>(0., hsiH, 1.));
174 m_lastClickPos.setY(1 - hsiI);
175 Q_EMIT paramChanged(hsiH, -1, -1, -1, -1, -1, hsiI, -1, -1);
176 break;
178 m_lastClickPos.setX(qBound<qreal>(0., hsyH, 1.));
179 m_lastClickPos.setY(1 - hsyY);
180 Q_EMIT paramChanged(hsyH, -1, -1, -1, -1, -1, -1, -1, hsyY);
181 break;
183 m_lastClickPos.setX(qBound<qreal>(0., hsvH, 1.));
184 m_lastClickPos.setY(1 - hsvS);
185 Q_EMIT paramChanged(hsvH, hsvS, -1, -1, -1, -1, -1, -1, -1);
186 break;
188 m_lastClickPos.setX(qBound<qreal>(0., hslH, 1.));
189 m_lastClickPos.setY(1 - hslS);
190 Q_EMIT paramChanged(hslH, -1, -1, hslS, -1, -1, -1, -1, -1);
191 break;
192
194 m_lastClickPos.setX(qBound<qreal>(0., hsiH, 1.));
195 m_lastClickPos.setY(1 - hsiS);
196 Q_EMIT paramChanged(hsiH, -1, -1, hsiS, -1, -1, -1, -1, -1);
197 break;
198
200 m_lastClickPos.setX(qBound<qreal>(0., hsyH, 1.));
201 m_lastClickPos.setY(1 - hsyS);
202 Q_EMIT paramChanged(hsyH, -1, -1, -1, -1, -1, -1, hsyS, -1);
203 break;
205 m_lastClickPos.setX(qBound<qreal>(0., hslL, 1.));
206 Q_EMIT paramChanged(-1, -1, -1, -1, hslL, -1, -1, -1, -1);
207 break;
209 m_lastClickPos.setX(qBound<qreal>(0., hsiI, 1.));
210 Q_EMIT paramChanged(-1, -1, -1, -1, -1, -1, hsiI, -1, -1);
211 break;
213 m_lastClickPos.setX(hsvV);
214 Q_EMIT paramChanged(-1, -1, hsvV, -1, -1, -1, -1, -1, -1);
215 break;
217 m_lastClickPos.setX(qBound<qreal>(0., hsyY, 1.));
218 Q_EMIT paramChanged(-1, -1, -1, -1, -1, -1, -1, -1, hsyY);
219 break;
221 m_lastClickPos.setX( hsvS );
222 Q_EMIT paramChanged(-1, hsvS, -1, -1, -1, -1, -1, -1, -1);
223 break;
225 m_lastClickPos.setX( hslS );
226 Q_EMIT paramChanged(-1, -1, -1, hslS, -1, -1, -1, -1, -1);
227 break;
229 m_lastClickPos.setX( hsiS );
230 Q_EMIT paramChanged(-1, -1, -1, -1, -1, hsiS, -1, -1, -1);
231 break;
233 m_lastClickPos.setX( hsyS );
234 Q_EMIT paramChanged(-1, -1, -1, -1, -1, -1, -1, hsyS, -1);
235 break;
237 m_lastClickPos.setX(qBound<qreal>(0., hsvH, 1.));
238 Q_EMIT paramChanged(hsvH, -1, -1, -1, -1, -1, -1, -1, -1);
239 break;
241 m_lastClickPos.setX(qBound<qreal>(0., hsyH, 1.));
242 Q_EMIT paramChanged(hsyH, -1, -1, -1, -1, -1, -1, -1, -1);
243 break;
244 default:
245 Q_ASSERT(false);
246 break;
247 }
248 Q_EMIT update();
249 //Workaround for bug 317648
252}
253
254void KisColorSelectorSimple::paint(QPainter* painter)
255{
256 if(isDirty()) {
257 KisPaintDeviceSP realPixelCache;
258 QPoint pixelCacheOffset;
261 QRect(0, 0, width(), height()),
262 realPixelCache,
264 pixelCacheOffset,
265 painter->device()->devicePixelRatioF());
266
267// if (!pixelCacheOffset.isNull()) {
268// warnKrita << "WARNING: offset of the rectangle selector is not null!";
269// }
270 }
271
272 painter->drawImage(0,0, m_pixelCache);
273
274 // draw blip
275 if(m_lastClickPos!=QPointF(-1,-1) && m_parent->displayBlip()) {
276 switch (m_parameter) {
287 if(width()>height()) {
288 painter->setPen(QColor(0,0,0));
289 painter->drawLine(m_lastClickPos.x()*width()-1, 0, m_lastClickPos.x()*width()-1, height());
290 painter->setPen(QColor(255,255,255));
291 painter->drawLine(m_lastClickPos.x()*width()+1, 0, m_lastClickPos.x()*width()+1, height());
292 }
293 else {
294 painter->setPen(QColor(0,0,0));
295 painter->drawLine(0, m_lastClickPos.x()*height()-1, width(), m_lastClickPos.x()*height()-1);
296 painter->setPen(QColor(255,255,255));
297 painter->drawLine(0, m_lastClickPos.x()*height()+1, width(), m_lastClickPos.x()*height()+1);
298 }
299 break;
313 painter->setPen(QColor(0,0,0));
314 painter->drawEllipse(m_lastClickPos.x()*width()-5, m_lastClickPos.y()*height()-5, 10, 10);
315 painter->setPen(QColor(255,255,255));
316 painter->drawEllipse(m_lastClickPos.x()*width()-4, m_lastClickPos.y()*height()-4, 8, 8);
317 break;
318 }
319
320 }
321}
322
324{
325 qreal xRel = x/qreal(width());
326 qreal yRel = 1.-y/qreal(height());
327 qreal relPos;
328 if(height()>width())
329 relPos = 1.-y/qreal(height());
330 else
331 relPos = x/qreal(width());
332
334
335 switch(m_parameter) {
337 color = m_parent->converter()->fromHslF(m_hue, xRel, yRel);
338 break;
340 color = m_parent->converter()->fromHsvF(m_hue, xRel, yRel);
341 break;
343 color = m_parent->converter()->fromHsvF(m_hue, xRel, xRel + (1.0-xRel)*yRel);
344 break;
346 color = m_parent->converter()->fromHsiF(m_hue, xRel, yRel);
347 break;
349 color = m_parent->converter()->fromHsyF(m_hue, xRel, yRel, R, G, B, Gamma);
350 break;
352 color = m_parent->converter()->fromHsvF(xRel, yRel, m_value);
353 break;
355 color = m_parent->converter()->fromHslF(xRel, yRel, m_lightness);
356 break;
358 color = m_parent->converter()->fromHsiF(xRel, yRel, m_intensity);
359 break;
361 color = m_parent->converter()->fromHsyF(xRel, yRel, m_luma, R, G, B, Gamma);
362 break;
364 color = m_parent->converter()->fromHsvF(xRel, m_hsvSaturation, yRel);
365 break;
367 color = m_parent->converter()->fromHslF(xRel, m_hslSaturation, yRel);
368 break;
370 color = m_parent->converter()->fromHsiF(xRel, m_hsiSaturation, yRel);
371 break;
373 color = m_parent->converter()->fromHsyF(xRel, m_hsySaturation, yRel, R, G, B, Gamma);
374 break;
376 color = m_parent->converter()->fromHsvF(relPos, 1, 1);
377 break;
379 color = m_parent->converter()->fromHsyF(relPos, 1, m_luma, R, G, B, Gamma);
380 break;
382 color = m_parent->converter()->fromHsvF(m_hue, relPos, m_value);
383 break;
385 color = m_parent->converter()->fromHslF(m_hue, relPos, m_lightness);
386 break;
388 color = m_parent->converter()->fromHsvF(m_hue, m_hsvSaturation, relPos);
389 break;
391 color = m_parent->converter()->fromHslF(m_hue, m_hslSaturation, relPos);
392 break;
394 color = m_parent->converter()->fromHsiF(m_hue, relPos, m_intensity);
395 break;
397 color = m_parent->converter()->fromHsiF(m_hue, m_hsiSaturation, relPos);
398 break;
400 color = m_parent->converter()->fromHsyF(m_hue, relPos, m_luma, R, G, B, Gamma);
401 break;
403 color = m_parent->converter()->fromHsyF(m_hue, m_hsySaturation, relPos, R, G, B, Gamma);
404 break;
405 default:
406 Q_ASSERT(false);
407
408 return color;
409 }
410
411 return color;
412}
static void render(Sampler *sampler, const KisDisplayColorConverter *converter, const QRect &sampleRect, KisPaintDeviceSP &realPixelCache, QImage &pixelCache, QPoint &pixelCacheOffset, qreal devicePixelRatioF)
KisDisplayColorConverter * converter() const
const KoColorSpace * colorSpace() const
void update()
request for repaint, for instance, if the hue changes.
bool isDirty() const
returns true, if ether the color space, the size or the parameters have changed since the last paint ...
virtual void setColor(const KoColor &color)
set the color, blibs etc
void paramChanged(qreal hue, qreal hsvSaturation, qreal value, qreal hslSaturation, qreal lightness, qreal hsiSaturation, qreal intensity, qreal hsySaturation, qreal luma)
-1, if unaffected
void paint(QPainter *) override
void setColor(const KoColor &color) override
set the color, blibs etc
KoColor colorAt(float x, float y)
KoColor selectColor(int x, int y) override
this method must be overloaded to return the color at position x/y and draw a marker on that position
KisColorSelectorSimple(KisColorSelector *parent)
bool displayBlip() const
void getHslF(const KoColor &srcColor, qreal *h, qreal *s, qreal *l, qreal *a=0)
KoColor fromHsiF(qreal h, qreal s, qreal i)
void getHsiF(const KoColor &srcColor, qreal *h, qreal *s, qreal *i)
void getHsyF(const KoColor &srcColor, qreal *h, qreal *s, qreal *y, qreal R=0.2126, qreal G=0.7152, qreal B=0.0722, qreal gamma=2.2)
KoColor fromHslF(qreal h, qreal s, qreal l, qreal a=1.0)
void getHsvF(const KoColor &srcColor, qreal *h, qreal *s, qreal *v, qreal *a=0)
KoColor fromHsyF(qreal h, qreal s, qreal y, qreal R=0.2126, qreal G=0.7152, qreal B=0.0722, qreal gamma=2.2)
KoColor fromHsvF(qreal h, qreal s, qreal v, qreal a=1.0)
static KoColor createTransparent(const KoColorSpace *cs)
Definition KoColor.cpp:681