forked from flameshot-org/flameshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorpickerwidget.cpp
More file actions
157 lines (137 loc) · 5.19 KB
/
colorpickerwidget.cpp
File metadata and controls
157 lines (137 loc) · 5.19 KB
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
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2022 Dearsh Oberoi
#include "colorpickerwidget.h"
#include "src/utils/confighandler.h"
#include "src/utils/globalvalues.h"
#include <QMouseEvent>
#include <QPainter>
ColorPickerWidget::ColorPickerWidget(QWidget* parent)
: QWidget(parent)
, m_selectedIndex(1)
, m_lastIndex(1)
{
initColorPicker();
}
const QVector<QColor>& ColorPickerWidget::getDefaultSmallColorPalette()
{
return defaultSmallColorPalette;
}
const QVector<QColor>& ColorPickerWidget::getDefaultLargeColorPalette()
{
return defaultLargeColorPalette;
}
void ColorPickerWidget::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QColor(Qt::black));
for (int i = 0; i < m_colorAreaList.size(); ++i) {
if (e->region().contains(m_colorAreaList.at(i))) {
painter.setClipRegion(e->region());
repaint(i, painter);
}
}
}
void ColorPickerWidget::repaint(int i, QPainter& painter)
{
// draw the highlight when we have to draw the selected color
if (i == m_selectedIndex) {
auto c = QColor(m_uiColor);
c.setAlpha(155);
painter.setBrush(c);
c.setAlpha(100);
painter.setPen(c);
QRect highlight = m_colorAreaList.at(i);
const int highlightThickness = 6;
// makes the highlight and color circles concentric
highlight.moveTo(highlight.x() - (highlightThickness / 2),
highlight.y() - (highlightThickness / 2));
highlight.setHeight(highlight.height() + highlightThickness);
highlight.setWidth(highlight.width() + highlightThickness);
painter.drawRoundedRect(highlight, 100, 100);
painter.setPen(QColor(Qt::black));
}
// draw available colors
if (m_colorList.at(i).isValid()) {
// draw preset color
painter.setBrush(QColor(m_colorList.at(i)));
painter.drawRoundedRect(m_colorAreaList.at(i), 100, 100);
} else {
// draw rainbow (part) for custom color
QRect lastRect = m_colorAreaList.at(i);
int nStep = 1;
int nSteps = lastRect.height() / nStep;
// 0.02 - start rainbow color, 0.33 - end rainbow color from range:
// 0.0 - 1.0
float h = 0.02;
for (int radius = nSteps; radius > 0; radius -= nStep * 2) {
// calculate color
float fHStep = (0.33 - h) / (nSteps / nStep / 2);
QColor color = QColor::fromHslF(h, 0.95, 0.5);
// set color and draw circle
painter.setPen(color);
painter.setBrush(color);
painter.drawRoundedRect(lastRect, 100, 100);
// set next color, circle geometry
h += fHStep;
lastRect.setX(lastRect.x() + nStep);
lastRect.setY(lastRect.y() + nStep);
lastRect.setHeight(lastRect.height() - nStep);
lastRect.setWidth(lastRect.width() - nStep);
painter.setPen(QColor(Qt::black));
}
}
}
void ColorPickerWidget::updateSelection(int index)
{
m_selectedIndex = index;
update(m_colorAreaList.at(index) + QMargins(10, 10, 10, 10));
update(m_colorAreaList.at(m_lastIndex) + QMargins(10, 10, 10, 10));
m_lastIndex = index;
}
void ColorPickerWidget::updateWidget()
{
m_colorAreaList.clear();
initColorPicker();
update();
}
void ColorPickerWidget::initColorPicker()
{
ConfigHandler config;
m_colorList = config.userColors();
m_colorAreaSize = GlobalValues::buttonBaseSize() * 0.6;
// save the color values in member variables for faster access
m_uiColor = config.uiColor();
// extraSize represents the extra space needed for the highlight of the
// selected color.
const int extraSize = 6;
const double slope = 3;
double radius = slope * m_colorList.size() + GlobalValues::buttonBaseSize();
setMinimumSize(radius * 2 + m_colorAreaSize + extraSize,
radius * 2 + m_colorAreaSize + extraSize);
resize(radius * 2 + m_colorAreaSize + extraSize,
radius * 2 + m_colorAreaSize + extraSize);
double degree = (double)360 / m_colorList.size();
double degreeAcum = 90;
// this line is the radius of the circle which will be rotated to add
// the color components.
QLineF baseLine =
QLineF(QPoint(radius + extraSize / 2, radius + extraSize / 2),
QPoint(radius + extraSize / 2, extraSize / 2));
for (int i = 0; i < m_colorList.size(); ++i) {
m_colorAreaList.append(QRect(
baseLine.x2(), baseLine.y2(), m_colorAreaSize, m_colorAreaSize));
degreeAcum += degree;
baseLine.setAngle(degreeAcum);
}
}
QVector<QColor> ColorPickerWidget::defaultSmallColorPalette = {
QColor(), Qt::darkRed, Qt::red, Qt::yellow, Qt::green,
Qt::darkGreen, Qt::cyan, Qt::blue, Qt::magenta, Qt::darkMagenta
};
QVector<QColor> ColorPickerWidget::defaultLargeColorPalette = {
QColor(), Qt::white, Qt::red, Qt::green, Qt::blue,
Qt::black, Qt::darkRed, Qt::darkGreen, Qt::darkBlue, Qt::darkGray,
Qt::cyan, Qt::magenta, Qt::yellow, Qt::lightGray, Qt::darkCyan,
Qt::darkMagenta, Qt::darkYellow
};