summaryrefslogtreecommitdiffstats
path: root/src/gui/painting/qcolor.h
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2019-06-12 01:37:05 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2019-06-12 19:55:17 +0200
commitde82d239f814cf2a717bea0defeee3732384e271 (patch)
tree2da5d584adf618b6401ba733e873cb06b1a95d1e /src/gui/painting/qcolor.h
parent166753d8e00e11d2fa92d3bfbe5667ad4b8f7b9d (diff)
Make QColor a literal type
Extracted from the SVG names patch. It basically just requires adding a constexpr constructor to the inner union, then sprinkling constexpr on the existing ones. Do minor refactorings as drive-by. Change-Id: I60e7a1c9068def3507cb07440450e51673269f84 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/gui/painting/qcolor.h')
-rw-r--r--src/gui/painting/qcolor.h42
1 files changed, 25 insertions, 17 deletions
diff --git a/src/gui/painting/qcolor.h b/src/gui/painting/qcolor.h
index e3c267f97d..723b9fce73 100644
--- a/src/gui/painting/qcolor.h
+++ b/src/gui/painting/qcolor.h
@@ -67,9 +67,16 @@ public:
enum Spec { Invalid, Rgb, Hsv, Cmyk, Hsl, ExtendedRgb };
enum NameFormat { HexRgb, HexArgb };
- inline QColor() noexcept;
+ Q_DECL_CONSTEXPR QColor() noexcept
+ : cspec(Invalid), ct(USHRT_MAX, 0, 0, 0, 0) {}
QColor(Qt::GlobalColor color) noexcept;
- inline QColor(int r, int g, int b, int a = 255);
+ Q_DECL_CONSTEXPR QColor(int r, int g, int b, int a = 255) noexcept
+ : cspec(isRgbaValid(r, g, b, a) ? Rgb : Invalid),
+ ct(cspec == Rgb ? a * 0x0101 : 0,
+ cspec == Rgb ? r * 0x0101 : 0,
+ cspec == Rgb ? g * 0x0101 : 0,
+ cspec == Rgb ? b * 0x0101 : 0,
+ 0) {}
QColor(QRgb rgb) noexcept;
QColor(QRgba64 rgba64) noexcept;
#if QT_STRINGVIEW_LEVEL < 2
@@ -81,8 +88,11 @@ public:
QColor(Spec spec) noexcept;
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
- inline QColor(const QColor &color) noexcept; // ### Qt 6: remove all of these, the trivial ones are fine.
- QColor(QColor &&other) noexcept : cspec(other.cspec), ct(other.ct) {}
+ // ### Qt 6: remove all of these, the trivial ones are fine.
+ Q_DECL_CONSTEXPR QColor(const QColor &color) noexcept
+ : cspec(color.cspec), ct(color.ct)
+ {}
+ Q_DECL_CONSTEXPR QColor(QColor &&other) noexcept : cspec(other.cspec), ct(other.ct) {}
QColor &operator=(QColor &&other) noexcept
{ cspec = other.cspec; ct = other.ct; return *this; }
QColor &operator=(const QColor &) noexcept;
@@ -244,8 +254,18 @@ private:
template <typename String>
bool setColorFromString(String name);
+ static Q_DECL_CONSTEXPR bool isRgbaValid(int r, int g, int b, int a = 255) noexcept Q_DECL_CONST_FUNCTION
+ {
+ return uint(r) <= 255 && uint(g) <= 255 && uint(b) <= 255 && uint(a) <= 255;
+ }
+
Spec cspec;
- union {
+ union CT {
+#ifdef Q_COMPILER_UNIFORM_INIT
+ CT() {} // doesn't init anything, thus can't be constexpr
+ Q_DECL_CONSTEXPR explicit CT(ushort a1, ushort a2, ushort a3, ushort a4, ushort a5) noexcept
+ : array{a1, a2, a3, a4, a5} {}
+#endif
struct {
ushort alpha;
ushort red;
@@ -292,12 +312,6 @@ private:
};
Q_DECLARE_TYPEINFO(QColor, QT_VERSION >= QT_VERSION_CHECK(6,0,0) ? Q_MOVABLE_TYPE : Q_RELOCATABLE_TYPE);
-inline QColor::QColor() noexcept
-{ invalidate(); }
-
-inline QColor::QColor(int r, int g, int b, int a)
-{ setRgb(r, g, b, a); }
-
inline QColor::QColor(QLatin1String aname)
{ setNamedColor(aname); }
@@ -309,12 +323,6 @@ inline QColor::QColor(const QString& aname)
{ setNamedColor(aname); }
#endif
-#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
-inline QColor::QColor(const QColor &acolor) noexcept
- : cspec(acolor.cspec)
-{ ct.argb = acolor.ct.argb; }
-#endif
-
inline bool QColor::isValid() const noexcept
{ return cspec != Invalid; }