swift
Loading...
Searching...
No Matches
rgbcolor.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "misc/rgbcolor.h"
5
6#include <QBrush>
7#include <QPainter>
8#include <QPixmap>
9#include <QSize>
10#include <QStringBuilder>
11#include <QtGlobal>
12
14#include "misc/icons.h"
16#include "misc/stringutils.h"
17
18using namespace swift::misc;
19
21
22namespace swift::misc
23{
24 CRgbColor::CRgbColor(const QString &color, bool isName) { this->setByString(color, isName); }
25
26 CRgbColor::CRgbColor(int r, int g, int b) : m_r(r), m_g(g), m_b(b) {}
27
28 CRgbColor::CRgbColor(const QColor &color) { this->setQColor(color); }
29
31 {
32 QPixmap pixmap(QSize(16, 16));
33 QPainter p(&pixmap);
34 p.setBackground(QBrush(this->toQColor()));
35 p.setBrush(this->toQColor());
36 p.drawRect(0, 0, 16, 16);
37 return pixmap;
38 }
39
41 {
42 // if (this->isValid())
43 //{
44 // return CIcon(toPixmap(), hex());
45 // }
46 // else
47 {
48 return CIcons::StandardIconError16;
49 }
50 }
51
52 QColor CRgbColor::toQColor() const { return { red(), green(), blue() }; }
53
54 bool CRgbColor::setQColor(const QColor &color)
55 {
56 if (color.isValid())
57 {
58 m_r = color.red();
59 m_g = color.green();
60 m_b = color.blue();
61 return true;
62 }
63 else
64 {
65 this->setInvalid();
66 return false;
67 }
68 }
69
70 int CRgbColor::red() const { return m_r; }
71
73 {
74 double c = red() * 1.0;
75 return c / colorRange();
76 }
77
78 QString CRgbColor::redHex(int digits) const { return intToHex(m_r, digits); }
79
80 int CRgbColor::green() const { return m_g; }
81
83 {
84 double c = green() * 1.0;
85 return c / colorRange();
86 }
87
88 QString CRgbColor::greenHex(int digits) const { return intToHex(m_g, digits); }
89
90 int CRgbColor::blue() const { return m_b; }
91
93 {
94 double c = blue() * 1.0;
95 return c / colorRange();
96 }
97
98 QString CRgbColor::blueHex(int digits) const { return intToHex(m_b, digits); }
99
100 QString CRgbColor::hex(bool withHash) const
101 {
102 if (!isValid())
103 {
104 return {};
105 }
106 const QString h(redHex() + greenHex() + blueHex());
107 return withHash ? u'#' % h : h;
108 }
109
111 {
112 if (!isValid())
113 {
114 return 0;
115 }
116 return static_cast<int>(toQColor().rgb() & qRgba(255, 255, 255, 0));
117 }
118
119 CRgbColor CRgbColor::fromPacked(int rgb) { return { qRed(rgb), qGreen(rgb), qBlue(rgb) }; }
120
121 void CRgbColor::setByString(const QString &color, bool isName)
122 {
123 if (color.isEmpty())
124 {
125 return;
126 }
127
128 if (isName)
129 {
130 const QColor q(color);
131 m_r = q.red();
132 m_g = q.green();
133 m_b = q.blue();
134 }
135 else
136 {
137 const QString c(color.trimmed());
138 QColor q(c);
139 if (this->setQColor(q))
140 {
141 return;
142 }
143 if (c.startsWith("#"))
144 {
145 this->setInvalid();
146 return;
147 }
148 q = QColor::fromString("#" + c);
149 this->setQColor(q);
150 }
151 }
152
153 bool CRgbColor::isValid() const { return m_r >= 0 && m_g >= 0 && m_b >= 0; }
154
155 double CRgbColor::colorDistance(const CRgbColor &color) const
156 {
157 if (!this->isValid() && !color.isValid())
158 {
159 return 0;
160 }
161 if (!this->isValid() || !color.isValid())
162 {
163 return 1;
164 }
165 if (*this == color)
166 {
167 return 0.0;
168 } // avoid rounding
169
170 // all values 0-1
171 const double rd = (normalizedRed() - color.normalizedRed());
172 const double bd = (normalizedBlue() - color.normalizedBlue());
173 const double gd = (normalizedGreen() - color.normalizedGreen());
174 return (rd * rd + bd * bd + gd * gd) / 3.0;
175 }
176
178 {
179 m_r = -1;
180 m_g = -1;
181 m_b = -1;
182 }
183
185 {
186 Q_UNUSED(i18n);
187 return this->hex();
188 }
189
191 {
192 if (index.isMyself())
193 {
194 return QVariant::fromValue(*this);
195 }
196 const auto i = index.frontCasted<ColumnIndex>();
197 switch (i)
198 {
199 case IndexBlue: return QVariant::fromValue(blue());
200 case IndexRed: return QVariant::fromValue(red());
201 case IndexGreen: return QVariant::fromValue(green());
202 case IndexWebHex: return QVariant::fromValue(hex());
203 default: return CValueObject::propertyByIndex(index);
204 }
205 }
206
208 {
209 if (index.isMyself())
210 {
211 (*this) = variant.value<CRgbColor>();
212 return;
213 }
214 const auto i = index.frontCasted<ColumnIndex>();
215 switch (i)
216 {
217 case IndexBlue: m_b = variant.toInt(); break;
218 case IndexRed: m_r = variant.toInt(); break;
219 case IndexGreen: m_g = variant.toInt(); break;
220 case IndexWebHex: this->setByString(variant.toString()); break;
221 default: CValueObject::setPropertyByIndex(index, variant); break;
222 }
223 }
224
226 {
227 if (index.isMyself())
228 {
229 return this->compare(compareValue);
230 }
231 const auto i = index.frontCasted<ColumnIndex>();
232 switch (i)
233 {
234 case IndexBlue: return Compare::compare(m_b, compareValue.m_b);
235 case IndexRed: return Compare::compare(m_r, compareValue.m_r);
236 case IndexGreen: return Compare::compare(m_g, compareValue.m_g);
237 case IndexWebHex: return this->compare(compareValue);
238 default: Q_ASSERT_X(false, Q_FUNC_INFO, "Missing compare"); break;
239 }
240 return 0;
241 }
242
243 int CRgbColor::compare(const CRgbColor &color) const
244 {
245 int c = Compare::compare(m_r, color.m_r);
246 if (c != 0)
247 {
248 return c;
249 }
250 c = Compare::compare(m_g, color.m_g);
251 if (c != 0)
252 {
253 return c;
254 }
255 return Compare::compare(m_b, color.m_b);
256 }
257
258 double CRgbColor::colorRange() const
259 {
260 if (!this->isValid())
261 {
262 return 255;
263 }
264 if (m_b < 256 && m_g < 256 && m_r < 256)
265 {
266 return 255;
267 }
268 if (m_b < 4096 && m_g < 4096 && m_r < 4096)
269 {
270 return 4095;
271 }
272 return 65535;
273 }
274
275 QString CRgbColor::intToHex(int h, int digits) { return swift::misc::intToHex(h, digits); }
276
277} // namespace swift::misc
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition icons.h:32
Non-owning reference to a CPropertyIndex with a subset of its features.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
QString blueHex(int digits=2) const
Blue as hex.
Definition rgbcolor.cpp:98
ColumnIndex
Properties by index.
Definition rgbcolor.h:31
double normalizedBlue() const
Blue 0..1.
Definition rgbcolor.cpp:92
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
Definition rgbcolor.cpp:190
QString hex(bool withHash=false) const
Hex value.
Definition rgbcolor.cpp:100
void setInvalid()
Mark as invalid.
Definition rgbcolor.cpp:177
int green() const
Green.
Definition rgbcolor.cpp:80
int blue() const
Blue.
Definition rgbcolor.cpp:90
QPixmap toPixmap() const
Icon as pixmap.
Definition rgbcolor.cpp:30
QColor toQColor() const
To QColor.
Definition rgbcolor.cpp:52
bool setQColor(const QColor &color)
Set by QColor.
Definition rgbcolor.cpp:54
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition rgbcolor.cpp:184
int packed() const
Value packed in 24 bit integer.
Definition rgbcolor.cpp:110
static CRgbColor fromPacked(int rgb)
Construct from packed 24 bit integer value.
Definition rgbcolor.cpp:119
double normalizedRed() const
Red 0..1.
Definition rgbcolor.cpp:72
double colorDistance(const CRgbColor &color) const
Color distance [0..1] http://stackoverflow.com/questions/4754506/color-similarity-distance-in-rgba-co...
Definition rgbcolor.cpp:155
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition rgbcolor.cpp:207
void setByString(const QString &color, bool isName=false)
Hex or color name.
Definition rgbcolor.cpp:121
int comparePropertyByIndex(CPropertyIndexRef index, const CRgbColor &compareValue) const
Compare for index.
Definition rgbcolor.cpp:225
int red() const
Red.
Definition rgbcolor.cpp:70
double normalizedGreen() const
Green 0..1.
Definition rgbcolor.cpp:82
QString redHex(int digits=2) const
Red as hex.
Definition rgbcolor.cpp:78
CRgbColor()=default
Constructor.
int compare(const CRgbColor &color) const
Compare with other color.
Definition rgbcolor.cpp:243
QString greenHex(int digits=2) const
Green as hex.
Definition rgbcolor.cpp:88
CIcons::IconIndex toIcon() const
Representing icon.
Definition rgbcolor.cpp:40
bool isValid() const
Valid?
Definition rgbcolor.cpp:153
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition mixinindex.h:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
int compare(T a, T b)
Compare arithmetic values.
Free functions in swift::misc.
SWIFT_MISC_EXPORT QString intToHex(int value, int digits=2)
Int to hex value.
int blue() const const
QColor fromString(QAnyStringView name)
int green() const const
bool isValid() const const
int red() const const
QRgb rgb() const const
void drawRect(const QRect &rectangle)
void setBackground(const QBrush &brush)
void setBrush(QBrush &&brush)
bool isEmpty() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString trimmed() const const
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
QString toString() const const
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67