Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_color_selector_triangle.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
9#include <QPainter>
10#include <QMouseEvent>
11
12#include <cmath>
13
14#include "KoColorSpace.h"
15
18
19
25
27{
28 QPoint triangleCoords = widgetToTriangleCoordinates(QPoint(x, y))*m_cacheDevicePixelRatioF;
29
30 if (!m_realPixelCache) return false;
31 KoColor pixel = Acs::sampleColor(m_realPixelCache, triangleCoords);
32 return pixel.opacityU8() == OPACITY_OPAQUE_U8;
33}
34
35void KisColorSelectorTriangle::paint(QPainter* painter)
36{
37 if(isDirty()) {
38 updatePixelCache(painter->device()->devicePixelRatioF());
39 }
40
41
42 painter->drawImage(width()/2-triangleWidth()/2,
43 height()/2-triangleHeight()*(2/3.),
45
46
47 if(m_lastClickPos.x()>-0.1 && m_parent->displayBlip()) {
48 painter->setPen(QColor(0,0,0));
49 painter->drawEllipse(m_lastClickPos.x()*width()-5, m_lastClickPos.y()*height()-5, 10, 10);
50 painter->setPen(QColor(255,255,255));
51 painter->drawEllipse(m_lastClickPos.x()*width()-4, m_lastClickPos.y()*height()-4, 8, 8);
52 }
53}
54
55void KisColorSelectorTriangle::updatePixelCache(qreal devicePixelRatioF)
56{
57 int width = triangleWidth() + 1;
58 int height = triangleHeight();
59
60 QPoint pixelCacheOffset;
61
62 if (m_cachedSize != QSize(width, height) && m_realPixelCache) {
64 }
65
68 QRect(0, 0, width, height),
71 pixelCacheOffset,
72 devicePixelRatioF);
73
74 m_cacheDevicePixelRatioF = devicePixelRatioF; // save device pixel ratio of the cache
75
76// if (!pixelCacheOffset.isNull()) {
77// warnKrita << "WARNING: offset of the triangle selector is not null!";
78// }
79
80 // antialiased border
81 QPainter gc(&m_renderedPixelCache);
82 gc.setRenderHint(QPainter::Antialiasing);
83 gc.setPen(QPen(QColor(0,0,0,128), 2.5));
84 gc.setCompositionMode(QPainter::CompositionMode_Clear);
85 gc.drawLine(QPointF(0, triangleHeight()), QPointF((triangleWidth()) / 2.0, 0));
86 gc.drawLine(QPointF(triangleWidth() / 2.0 + 1.0, 0), QPointF(triangleWidth() + 1, triangleHeight()));
87}
88
90{
91 Q_EMIT update();
92
93 QPoint triangleCoords = widgetToTriangleCoordinates(QPoint(x,y));
94
95 triangleCoords.setY(qBound(0, triangleCoords.y(), int(triangleHeight())));
96
97 int horizontalLineLength = triangleCoords.y()*(2./sqrt(3.));
98 int horizontalLineStart = triangleWidth()/2.-horizontalLineLength/2.;
99 int horizontalLineEnd = horizontalLineStart+horizontalLineLength;
100
101 triangleCoords.setX(qBound(horizontalLineStart, triangleCoords.x(), horizontalLineEnd));
102
103 QPoint widgetCoords = triangleToWidgetCoordinates(triangleCoords);
104
105 m_lastClickPos.setX(widgetCoords.x()/qreal(width()));
106 m_lastClickPos.setY(widgetCoords.y()/qreal(height()));
107
108 return colorAt(triangleCoords.x(), triangleCoords.y());
109}
110
112{
113 qreal h, s, v;
114 m_parent->converter()->getHsvF(color, &h, &s, &v);
115
116 qreal y = v * triangleHeight();
117 qreal horizontalLineLength = y * (2. / sqrt(3.));
118 qreal horizontalLineStart = 0.5 * (triangleWidth() - horizontalLineLength);
119 qreal x=s * horizontalLineLength + horizontalLineStart;
120
121 QPoint tmp = triangleToWidgetCoordinates(QPoint(x, y));
122
123 m_lastClickPos.setX(tmp.x()/qreal(width()));
124 m_lastClickPos.setY(tmp.y()/qreal(height()));
125
126 // Workaround for Bug 287001
127 setLastMousePosition(tmp.x(), tmp.y());
128
129 Q_EMIT paramChanged(-1, s, v, -1, -1, -1, -1, -1, -1);
130 Q_EMIT update();
132}
133
135{
136 return triangleHeight()*2/sqrt(3.0);
137}
138
140{
141 return height()*3./4.;
142}
143
145{
146 Q_ASSERT(x>=0 && x<=(triangleWidth() + 1));
147 Q_ASSERT(y>=0 && y<=triangleHeight());
148
149 int triangleHeight = this->triangleHeight();
150 int horizontalLineLength = y*(2./sqrt(3.));
151 int horizontalLineStart = triangleWidth()/2.-horizontalLineLength/2.;
152 int horizontalLineEnd = horizontalLineStart+horizontalLineLength;
153
154 if(x<horizontalLineStart || x>horizontalLineEnd || y>triangleHeight)
156
157 // if horizontalLineLength is 0, then saturation is improperly calculated
158 if(horizontalLineLength == 0)
159 return m_parent->converter()->fromHsvF(m_hue, 0.0, 0.0);
160
161 qreal relativeX = x-horizontalLineStart;
162
163 qreal value = (y)/qreal(triangleHeight);
164 qreal saturation = relativeX/qreal(horizontalLineLength);
165
166 return m_parent->converter()->fromHsvF(m_hue, saturation, value);
167}
168
170{
171 QPoint triangleTopLeft(width()/2-triangleWidth()/2,
172 height()/2-triangleHeight()*(2/3.));
173 QPoint ret=point-triangleTopLeft;
174 return ret;
175}
176
178{
179 QPoint triangleTopLeft(width()/2.-triangleWidth()/2.,
180 height()/2.-triangleHeight()*(2./3.));
181 QPoint ret=triangleTopLeft+point;
182 return ret;
183}
184
float value(const T *src, size_t ch)
qreal v
const quint8 OPACITY_OPAQUE_U8
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
bool containsPointInComponentCoords(int x, int y) const override
KisColorSelectorTriangle(KisColorSelector *parent)
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
QPoint widgetToTriangleCoordinates(const QPoint &point) const
QPoint triangleToWidgetCoordinates(const QPoint &point) const
KoColor colorAt(float x, float y) const
void updatePixelCache(qreal devicePixelRatioF)
void setColor(const KoColor &color) override
set the color, blibs etc
bool displayBlip() const
void getHsvF(const KoColor &srcColor, qreal *h, qreal *s, qreal *v, qreal *a=0)
KoColor fromHsvF(qreal h, qreal s, qreal v, qreal a=1.0)
static KoColor createTransparent(const KoColorSpace *cs)
Definition KoColor.cpp:682
quint8 opacityU8() const
Definition KoColor.cpp:341
KoColor sampleColor(PaintDeviceSP device, const QPoint &pt)