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>
This commit is contained in:
parent
166753d8e0
commit
de82d239f8
@ -1349,7 +1349,7 @@ void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a)
|
|||||||
*/
|
*/
|
||||||
void QColor::setRgb(int r, int g, int b, int a)
|
void QColor::setRgb(int r, int g, int b, int a)
|
||||||
{
|
{
|
||||||
if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255 || (uint)a > 255) {
|
if (!isRgbaValid(r, g, b, a)) {
|
||||||
qWarning("QColor::setRgb: RGB parameters out of range");
|
qWarning("QColor::setRgb: RGB parameters out of range");
|
||||||
invalidate();
|
invalidate();
|
||||||
return;
|
return;
|
||||||
@ -2398,10 +2398,7 @@ QColor QColor::fromRgba(QRgb rgba) noexcept
|
|||||||
*/
|
*/
|
||||||
QColor QColor::fromRgb(int r, int g, int b, int a)
|
QColor QColor::fromRgb(int r, int g, int b, int a)
|
||||||
{
|
{
|
||||||
if (r < 0 || r > 255
|
if (!isRgbaValid(r, g, b, a)) {
|
||||||
|| g < 0 || g > 255
|
|
||||||
|| b < 0 || b > 255
|
|
||||||
|| a < 0 || a > 255) {
|
|
||||||
qWarning("QColor::fromRgb: RGB parameters out of range");
|
qWarning("QColor::fromRgb: RGB parameters out of range");
|
||||||
return QColor();
|
return QColor();
|
||||||
}
|
}
|
||||||
|
@ -67,9 +67,16 @@ public:
|
|||||||
enum Spec { Invalid, Rgb, Hsv, Cmyk, Hsl, ExtendedRgb };
|
enum Spec { Invalid, Rgb, Hsv, Cmyk, Hsl, ExtendedRgb };
|
||||||
enum NameFormat { HexRgb, HexArgb };
|
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;
|
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(QRgb rgb) noexcept;
|
||||||
QColor(QRgba64 rgba64) noexcept;
|
QColor(QRgba64 rgba64) noexcept;
|
||||||
#if QT_STRINGVIEW_LEVEL < 2
|
#if QT_STRINGVIEW_LEVEL < 2
|
||||||
@ -81,8 +88,11 @@ public:
|
|||||||
QColor(Spec spec) noexcept;
|
QColor(Spec spec) noexcept;
|
||||||
|
|
||||||
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
|
#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.
|
// ### Qt 6: remove all of these, the trivial ones are fine.
|
||||||
QColor(QColor &&other) noexcept : cspec(other.cspec), ct(other.ct) {}
|
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
|
QColor &operator=(QColor &&other) noexcept
|
||||||
{ cspec = other.cspec; ct = other.ct; return *this; }
|
{ cspec = other.cspec; ct = other.ct; return *this; }
|
||||||
QColor &operator=(const QColor &) noexcept;
|
QColor &operator=(const QColor &) noexcept;
|
||||||
@ -244,8 +254,18 @@ private:
|
|||||||
template <typename String>
|
template <typename String>
|
||||||
bool setColorFromString(String name);
|
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;
|
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 {
|
struct {
|
||||||
ushort alpha;
|
ushort alpha;
|
||||||
ushort red;
|
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);
|
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)
|
inline QColor::QColor(QLatin1String aname)
|
||||||
{ setNamedColor(aname); }
|
{ setNamedColor(aname); }
|
||||||
|
|
||||||
@ -309,12 +323,6 @@ inline QColor::QColor(const QString& aname)
|
|||||||
{ setNamedColor(aname); }
|
{ setNamedColor(aname); }
|
||||||
#endif
|
#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
|
inline bool QColor::isValid() const noexcept
|
||||||
{ return cspec != Invalid; }
|
{ return cspec != Invalid; }
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user