-
Notifications
You must be signed in to change notification settings - Fork 3
/
colorwheel.cpp
275 lines (206 loc) · 8.88 KB
/
colorwheel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "colorwheel.h"
#include <QDebug>
#include <QPainter>
namespace {
constexpr qreal ONETURN { 360.0 };
}
ColorWheel::ColorWheel( QQuickItem* parent )
: QQuickPaintedItem( parent )
, m_quadHit { UpDown::UP }
, m_hitMode { HitPosition::IDLE }
, m_color { Qt::red }
, m_arrow { QPointF( 0, 0 ), QPointF( 0, 0 ), QPointF( 0, 0 ) }
, m_outerRadius { 1 }
, m_innerRadius { 0 }
, m_indicatorSize { 0 } {
setAcceptedMouseButtons( Qt::AllButtons );
setAcceptHoverEvents( true );
// Wheel gradient
m_wheelGradient.setAngle( 0.0 );
m_wheelGradient.setColorAt( 0.0, Qt::red );
m_wheelGradient.setColorAt( 60.0 / ONETURN, Qt::yellow );
m_wheelGradient.setColorAt( 120.0 / ONETURN, Qt::green );
m_wheelGradient.setColorAt( 180.0 / ONETURN, Qt::cyan );
m_wheelGradient.setColorAt( 240.0 / ONETURN, Qt::blue );
m_wheelGradient.setColorAt( 300.0 / ONETURN, Qt::magenta );
m_wheelGradient.setColorAt( 1.0, Qt::red );
connect( this, &ColorWheel::colorChanged, this, [&] { update(); } );
}
void ColorWheel::paint( QPainter* painter ) {
// *****************************
// Wheel
// *****************************
QPainterPath path;
painter->translate( width() / 2, height() / 2 );
painter->setRenderHints( QPainter::Antialiasing, true );
painter->setPen( Qt::NoPen );
painter->setBrush( m_wheelGradient );
path.addEllipse( QPointF( 0, 0 ), m_innerRadius, m_innerRadius );
path.addEllipse( QPointF( 0, 0 ), m_outerRadius, m_outerRadius );
painter->drawPath( path );
// *****************************
// Chooser
// *****************************
QLinearGradient colorGradientSaturation { QPointF( 0, 0 ), QPointF( 1, 0 ) };
colorGradientSaturation.setCoordinateMode( QGradient::ObjectMode );
colorGradientSaturation.setColorAt( 0, QColor::fromHsvF( m_color.hueF(), 0.0, 1.0, 1.0 ) );
colorGradientSaturation.setColorAt( 1, QColor::fromHsvF( m_color.hueF(), 1.0, 1.0, 1.0 ) );
QLinearGradient colorGradientValue { QPointF( 0, 0 ), QPointF( 0, 1 ) };
colorGradientValue.setCoordinateMode( QGradient::ObjectMode );
colorGradientValue.setColorAt( 0, Qt::transparent );
colorGradientValue.setColorAt( 1, Qt::black );
const qreal localChooserWidth { m_chooserSize.width() };
const qreal localChooserHeight { m_chooserSize.height() };
const QPointF translateChooser { -( localChooserWidth / 2.0 ), -( localChooserHeight / 2.0 ) };
painter->setBrush( colorGradientSaturation );
painter->drawRect( QRectF( translateChooser, QSizeF( localChooserWidth, localChooserHeight ) ) );
painter->setPen( Qt::SolidLine );
painter->setBrush( colorGradientValue );
painter->drawRect( QRectF( translateChooser, QSizeF( localChooserWidth, localChooserHeight ) ) );
// *****************************
// Wheel Indicator
// *****************************
// Rotate Indicator
painter->save();
painter->rotate( -m_color.hsvHue() );
// Draw wheel indicator
painter->setPen( QPen( Qt::NoPen ) );
painter->setBrush( Qt::black );
painter->drawConvexPolygon( m_arrow );
painter->setPen( QPen( Qt::black, 1 ) );
painter->drawLine( QPointF( m_innerRadius + 1.0, 0 ), QPointF( m_outerRadius, 0 ) );
painter->restore();
// *****************************
// Chooser Indicator
// *****************************
// Draw chooser indicator
const QPointF indicatorPosition = saturationValueFromColor( m_color );
painter->drawEllipse( indicatorPosition, m_indicatorSize, m_indicatorSize );
painter->setBrush( m_color );
painter->setPen( QPen( Qt::white, 1, Qt::SolidLine ) );
painter->drawEllipse( indicatorPosition, m_indicatorSize - 1.0, m_indicatorSize - 1.0 );
}
void ColorWheel::setHue( qreal value ) {
m_color = QColor::fromHsvF( static_cast<float>( value ), m_color.saturationF(), m_color.valueF(), m_color.alphaF() );
Q_EMIT colorChanged();
}
// *****************************
// PRIVATE
// *****************************
bool ColorWheel::isHitMode() noexcept {
if ( static_cast<qreal>( m_mouseVec.length() ) > m_innerRadius && static_cast<qreal>( m_mouseVec.length() ) < m_outerRadius ) {
m_hitMode = HitPosition::WHEEL;
return true;
}
if ( m_chooserSize.contains( static_cast<qreal>( m_mouseVec.x() ), static_cast<qreal>( m_mouseVec.y() ) ) ) {
m_hitMode = HitPosition::CHOOSER;
return true;
}
return false;
}
QColor ColorWheel::hueAt( const QVector2D in_mouseVec ) noexcept {
const QVector2D vec { 1.0, 0.0 };
qreal angle = qRadiansToDegrees( std::acos( QVector2D::dotProduct( vec, in_mouseVec ) / ( in_mouseVec.length() * vec.length() ) ) );
m_quadHit
= getQuadrant( QPoint( static_cast<int>( mapFromGlobal( QCursor::pos() ).x() ), static_cast<int>( mapFromGlobal( QCursor::pos() ).y() ) ) );
if ( m_quadHit == UpDown::DOWN ) {
angle = ONETURN - angle;
}
return QColor::fromHsvF( static_cast<float>( angle / ONETURN ), m_color.hsvSaturationF(), m_color.valueF(), m_color.alphaF() );
}
QColor ColorWheel::saturationValuePositionLimit( const QVector2D position ) noexcept {
qreal x { static_cast<qreal>( position.x() ) };
qreal y { static_cast<qreal>( -position.y() ) };
if ( m_hitMode == HitPosition::CHOOSER ) {
x = std::clamp( x, m_chooserSize.left(), m_chooserSize.right() );
y = std::clamp( y, m_chooserSize.top(), m_chooserSize.bottom() );
}
const float m_sat = static_cast<float>( x / m_chooserSize.width() ) + 0.5F;
const float m_val = static_cast<float>( y / m_chooserSize.height() ) + 0.5F;
return QColor::fromHsvF( m_color.hsvHueF(), m_sat, m_val, m_color.alphaF() );
}
QPointF ColorWheel::saturationValueFromColor( const QColor& color ) noexcept {
return { static_cast<float>( m_chooserSize.width() * ( color.saturationF() - 0.5F ) ),
static_cast<float>( -m_chooserSize.height() * ( color.valueF() - 0.5F ) ) };
}
ColorWheel::UpDown ColorWheel::getQuadrant( const QPoint position ) noexcept {
return ( position.y() <= static_cast<int>( height() / 2 ) ) ? UpDown::UP : UpDown::DOWN;
}
void ColorWheel::updateMousePosition( const QPoint position ) {
m_mouseVec = QVector2D( static_cast<float>( position.x() - width() / 2.0F ), static_cast<float>( position.y() - height() / 2.0F ) );
}
// *****************************
// PROTECTED
// *****************************
void ColorWheel::mousePressEvent( QMouseEvent* event ) {
if ( event->buttons() != Qt::LeftButton ) {
return;
}
updateMousePosition( event->pos() );
if ( !isHitMode() ) {
return;
}
switch ( m_hitMode ) {
case HitPosition::WHEEL:
m_color = hueAt( m_mouseVec );
setCursor( Qt::ClosedHandCursor );
break;
case HitPosition::CHOOSER:
m_color = saturationValuePositionLimit( m_mouseVec );
setCursor( Qt::BlankCursor );
break;
default:
break;
}
Q_EMIT colorChanged();
}
void ColorWheel::mouseMoveEvent( QMouseEvent* event ) {
if ( event->buttons() != Qt::LeftButton ) {
return;
}
if ( m_hitMode == HitPosition::IDLE ) {
return;
}
updateMousePosition( event->pos() );
switch ( m_hitMode ) {
case HitPosition::WHEEL:
m_color = hueAt( m_mouseVec );
break;
case HitPosition::CHOOSER:
m_color = saturationValuePositionLimit( m_mouseVec );
break;
default:
break;
}
Q_EMIT colorChanged();
}
void ColorWheel::mouseReleaseEvent( QMouseEvent* /*event*/ ) {
if ( m_hitMode != HitPosition::IDLE ) {
Q_EMIT editFinished();
}
m_hitMode = HitPosition::IDLE;
setCursor( Qt::ArrowCursor );
}
void ColorWheel::geometryChange( const QRectF& newGeometry, const QRectF& /*oldGeometry*/ ) {
const qreal side = std::fmin( newGeometry.width(), newGeometry.height() ) / 2;
m_outerRadius = side - 1.0;
m_innerRadius = side - ( side * 0.25 );
// Make the arrow points relative to widget size
constexpr float scaleFactor { 0.01F };
m_arrow[0] = QPointF( m_innerRadius, 3 * ( side * scaleFactor ) );
m_arrow[1] = QPointF( m_innerRadius + 6 * ( side * scaleFactor ), 0 );
m_arrow[2] = QPointF( m_innerRadius, -3 * ( side * scaleFactor ) );
// Calculate chooser indicator size
m_indicatorSize = m_innerRadius * 0.04;
if ( m_indicatorSize < 1.0 ) {
m_indicatorSize = 1.0;
}
// Calculate the size for chooser
const double diagonal = std::cos( qDegreesToRadians( 45.0 ) ) * m_innerRadius;
const double gap = ( diagonal * 0.05 );
const double diagonalMin = -diagonal + gap;
const double diagonalMax = diagonal - gap;
// Setup the rect size for color sampler
m_chooserSize.setTopLeft( QPointF( diagonalMin, diagonalMin ) );
m_chooserSize.setBottomRight( QPointF( diagonalMax, diagonalMax ) );
}