diff options
Diffstat (limited to 'src/gui/painting/qcolor.cpp')
-rw-r--r-- | src/gui/painting/qcolor.cpp | 794 |
1 files changed, 390 insertions, 404 deletions
diff --git a/src/gui/painting/qcolor.cpp b/src/gui/painting/qcolor.cpp index c567b25468..2af2e8cb6b 100644 --- a/src/gui/painting/qcolor.cpp +++ b/src/gui/painting/qcolor.cpp @@ -1,44 +1,9 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtGui module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qcolor.h" #include "qcolor_p.h" +#include "qdrawhelper_p.h" #include "qfloat16.h" #include "qnamespace.h" #include "qdatastream.h" @@ -47,12 +12,19 @@ #include "private/qtools_p.h" #include <algorithm> +#include <optional> #include <stdio.h> #include <limits.h> QT_BEGIN_NAMESPACE +// QColor fits into QVariant's internal storage on 64bit systems. +// It could also fit on 32bit systems, but we cannot make it happen in Qt6, due to BC. +#if QT_VERSION >= QT_VERSION_CHECK(7,0,0) || QT_POINTER_SIZE > 4 +static_assert(sizeof(QColor) <= QVariant::Private::MaxInternalSize); +#endif + /*! \internal If s[0..n] is a valid hex number, returns its integer value, @@ -73,10 +45,10 @@ static inline int hex2int(const char *s, int n) return result; } -static bool get_hex_rgb(const char *name, size_t len, QRgba64 *rgb) +static std::optional<QRgba64> get_hex_rgb(const char *name, size_t len) { if (name[0] != '#') - return false; + return std::nullopt; name++; --len; int a, r, g, b; @@ -89,6 +61,8 @@ static bool get_hex_rgb(const char *name, size_t len, QRgba64 *rgb) r = hex2int(name + 0, 3); g = hex2int(name + 3, 3); b = hex2int(name + 6, 3); + if (r == -1 || g == -1 || b == -1) + return std::nullopt; r = (r << 4) | (r >> 8); g = (g << 4) | (g >> 8); b = (b << 4) | (b >> 8); @@ -108,32 +82,34 @@ static bool get_hex_rgb(const char *name, size_t len, QRgba64 *rgb) } else { r = g = b = -1; } - if ((uint)r > 65535 || (uint)g > 65535 || (uint)b > 65535 || (uint)a > 65535) { - *rgb = 0; - return false; - } - *rgb = qRgba64(r, g ,b, a); - return true; + if (uint(r) > 65535 || uint(g) > 65535 || uint(b) > 65535 || uint(a) > 65535) + return std::nullopt; + return qRgba64(r, g ,b, a); } -bool qt_get_hex_rgb(const char *name, QRgb *rgb) +std::optional<QRgb> qt_get_hex_rgb(const char *name) { - QRgba64 rgba64; - if (!get_hex_rgb(name, qstrlen(name), &rgba64)) - return false; - *rgb = rgba64.toArgb32(); - return true; + if (std::optional<QRgba64> rgba64 = get_hex_rgb(name, qstrlen(name))) + return rgba64->toArgb32(); + return std::nullopt; } -static bool get_hex_rgb(const QChar *str, size_t len, QRgba64 *rgb) +static std::optional<QRgba64> get_hex_rgb(const QChar *str, size_t len) { if (len > 13) - return false; + return std::nullopt; char tmp[16]; for (size_t i = 0; i < len; ++i) tmp[i] = str[i].toLatin1(); tmp[len] = 0; - return get_hex_rgb(tmp, len, rgb); + return get_hex_rgb(tmp, len); +} + +static std::optional<QRgba64> get_hex_rgb(QAnyStringView name) +{ + return name.visit([] (auto name) { + return get_hex_rgb(name.data(), name.size()); + }); } #ifndef QT_NO_COLORNAMES @@ -148,7 +124,7 @@ static bool get_hex_rgb(const QChar *str, size_t len, QRgba64 *rgb) #define rgb(r,g,b) (0xff000000 | (r << 16) | (g << 8) | b) // keep this is in sync with QColorConstants -static const struct RGBData { +static constexpr struct RGBData { const char name[21]; uint value; } rgbTbl[] = { @@ -304,6 +280,16 @@ static const struct RGBData { static const int rgbTblSize = sizeof(rgbTbl) / sizeof(RGBData); +static_assert([] { + for (auto e : rgbTbl) { + for (auto it = e.name; *it ; ++it) { + if (uchar(*it) > 127) + return false; + } + } + return true; + }(), "the lookup code expects color names to be US-ASCII-only"); + #undef rgb inline bool operator<(const char *name, const RGBData &data) @@ -311,43 +297,35 @@ inline bool operator<(const char *name, const RGBData &data) inline bool operator<(const RGBData &data, const char *name) { return qstrcmp(data.name, name) < 0; } -static bool get_named_rgb_no_space(const char *name_no_space, QRgb *rgb) +static std::optional<QRgb> get_named_rgb_no_space(const char *name_no_space) { const RGBData *r = std::lower_bound(rgbTbl, rgbTbl + rgbTblSize, name_no_space); - if ((r != rgbTbl + rgbTblSize) && !(name_no_space < *r)) { - *rgb = r->value; - return true; - } - return false; + if ((r != rgbTbl + rgbTblSize) && !(name_no_space < *r)) + return r->value; + return std::nullopt; } -static bool get_named_rgb(const char *name, int len, QRgb* rgb) -{ - if (len > 255) - return false; - char name_no_space[256]; - int pos = 0; - for (int i = 0; i < len; i++) { - if (name[i] != '\t' && name[i] != ' ') - name_no_space[pos++] = QChar::toLower(name[i]); - } - name_no_space[pos] = 0; - - return get_named_rgb_no_space(name_no_space, rgb); +namespace { +// named colors are US-ASCII (enforced by static_assert above): +static char to_char(char ch) noexcept { return ch; } +static char to_char(QChar ch) noexcept { return ch.toLatin1(); } } -static bool get_named_rgb(const QChar *name, int len, QRgb *rgb) +static std::optional<QRgb> get_named_rgb(QAnyStringView name) { - if (len > 255) - return false; + if (name.size() > 255) + return std::nullopt; char name_no_space[256]; int pos = 0; - for (int i = 0; i < len; i++) { - if (name[i] != QLatin1Char('\t') && name[i] != QLatin1Char(' ')) - name_no_space[pos++] = name[i].toLower().toLatin1(); - } + name.visit([&pos, &name_no_space] (auto name) { + for (auto c : name) { + if (c != u'\t' && c != u' ') + name_no_space[pos++] = QtMiscUtils::toAsciiLower(to_char(c)); + } + }); name_no_space[pos] = 0; - return get_named_rgb_no_space(name_no_space, rgb); + + return get_named_rgb_no_space(name_no_space); } #endif // QT_NO_COLORNAMES @@ -358,7 +336,7 @@ static QStringList get_colornames() #ifndef QT_NO_COLORNAMES lst.reserve(rgbTblSize); for (int i = 0; i < rgbTblSize; i++) - lst << QLatin1String(rgbTbl[i].name); + lst << QLatin1StringView(rgbTbl[i].name); #endif return lst; } @@ -402,7 +380,7 @@ static QStringList get_colornames() A color can be set by passing an RGB string (such as "#112233"), or an ARGB string (such as "#ff112233") or a color name (such as "blue"), - to the setNamedColor() function. + to the fromString() function. The color names are taken from the SVG 1.0 color names. The name() function returns the name of the color in the format "#RRGGBB". Colors can also be set using setRgb(), setHsv() and @@ -469,7 +447,7 @@ static QStringList get_colornames() The alpha channel of a color can be retrieved and set using the alpha() and setAlpha() functions if its value is an integer, and - alphaF() and setAlphaF() if its value is qreal (double). By + alphaF() and setAlphaF() if its value is float. By default, the alpha-channel is set to 255 (opaque). To retrieve and set \e all the RGB color components (including the alpha-channel) in one go, use the rgba() and setRgba() functions. @@ -610,9 +588,9 @@ static QStringList get_colornames() #define QCOLOR_REAL_RANGE_CHECK(fn, var) \ do { \ - if (var < qreal(0.0) || var > qreal(1.0)) { \ + if (var < 0.0f || var > 1.0f) { \ qWarning(#fn": invalid value %g", var); \ - var = qMax(qreal(0.0), qMin(var, qreal(1.0))); \ + var = qMax(0.0f, qMin(var, 1.0f)); \ } \ } while (0) @@ -777,7 +755,7 @@ QColor::QColor(QRgba64 rgba64) noexcept Constructs a color with the given \a spec. - This function is primarly present to avoid that QColor::Invalid + This function is primarily present to avoid that QColor::Invalid becomes a valid color by accident. */ @@ -806,6 +784,7 @@ QColor::QColor(Spec spec) noexcept } } +// ### Qt 7: remove those after deprecating them for the last Qt 6 LTS release /*! \fn QColor::QColor(const QString &name) @@ -828,7 +807,7 @@ QColor::QColor(Spec spec) noexcept */ /*! - \fn QColor::QColor(QLatin1String name) + \fn QColor::QColor(QLatin1StringView name) Constructs a named color in the same way as setNamedColor() using the given \a name. @@ -838,16 +817,6 @@ QColor::QColor(Spec spec) noexcept \sa setNamedColor(), name(), isValid() */ -#if QT_VERSION < QT_VERSION_CHECK(6,0,0) -/*! - \fn QColor::QColor(const QColor &color) - - Constructs a color that is a copy of \a color. - - \sa isValid() -*/ -#endif - /*! \fn bool QColor::isValid() const @@ -859,23 +828,25 @@ QColor::QColor(Spec spec) noexcept Returns the name of the color in the specified \a format. - \sa setNamedColor(), NameFormat + \sa fromString(), NameFormat */ QString QColor::name(NameFormat format) const { switch (format) { case HexRgb: - return QLatin1Char('#') + QString::number(rgba() | 0x1000000, 16).rightRef(6); + return u'#' + QStringView{QString::number(rgba() | 0x1000000, 16)}.right(6); case HexArgb: // it's called rgba() but it does return AARRGGBB - return QLatin1Char('#') + QString::number(rgba() | Q_INT64_C(0x100000000), 16).rightRef(8); + return u'#' + QStringView{QString::number(rgba() | Q_INT64_C(0x100000000), 16)}.right(8); } return QString(); } -#if QT_STRINGVIEW_LEVEL < 2 +#if QT_DEPRECATED_SINCE(6, 6) /*! + \deprecated [6.6] Use fromString() instead. + Sets the RGB value of this QColor to \a name, which may be in one of these formats: @@ -901,34 +872,36 @@ QString QColor::name(NameFormat format) const void QColor::setNamedColor(const QString &name) { - setColorFromString(qToStringViewIgnoringNull(name)); + *this = fromString(qToAnyStringViewIgnoringNull(name)); } -#endif /*! \overload \since 5.10 + \deprecated [6.6] Use fromString() instead. */ void QColor::setNamedColor(QStringView name) { - setColorFromString(name); + *this = fromString(name); } /*! \overload \since 5.8 + \deprecated [6.6] Use fromString() instead. */ -void QColor::setNamedColor(QLatin1String name) +void QColor::setNamedColor(QLatin1StringView name) { - setColorFromString(name); + *this = fromString(name); } -#if QT_STRINGVIEW_LEVEL < 2 /*! \since 4.7 + \deprecated [6.6] Use isValidColorName() instead. + Returns \c true if the \a name is a valid color name and can be used to construct a valid QColor object, otherwise returns false. @@ -939,58 +912,86 @@ void QColor::setNamedColor(QLatin1String name) */ bool QColor::isValidColor(const QString &name) { - return isValidColor(qToStringViewIgnoringNull(name)); + return isValidColorName(qToAnyStringViewIgnoringNull(name)); } -#endif /*! \overload \since 5.10 + \deprecated [6.6] Use isValidColorName() instead. */ bool QColor::isValidColor(QStringView name) noexcept { - return name.size() && QColor().setColorFromString(name); + return isValidColorName(name); } /*! \overload \since 5.8 + \deprecated [6.6] Use isValidColorName() instead. */ -bool QColor::isValidColor(QLatin1String name) noexcept +bool QColor::isValidColor(QLatin1StringView name) noexcept { - return name.size() && QColor().setColorFromString(name); + return isValidColorName(name); } +#endif // QT_DEPRECATED_SINCE(6, 6) -template <typename String> -bool QColor::setColorFromString(String name) +/*! + \since 6.4 + + Returns \c true if the \a name is a valid color name and can + be used to construct a valid QColor object, otherwise returns + false. + + It uses the same algorithm used in fromString(). + + \sa fromString() +*/ +bool QColor::isValidColorName(QAnyStringView name) noexcept { - if (!name.size()) { - invalidate(); - return true; - } + return fromString(name).isValid(); +} - if (name[0] == QLatin1Char('#')) { - QRgba64 rgba; - if (get_hex_rgb(name.data(), name.size(), &rgba)) { - setRgba64(rgba); - return true; - } else { - invalidate(); - return false; - } - } +/*! + \since 6.4 + + Returns an RGB QColor parsed from \a name, which may be in one + of these formats: + \list + \li #RGB (each of R, G, and B is a single hex digit) + \li #RRGGBB + \li #AARRGGBB (Since 5.2) + \li #RRRGGGBBB + \li #RRRRGGGGBBBB + \li A name from the list of colors defined in the list of + \l{https://www.w3.org/TR/SVG11/types.html#ColorKeywords}{SVG color keyword names} + provided by the World Wide Web Consortium; for example, "steelblue" or "gainsboro". + These color names work on all platforms. Note that these color names are \e not the + same as defined by the Qt::GlobalColor enums, e.g. "green" and Qt::green does not + refer to the same color. + \li \c transparent - representing the absence of a color. + \endlist + + Returns an invalid color if \a name cannot be parsed. + + \sa isValidColorName() +*/ +QColor QColor::fromString(QAnyStringView name) noexcept +{ + if (!name.size()) + return {}; + + if (name.front() == u'#') { + if (std::optional<QRgba64> r = get_hex_rgb(name)) + return QColor::fromRgba64(*r); #ifndef QT_NO_COLORNAMES - QRgb rgb; - if (get_named_rgb(name.data(), name.size(), &rgb)) { - setRgba(rgb); - return true; - } else + } else if (std::optional<QRgb> r = get_named_rgb(name)) { + return QColor::fromRgba(*r); #endif - { - invalidate(); - return false; } + + return {}; } /*! @@ -1013,7 +1014,7 @@ QStringList QColor::colorNames() \sa setHsv(), {QColor#The HSV Color Model}{The HSV Color Model} */ -void QColor::getHsvF(qreal *h, qreal *s, qreal *v, qreal *a) const +void QColor::getHsvF(float *h, float *s, float *v, float *a) const { if (!h || !s || !v) return; @@ -1023,12 +1024,12 @@ void QColor::getHsvF(qreal *h, qreal *s, qreal *v, qreal *a) const return; } - *h = ct.ahsv.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsv.hue / qreal(36000.0); - *s = ct.ahsv.saturation / qreal(USHRT_MAX); - *v = ct.ahsv.value / qreal(USHRT_MAX); + *h = ct.ahsv.hue == USHRT_MAX ? -1.0f : ct.ahsv.hue / 36000.0f; + *s = ct.ahsv.saturation / float(USHRT_MAX); + *v = ct.ahsv.value / float(USHRT_MAX); if (a) - *a = ct.ahsv.alpha / qreal(USHRT_MAX); + *a = ct.ahsv.alpha / float(USHRT_MAX); } /*! @@ -1052,11 +1053,11 @@ void QColor::getHsv(int *h, int *s, int *v, int *a) const } *h = ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100; - *s = ct.ahsv.saturation >> 8; - *v = ct.ahsv.value >> 8; + *s = qt_div_257(ct.ahsv.saturation); + *v = qt_div_257(ct.ahsv.value); if (a) - *a = ct.ahsv.alpha >> 8; + *a = qt_div_257(ct.ahsv.alpha); } /*! @@ -1067,12 +1068,12 @@ void QColor::getHsv(int *h, int *s, int *v, int *a) const \sa getHsvF(), setHsv(), {QColor#The HSV Color Model}{The HSV Color Model} */ -void QColor::setHsvF(qreal h, qreal s, qreal v, qreal a) +void QColor::setHsvF(float h, float s, float v, float a) { - if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0)) - || (s < qreal(0.0) || s > qreal(1.0)) - || (v < qreal(0.0) || v > qreal(1.0)) - || (a < qreal(0.0) || a > qreal(1.0))) { + if (((h < 0.0f || h > 1.0f) && h != -1.0f) + || (s < 0.0f || s > 1.0f) + || (v < 0.0f || v > 1.0f) + || (a < 0.0f || a > 1.0f)) { qWarning("QColor::setHsvF: HSV parameters out of range"); invalidate(); return; @@ -1080,7 +1081,7 @@ void QColor::setHsvF(qreal h, qreal s, qreal v, qreal a) cspec = Hsv; ct.ahsv.alpha = qRound(a * USHRT_MAX); - ct.ahsv.hue = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000); + ct.ahsv.hue = h == -1.0f ? USHRT_MAX : qRound(h * 36000.0f); ct.ahsv.saturation = qRound(s * USHRT_MAX); ct.ahsv.value = qRound(v * USHRT_MAX); ct.ahsv.pad = 0; @@ -1123,7 +1124,7 @@ void QColor::setHsv(int h, int s, int v, int a) \sa getHsl(), setHslF(), {QColor#The HSL Color Model}{The HSL Color Model} */ -void QColor::getHslF(qreal *h, qreal *s, qreal *l, qreal *a) const +void QColor::getHslF(float *h, float *s, float *l, float *a) const { if (!h || !s || !l) return; @@ -1133,12 +1134,12 @@ void QColor::getHslF(qreal *h, qreal *s, qreal *l, qreal *a) const return; } - *h = ct.ahsl.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsl.hue / qreal(36000.0); - *s = ct.ahsl.saturation / qreal(USHRT_MAX); - *l = ct.ahsl.lightness / qreal(USHRT_MAX); + *h = ct.ahsl.hue == USHRT_MAX ? -1.0f : ct.ahsl.hue / 36000.0f; + *s = ct.ahsl.saturation / float(USHRT_MAX); + *l = ct.ahsl.lightness / float(USHRT_MAX); if (a) - *a = ct.ahsl.alpha / qreal(USHRT_MAX); + *a = ct.ahsl.alpha / float(USHRT_MAX); } /*! @@ -1164,11 +1165,11 @@ void QColor::getHsl(int *h, int *s, int *l, int *a) const } *h = ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100; - *s = ct.ahsl.saturation >> 8; - *l = ct.ahsl.lightness >> 8; + *s = qt_div_257(ct.ahsl.saturation); + *l = qt_div_257(ct.ahsl.lightness); if (a) - *a = ct.ahsl.alpha >> 8; + *a = qt_div_257(ct.ahsl.alpha); } /*! @@ -1181,12 +1182,12 @@ void QColor::getHsl(int *h, int *s, int *l, int *a) const \sa getHslF(), setHsl() */ -void QColor::setHslF(qreal h, qreal s, qreal l, qreal a) +void QColor::setHslF(float h, float s, float l, float a) { - if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0)) - || (s < qreal(0.0) || s > qreal(1.0)) - || (l < qreal(0.0) || l > qreal(1.0)) - || (a < qreal(0.0) || a > qreal(1.0))) { + if (((h < 0.0f || h > 1.0f) && h != -1.0f) + || (s < 0.0f || s > 1.0f) + || (l < 0.0f || l > 1.0f) + || (a < 0.0f || a > 1.0f)) { qWarning("QColor::setHslF: HSL parameters out of range"); invalidate(); return; @@ -1194,7 +1195,7 @@ void QColor::setHslF(qreal h, qreal s, qreal l, qreal a) cspec = Hsl; ct.ahsl.alpha = qRound(a * USHRT_MAX); - ct.ahsl.hue = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000); + ct.ahsl.hue = h == -1.0f ? USHRT_MAX : qRound(h * 36000.0f); ct.ahsl.saturation = qRound(s * USHRT_MAX); ct.ahsl.lightness = qRound(l * USHRT_MAX); ct.ahsl.pad = 0; @@ -1248,25 +1249,22 @@ static inline const qfloat16 &castF16(const quint16 &v) \sa rgb(), setRgb() */ -void QColor::getRgbF(qreal *r, qreal *g, qreal *b, qreal *a) const +void QColor::getRgbF(float *r, float *g, float *b, float *a) const { if (!r || !g || !b) return; - if (cspec == Invalid) - return; - - if (cspec != Rgb && cspec != ExtendedRgb) { + if (cspec != Invalid && cspec != Rgb && cspec != ExtendedRgb) { toRgb().getRgbF(r, g, b, a); return; } - if (cspec == Rgb) { - *r = ct.argb.red / qreal(USHRT_MAX); - *g = ct.argb.green / qreal(USHRT_MAX); - *b = ct.argb.blue / qreal(USHRT_MAX); + if (cspec == Rgb || cspec == Invalid) { + *r = ct.argb.red / float(USHRT_MAX); + *g = ct.argb.green / float(USHRT_MAX); + *b = ct.argb.blue / float(USHRT_MAX); if (a) - *a = ct.argb.alpha / qreal(USHRT_MAX); + *a = ct.argb.alpha / float(USHRT_MAX); } else { *r = castF16(ct.argbExtended.redF16); *g = castF16(ct.argbExtended.greenF16); @@ -1296,16 +1294,16 @@ void QColor::getRgb(int *r, int *g, int *b, int *a) const return; } - *r = ct.argb.red >> 8; - *g = ct.argb.green >> 8; - *b = ct.argb.blue >> 8; + *r = qt_div_257(ct.argb.red); + *g = qt_div_257(ct.argb.green); + *b = qt_div_257(ct.argb.blue); if (a) - *a = ct.argb.alpha >> 8; + *a = qt_div_257(ct.argb.alpha); } /*! - \fn void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a) + \fn void QColor::setRgbF(float r, float g, float b, float a) Sets the color channels of this color to \a r (red), \a g (green), \a b (blue) and \a a (alpha, transparency). @@ -1316,16 +1314,16 @@ void QColor::getRgb(int *r, int *g, int *b, int *a) const \sa rgb(), getRgbF(), setRgb() */ -void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a) +void QColor::setRgbF(float r, float g, float b, float a) { - if (a < qreal(0.0) || a > qreal(1.0)) { + if (a < 0.0f || a > 1.0f) { qWarning("QColor::setRgbF: Alpha parameter is out of range"); invalidate(); return; } - if (r < qreal(0.0) || r > qreal(1.0) || - g < qreal(0.0) || g > qreal(1.0) || - b < qreal(0.0) || b > qreal(1.0) || cspec == ExtendedRgb) { + if (r < 0.0f || r > 1.0f || + g < 0.0f || g > 1.0f || + b < 0.0f || b > 1.0f || cspec == ExtendedRgb) { cspec = ExtendedRgb; castF16(ct.argbExtended.redF16) = qfloat16(r); castF16(ct.argbExtended.greenF16) = qfloat16(g); @@ -1379,7 +1377,7 @@ QRgb QColor::rgba() const noexcept { if (cspec != Invalid && cspec != Rgb) return toRgb().rgba(); - return qRgba(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8, ct.argb.alpha >> 8); + return qRgba(qt_div_257(ct.argb.red), qt_div_257(ct.argb.green), qt_div_257(ct.argb.blue), qt_div_257(ct.argb.alpha)); } /*! @@ -1442,7 +1440,7 @@ QRgb QColor::rgb() const noexcept { if (cspec != Invalid && cspec != Rgb) return toRgb().rgb(); - return qRgb(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8); + return qRgb(qt_div_257(ct.argb.red), qt_div_257(ct.argb.green), qt_div_257(ct.argb.blue)); } /*! @@ -1468,8 +1466,8 @@ void QColor::setRgb(QRgb rgb) noexcept int QColor::alpha() const noexcept { if (cspec == ExtendedRgb) - return qRound(qreal(castF16(ct.argbExtended.alphaF16)) * 255); - return ct.argb.alpha >> 8; + return qRound(float(castF16(ct.argbExtended.alphaF16)) * 255); + return qt_div_257(ct.argb.alpha); } @@ -1484,8 +1482,8 @@ void QColor::setAlpha(int alpha) { QCOLOR_INT_RANGE_CHECK("QColor::setAlpha", alpha); if (cspec == ExtendedRgb) { - constexpr qreal f = qreal(1.0) / 255; - castF16(ct.argbExtended.alphaF16) = alpha * f; + constexpr float f = 1.0f / 255; + castF16(ct.argbExtended.alphaF16) = qfloat16(alpha * f); return; } ct.argb.alpha = alpha * 0x101; @@ -1496,28 +1494,28 @@ void QColor::setAlpha(int alpha) \sa setAlphaF(), alpha(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing} */ -qreal QColor::alphaF() const noexcept +float QColor::alphaF() const noexcept { if (cspec == ExtendedRgb) return castF16(ct.argbExtended.alphaF16); - return ct.argb.alpha / qreal(USHRT_MAX); + return ct.argb.alpha / float(USHRT_MAX); } /*! - Sets the alpha of this color to \a alpha. qreal alpha is specified in the + Sets the alpha of this color to \a alpha. float alpha is specified in the range 0.0-1.0. \sa alphaF(), alpha(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing} */ -void QColor::setAlphaF(qreal alpha) +void QColor::setAlphaF(float alpha) { QCOLOR_REAL_RANGE_CHECK("QColor::setAlphaF", alpha); if (cspec == ExtendedRgb) { - castF16(ct.argbExtended.alphaF16) = alpha; + castF16(ct.argbExtended.alphaF16) = qfloat16(alpha); return; } - qreal tmp = alpha * USHRT_MAX; + float tmp = alpha * USHRT_MAX; ct.argb.alpha = qRound(tmp); } @@ -1531,7 +1529,7 @@ int QColor::red() const noexcept { if (cspec != Invalid && cspec != Rgb) return toRgb().red(); - return ct.argb.red >> 8; + return qt_div_257(ct.argb.red); } /*! @@ -1558,7 +1556,7 @@ int QColor::green() const noexcept { if (cspec != Invalid && cspec != Rgb) return toRgb().green(); - return ct.argb.green >> 8; + return qt_div_257(ct.argb.green); } /*! @@ -1586,7 +1584,7 @@ int QColor::blue() const noexcept { if (cspec != Invalid && cspec != Rgb) return toRgb().blue(); - return ct.argb.blue >> 8; + return qt_div_257(ct.argb.blue); } @@ -1610,10 +1608,10 @@ void QColor::setBlue(int blue) \sa setRedF(), red(), getRgbF() */ -qreal QColor::redF() const noexcept +float QColor::redF() const noexcept { if (cspec == Rgb || cspec == Invalid) - return ct.argb.red / qreal(USHRT_MAX); + return ct.argb.red / float(USHRT_MAX); if (cspec == ExtendedRgb) return castF16(ct.argbExtended.redF16); @@ -1627,12 +1625,12 @@ qreal QColor::redF() const noexcept \sa redF(), red(), setRgbF() */ -void QColor::setRedF(qreal red) +void QColor::setRedF(float red) { - if (cspec == Rgb && red >= qreal(0.0) && red <= qreal(1.0)) + if (cspec == Rgb && red >= 0.0f && red <= 1.0f) ct.argb.red = qRound(red * USHRT_MAX); else if (cspec == ExtendedRgb) - castF16(ct.argbExtended.redF16) = red; + castF16(ct.argbExtended.redF16) = qfloat16(red); else setRgbF(red, greenF(), blueF(), alphaF()); } @@ -1642,10 +1640,10 @@ void QColor::setRedF(qreal red) \sa setGreenF(), green(), getRgbF() */ -qreal QColor::greenF() const noexcept +float QColor::greenF() const noexcept { if (cspec == Rgb || cspec == Invalid) - return ct.argb.green / qreal(USHRT_MAX); + return ct.argb.green / float(USHRT_MAX); if (cspec == ExtendedRgb) return castF16(ct.argbExtended.greenF16); @@ -1659,12 +1657,12 @@ qreal QColor::greenF() const noexcept \sa greenF(), green(), setRgbF() */ -void QColor::setGreenF(qreal green) +void QColor::setGreenF(float green) { - if (cspec == Rgb && green >= qreal(0.0) && green <= qreal(1.0)) + if (cspec == Rgb && green >= 0.0f && green <= 1.0f) ct.argb.green = qRound(green * USHRT_MAX); else if (cspec == ExtendedRgb) - castF16(ct.argbExtended.greenF16) = green; + castF16(ct.argbExtended.greenF16) = qfloat16(green); else setRgbF(redF(), green, blueF(), alphaF()); } @@ -1674,10 +1672,10 @@ void QColor::setGreenF(qreal green) \sa setBlueF(), blue(), getRgbF() */ -qreal QColor::blueF() const noexcept +float QColor::blueF() const noexcept { if (cspec == Rgb || cspec == Invalid) - return ct.argb.blue / qreal(USHRT_MAX); + return ct.argb.blue / float(USHRT_MAX); if (cspec == ExtendedRgb) return castF16(ct.argbExtended.blueF16); @@ -1689,12 +1687,12 @@ qreal QColor::blueF() const noexcept the 0.0-1.0 range, the color model will be changed to \c ExtendedRgb. \sa blueF(), blue(), setRgbF() */ -void QColor::setBlueF(qreal blue) +void QColor::setBlueF(float blue) { - if (cspec == Rgb && blue >= qreal(0.0) && blue <= qreal(1.0)) + if (cspec == Rgb && blue >= 0.0f && blue <= 1.0f) ct.argb.blue = qRound(blue * USHRT_MAX); else if (cspec == ExtendedRgb) - castF16(ct.argbExtended.blueF16) = blue; + castF16(ct.argbExtended.blueF16) = qfloat16(blue); else setRgbF(redF(), greenF(), blue, alphaF()); } @@ -1747,7 +1745,7 @@ int QColor::hsvSaturation() const noexcept { if (cspec != Invalid && cspec != Hsv) return toHsv().saturation(); - return ct.ahsv.saturation >> 8; + return qt_div_257(ct.ahsv.saturation); } /*! @@ -1759,7 +1757,7 @@ int QColor::value() const noexcept { if (cspec != Invalid && cspec != Hsv) return toHsv().value(); - return ct.ahsv.value >> 8; + return qt_div_257(ct.ahsv.value); } /*! @@ -1769,7 +1767,7 @@ int QColor::value() const noexcept \sa hsvHueF(), hslHueF(), hue(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model} */ -qreal QColor::hueF() const noexcept +float QColor::hueF() const noexcept { return hsvHueF(); } @@ -1780,11 +1778,11 @@ qreal QColor::hueF() const noexcept \sa hue(), hslHueF(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model} */ -qreal QColor::hsvHueF() const noexcept +float QColor::hsvHueF() const noexcept { if (cspec != Invalid && cspec != Hsv) return toHsv().hueF(); - return ct.ahsv.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsv.hue / qreal(36000.0); + return ct.ahsv.hue == USHRT_MAX ? -1.0f : ct.ahsv.hue / 36000.0f; } /*! @@ -1795,7 +1793,7 @@ qreal QColor::hsvHueF() const noexcept \sa hsvSaturationF(), hslSaturationF(), saturation(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model} */ -qreal QColor::saturationF() const noexcept +float QColor::saturationF() const noexcept { return hsvSaturationF(); } @@ -1805,11 +1803,11 @@ qreal QColor::saturationF() const noexcept \sa saturation(), hslSaturationF(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model} */ -qreal QColor::hsvSaturationF() const noexcept +float QColor::hsvSaturationF() const noexcept { if (cspec != Invalid && cspec != Hsv) return toHsv().saturationF(); - return ct.ahsv.saturation / qreal(USHRT_MAX); + return ct.ahsv.saturation / float(USHRT_MAX); } /*! @@ -1817,11 +1815,11 @@ qreal QColor::hsvSaturationF() const noexcept \sa value(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model} */ -qreal QColor::valueF() const noexcept +float QColor::valueF() const noexcept { if (cspec != Invalid && cspec != Hsv) return toHsv().valueF(); - return ct.ahsv.value / qreal(USHRT_MAX); + return ct.ahsv.value / float(USHRT_MAX); } /*! @@ -1849,7 +1847,7 @@ int QColor::hslSaturation() const noexcept { if (cspec != Invalid && cspec != Hsl) return toHsl().hslSaturation(); - return ct.ahsl.saturation >> 8; + return qt_div_257(ct.ahsl.saturation); } /*! @@ -1863,7 +1861,7 @@ int QColor::lightness() const noexcept { if (cspec != Invalid && cspec != Hsl) return toHsl().lightness(); - return ct.ahsl.lightness >> 8; + return qt_div_257(ct.ahsl.lightness); } /*! @@ -1873,11 +1871,11 @@ int QColor::lightness() const noexcept \sa hslHue(), hsvHueF(), getHslF() */ -qreal QColor::hslHueF() const noexcept +float QColor::hslHueF() const noexcept { if (cspec != Invalid && cspec != Hsl) return toHsl().hslHueF(); - return ct.ahsl.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsl.hue / qreal(36000.0); + return ct.ahsl.hue == USHRT_MAX ? -1.0f : ct.ahsl.hue / 36000.0f; } /*! @@ -1887,11 +1885,11 @@ qreal QColor::hslHueF() const noexcept \sa hslSaturation(), hsvSaturationF(), getHslF(), {QColor#The HSL Color Model}{The HSL Color Model} */ -qreal QColor::hslSaturationF() const noexcept +float QColor::hslSaturationF() const noexcept { if (cspec != Invalid && cspec != Hsl) return toHsl().hslSaturationF(); - return ct.ahsl.saturation / qreal(USHRT_MAX); + return ct.ahsl.saturation / float(USHRT_MAX); } /*! @@ -1901,11 +1899,11 @@ qreal QColor::hslSaturationF() const noexcept \sa value(), getHslF() */ -qreal QColor::lightnessF() const noexcept +float QColor::lightnessF() const noexcept { if (cspec != Invalid && cspec != Hsl) return toHsl().lightnessF(); - return ct.ahsl.lightness / qreal(USHRT_MAX); + return ct.ahsl.lightness / float(USHRT_MAX); } /*! @@ -1917,7 +1915,7 @@ int QColor::cyan() const noexcept { if (cspec != Invalid && cspec != Cmyk) return toCmyk().cyan(); - return ct.acmyk.cyan >> 8; + return qt_div_257(ct.acmyk.cyan); } /*! @@ -1929,7 +1927,7 @@ int QColor::magenta() const noexcept { if (cspec != Invalid && cspec != Cmyk) return toCmyk().magenta(); - return ct.acmyk.magenta >> 8; + return qt_div_257(ct.acmyk.magenta); } /*! @@ -1941,7 +1939,7 @@ int QColor::yellow() const noexcept { if (cspec != Invalid && cspec != Cmyk) return toCmyk().yellow(); - return ct.acmyk.yellow >> 8; + return qt_div_257(ct.acmyk.yellow); } /*! @@ -1954,7 +1952,7 @@ int QColor::black() const noexcept { if (cspec != Invalid && cspec != Cmyk) return toCmyk().black(); - return ct.acmyk.black >> 8; + return qt_div_257(ct.acmyk.black); } /*! @@ -1962,11 +1960,11 @@ int QColor::black() const noexcept \sa cyan(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model} */ -qreal QColor::cyanF() const noexcept +float QColor::cyanF() const noexcept { if (cspec != Invalid && cspec != Cmyk) return toCmyk().cyanF(); - return ct.acmyk.cyan / qreal(USHRT_MAX); + return ct.acmyk.cyan / float(USHRT_MAX); } /*! @@ -1974,11 +1972,11 @@ qreal QColor::cyanF() const noexcept \sa magenta(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model} */ -qreal QColor::magentaF() const noexcept +float QColor::magentaF() const noexcept { if (cspec != Invalid && cspec != Cmyk) return toCmyk().magentaF(); - return ct.acmyk.magenta / qreal(USHRT_MAX); + return ct.acmyk.magenta / float(USHRT_MAX); } /*! @@ -1986,11 +1984,11 @@ qreal QColor::magentaF() const noexcept \sa yellow(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model} */ -qreal QColor::yellowF() const noexcept +float QColor::yellowF() const noexcept { if (cspec != Invalid && cspec != Cmyk) return toCmyk().yellowF(); - return ct.acmyk.yellow / qreal(USHRT_MAX); + return ct.acmyk.yellow / float(USHRT_MAX); } /*! @@ -1998,11 +1996,11 @@ qreal QColor::yellowF() const noexcept \sa black(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model} */ -qreal QColor::blackF() const noexcept +float QColor::blackF() const noexcept { if (cspec != Invalid && cspec != Cmyk) return toCmyk().blackF(); - return ct.acmyk.black / qreal(USHRT_MAX); + return ct.acmyk.black / float(USHRT_MAX); } /*! @@ -2018,7 +2016,7 @@ QColor QColor::toExtendedRgb() const noexcept if (cspec != Rgb) return toRgb().toExtendedRgb(); - constexpr qreal f = qreal(1.0) / USHRT_MAX; + constexpr float f = 1.0f / USHRT_MAX; QColor color; color.cspec = ExtendedRgb; castF16(color.ct.argbExtended.alphaF16) = qfloat16(ct.argb.alpha * f); @@ -2055,15 +2053,15 @@ QColor QColor::toRgb() const noexcept } // chromatic case - const qreal h = ct.ahsv.hue == 36000 ? 0 : ct.ahsv.hue / 6000.; - const qreal s = ct.ahsv.saturation / qreal(USHRT_MAX); - const qreal v = ct.ahsv.value / qreal(USHRT_MAX); + const float h = ct.ahsv.hue == 36000 ? 0.0f : ct.ahsv.hue / 6000.0f; + const float s = ct.ahsv.saturation / float(USHRT_MAX); + const float v = ct.ahsv.value / float(USHRT_MAX); const int i = int(h); - const qreal f = h - i; - const qreal p = v * (qreal(1.0) - s); + const float f = h - i; + const float p = v * (1.0f - s); if (i & 1) { - const qreal q = v * (qreal(1.0) - (s * f)); + const float q = v * (1.0f - (s * f)); switch (i) { case 1: @@ -2083,7 +2081,7 @@ QColor QColor::toRgb() const noexcept break; } } else { - const qreal t = v * (qreal(1.0) - (s * (qreal(1.0) - f))); + const float t = v * (1.0f - (s * (1.0f - f))); switch (i) { case 0: @@ -2115,34 +2113,34 @@ QColor QColor::toRgb() const noexcept color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = 0; } else { // chromatic case - const qreal h = ct.ahsl.hue == 36000 ? 0 : ct.ahsl.hue / 36000.; - const qreal s = ct.ahsl.saturation / qreal(USHRT_MAX); - const qreal l = ct.ahsl.lightness / qreal(USHRT_MAX); + const float h = ct.ahsl.hue == 36000 ? 0.0f : ct.ahsl.hue / 36000.0f; + const float s = ct.ahsl.saturation / float(USHRT_MAX); + const float l = ct.ahsl.lightness / float(USHRT_MAX); - qreal temp2; - if (l < qreal(0.5)) - temp2 = l * (qreal(1.0) + s); + float temp2; + if (l < 0.5f) + temp2 = l * (1.0f + s); else temp2 = l + s - (l * s); - const qreal temp1 = (qreal(2.0) * l) - temp2; - qreal temp3[3] = { h + (qreal(1.0) / qreal(3.0)), + const float temp1 = (2.0f * l) - temp2; + float temp3[3] = { h + (1.0f / 3.0f), h, - h - (qreal(1.0) / qreal(3.0)) }; + h - (1.0f / 3.0f) }; for (int i = 0; i != 3; ++i) { - if (temp3[i] < qreal(0.0)) - temp3[i] += qreal(1.0); - else if (temp3[i] > qreal(1.0)) - temp3[i] -= qreal(1.0); + if (temp3[i] < 0.0f) + temp3[i] += 1.0f; + else if (temp3[i] > 1.0f) + temp3[i] -= 1.0f; - const qreal sixtemp3 = temp3[i] * qreal(6.0); - if (sixtemp3 < qreal(1.0)) + const float sixtemp3 = temp3[i] * 6.0f; + if (sixtemp3 < 1.0f) color.ct.array[i+1] = qRound((temp1 + (temp2 - temp1) * sixtemp3) * USHRT_MAX); - else if ((temp3[i] * qreal(2.0)) < qreal(1.0)) + else if ((temp3[i] * 2.0f) < 1.0f) color.ct.array[i+1] = qRound(temp2 * USHRT_MAX); - else if ((temp3[i] * qreal(3.0)) < qreal(2.0)) - color.ct.array[i+1] = qRound((temp1 + (temp2 -temp1) * (qreal(2.0) /qreal(3.0) - temp3[i]) * qreal(6.0)) * USHRT_MAX); + else if ((temp3[i] * 3.0f) < 2.0f) + color.ct.array[i+1] = qRound((temp1 + (temp2 -temp1) * (2.0f /3.0f - temp3[i]) * 6.0f) * USHRT_MAX); else color.ct.array[i+1] = qRound(temp1 * USHRT_MAX); } @@ -2154,21 +2152,21 @@ QColor QColor::toRgb() const noexcept } case Cmyk: { - const qreal c = ct.acmyk.cyan / qreal(USHRT_MAX); - const qreal m = ct.acmyk.magenta / qreal(USHRT_MAX); - const qreal y = ct.acmyk.yellow / qreal(USHRT_MAX); - const qreal k = ct.acmyk.black / qreal(USHRT_MAX); - - color.ct.argb.red = qRound((qreal(1.0) - (c * (qreal(1.0) - k) + k)) * USHRT_MAX); - color.ct.argb.green = qRound((qreal(1.0) - (m * (qreal(1.0) - k) + k)) * USHRT_MAX); - color.ct.argb.blue = qRound((qreal(1.0) - (y * (qreal(1.0) - k) + k)) * USHRT_MAX); + const float c = ct.acmyk.cyan / float(USHRT_MAX); + const float m = ct.acmyk.magenta / float(USHRT_MAX); + const float y = ct.acmyk.yellow / float(USHRT_MAX); + const float k = ct.acmyk.black / float(USHRT_MAX); + + color.ct.argb.red = qRound((1.0f - (c * (1.0f - k) + k)) * USHRT_MAX); + color.ct.argb.green = qRound((1.0f - (m * (1.0f - k) + k)) * USHRT_MAX); + color.ct.argb.blue = qRound((1.0f - (y * (1.0f - k) + k)) * USHRT_MAX); break; } case ExtendedRgb: - color.ct.argb.alpha = qRound(USHRT_MAX * qreal(castF16(ct.argbExtended.alphaF16))); - color.ct.argb.red = qRound(USHRT_MAX * qBound(qreal(0.0), qreal(castF16(ct.argbExtended.redF16)), qreal(1.0))); - color.ct.argb.green = qRound(USHRT_MAX * qBound(qreal(0.0), qreal(castF16(ct.argbExtended.greenF16)), qreal(1.0))); - color.ct.argb.blue = qRound(USHRT_MAX * qBound(qreal(0.0), qreal(castF16(ct.argbExtended.blueF16)), qreal(1.0))); + color.ct.argb.alpha = qRound(USHRT_MAX * float(castF16(ct.argbExtended.alphaF16))); + color.ct.argb.red = qRound(USHRT_MAX * qBound(0.0f, float(castF16(ct.argbExtended.redF16)), 1.0f)); + color.ct.argb.green = qRound(USHRT_MAX * qBound(0.0f, float(castF16(ct.argbExtended.greenF16)), 1.0f)); + color.ct.argb.blue = qRound(USHRT_MAX * qBound(0.0f, float(castF16(ct.argbExtended.blueF16)), 1.0f)); break; default: break; @@ -2200,12 +2198,12 @@ QColor QColor::toHsv() const noexcept color.ct.ahsv.alpha = ct.argb.alpha; color.ct.ahsv.pad = 0; - const qreal r = ct.argb.red / qreal(USHRT_MAX); - const qreal g = ct.argb.green / qreal(USHRT_MAX); - const qreal b = ct.argb.blue / qreal(USHRT_MAX); - const qreal max = Q_MAX_3(r, g, b); - const qreal min = Q_MIN_3(r, g, b); - const qreal delta = max - min; + const float r = ct.argb.red / float(USHRT_MAX); + const float g = ct.argb.green / float(USHRT_MAX); + const float b = ct.argb.blue / float(USHRT_MAX); + const float max = Q_MAX_3(r, g, b); + const float min = Q_MIN_3(r, g, b); + const float delta = max - min; color.ct.ahsv.value = qRound(max * USHRT_MAX); if (qFuzzyIsNull(delta)) { // achromatic case, hue is undefined @@ -2213,21 +2211,21 @@ QColor QColor::toHsv() const noexcept color.ct.ahsv.saturation = 0; } else { // chromatic case - qreal hue = 0; + float hue = 0; color.ct.ahsv.saturation = qRound((delta / max) * USHRT_MAX); if (qFuzzyCompare(r, max)) { hue = ((g - b) /delta); } else if (qFuzzyCompare(g, max)) { - hue = (qreal(2.0) + (b - r) / delta); + hue = (2.0f + (b - r) / delta); } else if (qFuzzyCompare(b, max)) { - hue = (qreal(4.0) + (r - g) / delta); + hue = (4.0f + (r - g) / delta); } else { Q_ASSERT_X(false, "QColor::toHsv", "internal error"); } - hue *= qreal(60.0); - if (hue < qreal(0.0)) - hue += qreal(360.0); - color.ct.ahsv.hue = qRound(hue * 100); + hue *= 60.0f; + if (hue < 0.0f) + hue += 360.0f; + color.ct.ahsv.hue = qRound(hue * 100.0f); } return color; @@ -2251,14 +2249,14 @@ QColor QColor::toHsl() const noexcept color.ct.ahsl.alpha = ct.argb.alpha; color.ct.ahsl.pad = 0; - const qreal r = ct.argb.red / qreal(USHRT_MAX); - const qreal g = ct.argb.green / qreal(USHRT_MAX); - const qreal b = ct.argb.blue / qreal(USHRT_MAX); - const qreal max = Q_MAX_3(r, g, b); - const qreal min = Q_MIN_3(r, g, b); - const qreal delta = max - min; - const qreal delta2 = max + min; - const qreal lightness = qreal(0.5) * delta2; + const float r = ct.argb.red / float(USHRT_MAX); + const float g = ct.argb.green / float(USHRT_MAX); + const float b = ct.argb.blue / float(USHRT_MAX); + const float max = Q_MAX_3(r, g, b); + const float min = Q_MIN_3(r, g, b); + const float delta = max - min; + const float delta2 = max + min; + const float lightness = 0.5f * delta2; color.ct.ahsl.lightness = qRound(lightness * USHRT_MAX); if (qFuzzyIsNull(delta)) { // achromatic case, hue is undefined @@ -2266,24 +2264,24 @@ QColor QColor::toHsl() const noexcept color.ct.ahsl.saturation = 0; } else { // chromatic case - qreal hue = 0; - if (lightness < qreal(0.5)) + float hue = 0; + if (lightness < 0.5f) color.ct.ahsl.saturation = qRound((delta / delta2) * USHRT_MAX); else - color.ct.ahsl.saturation = qRound((delta / (qreal(2.0) - delta2)) * USHRT_MAX); + color.ct.ahsl.saturation = qRound((delta / (2.0f - delta2)) * USHRT_MAX); if (qFuzzyCompare(r, max)) { hue = ((g - b) /delta); } else if (qFuzzyCompare(g, max)) { - hue = (qreal(2.0) + (b - r) / delta); + hue = (2.0f + (b - r) / delta); } else if (qFuzzyCompare(b, max)) { - hue = (qreal(4.0) + (r - g) / delta); + hue = (4.0f + (r - g) / delta); } else { Q_ASSERT_X(false, "QColor::toHsv", "internal error"); } - hue *= qreal(60.0); - if (hue < qreal(0.0)) - hue += qreal(360.0); - color.ct.ahsl.hue = qRound(hue * 100); + hue *= 60.0f; + if (hue < 0.0f) + hue += 360.0f; + color.ct.ahsl.hue = qRound(hue * 100.0f); } return color; @@ -2313,18 +2311,18 @@ QColor QColor::toCmyk() const noexcept color.ct.acmyk.black = USHRT_MAX; } else { // rgb -> cmy - const qreal r = ct.argb.red / qreal(USHRT_MAX); - const qreal g = ct.argb.green / qreal(USHRT_MAX); - const qreal b = ct.argb.blue / qreal(USHRT_MAX); - qreal c = qreal(1.0) - r; - qreal m = qreal(1.0) - g; - qreal y = qreal(1.0) - b; + const float r = ct.argb.red / float(USHRT_MAX); + const float g = ct.argb.green / float(USHRT_MAX); + const float b = ct.argb.blue / float(USHRT_MAX); + float c = 1.0f - r; + float m = 1.0f - g; + float y = 1.0f - b; // cmy -> cmyk - const qreal k = qMin(c, qMin(m, y)); - c = (c - k) / (qreal(1.0) - k); - m = (m - k) / (qreal(1.0) - k); - y = (y - k) / (qreal(1.0) - k); + const float k = qMin(c, qMin(m, y)); + c = (c - k) / (1.0f - k); + m = (m - k) / (1.0f - k); + y = (y - k) / (1.0f - k); color.ct.acmyk.cyan = qRound(c * USHRT_MAX); color.ct.acmyk.magenta = qRound(m * USHRT_MAX); @@ -2426,16 +2424,16 @@ QColor QColor::fromRgb(int r, int g, int b, int a) \sa fromRgb(), fromRgba64(), toRgb(), isValid() */ -QColor QColor::fromRgbF(qreal r, qreal g, qreal b, qreal a) +QColor QColor::fromRgbF(float r, float g, float b, float a) { - if (a < qreal(0.0) || a > qreal(1.0)) { + if (a < 0.0f || a > 1.0f) { qWarning("QColor::fromRgbF: Alpha parameter out of range"); return QColor(); } - if (r < qreal(0.0) || r > qreal(1.0) - || g < qreal(0.0) || g > qreal(1.0) - || b < qreal(0.0) || b > qreal(1.0)) { + if (r < 0.0f || r > 1.0f + || g < 0.0f || g > 1.0f + || b < 0.0f || b > 1.0f) { QColor color; color.cspec = ExtendedRgb; castF16(color.ct.argbExtended.alphaF16) = qfloat16(a); @@ -2529,12 +2527,12 @@ QColor QColor::fromHsv(int h, int s, int v, int a) \sa toHsv(), fromHsv(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model} */ -QColor QColor::fromHsvF(qreal h, qreal s, qreal v, qreal a) +QColor QColor::fromHsvF(float h, float s, float v, float a) { - if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0)) - || (s < qreal(0.0) || s > qreal(1.0)) - || (v < qreal(0.0) || v > qreal(1.0)) - || (a < qreal(0.0) || a > qreal(1.0))) { + if (((h < 0.0f || h > 1.0f) && h != -1.0f) + || (s < 0.0f || s > 1.0f) + || (v < 0.0f || v > 1.0f) + || (a < 0.0f || a > 1.0f)) { qWarning("QColor::fromHsvF: HSV parameters out of range"); return QColor(); } @@ -2542,7 +2540,7 @@ QColor QColor::fromHsvF(qreal h, qreal s, qreal v, qreal a) QColor color; color.cspec = Hsv; color.ct.ahsv.alpha = qRound(a * USHRT_MAX); - color.ct.ahsv.hue = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000); + color.ct.ahsv.hue = h == -1.0f ? USHRT_MAX : qRound(h * 36000.0f); color.ct.ahsv.saturation = qRound(s * USHRT_MAX); color.ct.ahsv.value = qRound(v * USHRT_MAX); color.ct.ahsv.pad = 0; @@ -2593,12 +2591,12 @@ QColor QColor::fromHsl(int h, int s, int l, int a) \sa toHsl(), fromHsl(), isValid(), {QColor#The HSL Color Model}{The HSL Color Model} */ -QColor QColor::fromHslF(qreal h, qreal s, qreal l, qreal a) +QColor QColor::fromHslF(float h, float s, float l, float a) { - if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0)) - || (s < qreal(0.0) || s > qreal(1.0)) - || (l < qreal(0.0) || l > qreal(1.0)) - || (a < qreal(0.0) || a > qreal(1.0))) { + if (((h < 0.0f || h > 1.0f) && h != -1.0f) + || (s < 0.0f || s > 1.0f) + || (l < 0.0f || l > 1.0f) + || (a < 0.0f || a > 1.0f)) { qWarning("QColor::fromHslF: HSL parameters out of range"); return QColor(); } @@ -2606,7 +2604,7 @@ QColor QColor::fromHslF(qreal h, qreal s, qreal l, qreal a) QColor color; color.cspec = Hsl; color.ct.ahsl.alpha = qRound(a * USHRT_MAX); - color.ct.ahsl.hue = (h == qreal(-1.0)) ? USHRT_MAX : qRound(h * 36000); + color.ct.ahsl.hue = (h == -1.0f) ? USHRT_MAX : qRound(h * 36000.0f); if (color.ct.ahsl.hue == 36000) color.ct.ahsl.hue = 0; color.ct.ahsl.saturation = qRound(s * USHRT_MAX); @@ -2635,13 +2633,13 @@ void QColor::getCmyk(int *c, int *m, int *y, int *k, int *a) const return; } - *c = ct.acmyk.cyan >> 8; - *m = ct.acmyk.magenta >> 8; - *y = ct.acmyk.yellow >> 8; - *k = ct.acmyk.black >> 8; + *c = qt_div_257(ct.acmyk.cyan); + *m = qt_div_257(ct.acmyk.magenta); + *y = qt_div_257(ct.acmyk.yellow); + *k = qt_div_257(ct.acmyk.black); if (a) - *a = ct.acmyk.alpha >> 8; + *a = qt_div_257(ct.acmyk.alpha); } /*! @@ -2654,7 +2652,7 @@ void QColor::getCmyk(int *c, int *m, int *y, int *k, int *a) const \sa setCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model} */ -void QColor::getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a) const +void QColor::getCmykF(float *c, float *m, float *y, float *k, float *a) const { if (!c || !m || !y || !k) return; @@ -2664,13 +2662,13 @@ void QColor::getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a) const return; } - *c = ct.acmyk.cyan / qreal(USHRT_MAX); - *m = ct.acmyk.magenta / qreal(USHRT_MAX); - *y = ct.acmyk.yellow / qreal(USHRT_MAX); - *k = ct.acmyk.black / qreal(USHRT_MAX); + *c = ct.acmyk.cyan / float(USHRT_MAX); + *m = ct.acmyk.magenta / float(USHRT_MAX); + *y = ct.acmyk.yellow / float(USHRT_MAX); + *k = ct.acmyk.black / float(USHRT_MAX); if (a) - *a = ct.acmyk.alpha / qreal(USHRT_MAX); + *a = ct.acmyk.alpha / float(USHRT_MAX); } /*! @@ -2711,13 +2709,13 @@ void QColor::setCmyk(int c, int m, int y, int k, int a) \sa getCmykF(), setCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model} */ -void QColor::setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a) +void QColor::setCmykF(float c, float m, float y, float k, float a) { - if (c < qreal(0.0) || c > qreal(1.0) - || m < qreal(0.0) || m > qreal(1.0) - || y < qreal(0.0) || y > qreal(1.0) - || k < qreal(0.0) || k > qreal(1.0) - || a < qreal(0.0) || a > qreal(1.0)) { + if (c < 0.0f || c > 1.0f + || m < 0.0f || m > 1.0f + || y < 0.0f || y > 1.0f + || k < 0.0f || k > 1.0f + || a < 0.0f || a > 1.0f) { qWarning("QColor::setCmykF: CMYK parameters out of range"); invalidate(); return; @@ -2772,13 +2770,13 @@ QColor QColor::fromCmyk(int c, int m, int y, int k, int a) \sa toCmyk(), fromCmyk(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model} */ -QColor QColor::fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a) +QColor QColor::fromCmykF(float c, float m, float y, float k, float a) { - if (c < qreal(0.0) || c > qreal(1.0) - || m < qreal(0.0) || m > qreal(1.0) - || y < qreal(0.0) || y > qreal(1.0) - || k < qreal(0.0) || k > qreal(1.0) - || a < qreal(0.0) || a > qreal(1.0)) { + if (c < 0.0f || c > 1.0f + || m < 0.0f || m > 1.0f + || y < 0.0f || y > 1.0f + || k < 0.0f || k > 1.0f + || a < 0.0f || a > 1.0f) { qWarning("QColor::fromCmykF: CMYK parameters out of range"); return QColor(); } @@ -2870,28 +2868,6 @@ QColor QColor::darker(int factor) const noexcept return hsv.convertTo(cspec); } -#if QT_DEPRECATED_SINCE(5, 13) -/*! - \obsolete - - Use lighter(\a factor) instead. -*/ -QColor QColor::light(int factor) const noexcept -{ - return lighter(factor); -} - -/*! - \obsolete - - Use darker(\a factor) instead. -*/ -QColor QColor::dark(int factor) const noexcept -{ - return darker(factor); -} -#endif - /*! \overload Assigns a copy of \a color and returns a reference to this color. */ @@ -2901,26 +2877,27 @@ QColor &QColor::operator=(Qt::GlobalColor color) noexcept } /*! - Returns \c true if this color has the same RGB and alpha values as \a color; + Returns \c true if this color has the same color specification and component values as \a color; otherwise returns \c false. + + ExtendedRgb and Rgb specifications are considered matching in this context. + + \sa spec() */ bool QColor::operator==(const QColor &color) const noexcept { - if (cspec == Hsl && cspec == color.cspec) { - return (ct.argb.alpha == color.ct.argb.alpha - && ct.ahsl.hue % 36000 == color.ct.ahsl.hue % 36000 - && (qAbs(ct.ahsl.saturation - color.ct.ahsl.saturation) < 50 - || ct.ahsl.lightness == 0 - || color.ct.ahsl.lightness == 0 - || ct.ahsl.lightness == USHRT_MAX - || color.ct.ahsl.lightness == USHRT_MAX) - && (qAbs(ct.ahsl.lightness - color.ct.ahsl.lightness)) < 50); + if ((cspec == ExtendedRgb || color.cspec == ExtendedRgb) && + (cspec == color.cspec || cspec == Rgb || color.cspec == Rgb)) { + return qFuzzyCompare(alphaF(), color.alphaF()) + && qFuzzyCompare(redF(), color.redF()) + && qFuzzyCompare(greenF(), color.greenF()) + && qFuzzyCompare(blueF(), color.blueF()); } else { return (cspec == color.cspec && ct.argb.alpha == color.ct.argb.alpha - && (((cspec == QColor::Hsv) + && (((cspec == QColor::Hsv || cspec == QColor::Hsl) && ((ct.ahsv.hue % 36000) == (color.ct.ahsv.hue % 36000))) - || (ct.ahsv.hue == color.ct.ahsv.hue)) + || (ct.argb.red == color.ct.argb.red)) && ct.argb.green == color.ct.argb.green && ct.argb.blue == color.ct.argb.blue && ct.argb.pad == color.ct.argb.pad); @@ -2928,8 +2905,12 @@ bool QColor::operator==(const QColor &color) const noexcept } /*! - Returns \c true if this color has a different RGB and alpha values from + Returns \c true if this color has different color specification or component values from \a color; otherwise returns \c false. + + ExtendedRgb and Rgb specifications are considered matching in this context. + + \sa spec() */ bool QColor::operator!=(const QColor &color) const noexcept { return !operator==(color); } @@ -2940,7 +2921,7 @@ bool QColor::operator!=(const QColor &color) const noexcept */ QColor::operator QVariant() const { - return QVariant(QVariant::Color, this); + return QVariant::fromValue(*this); } /*! \internal @@ -3146,7 +3127,7 @@ const uint qt_inv_premul_factor[256] = { Returns the ARGB quadruplet (255, \a{r}, \a{g}, \a{b}). - \sa qRgba(), qRed(), qGreen(), qBlue() + \sa qRgba(), qRed(), qGreen(), qBlue(), qAlpha() */ /*! @@ -3155,7 +3136,7 @@ const uint qt_inv_premul_factor[256] = { Returns the ARGB quadruplet (\a{a}, \a{r}, \a{g}, \a{b}). - \sa qRgb(), qRed(), qGreen(), qBlue() + \sa qRgb(), qRed(), qGreen(), qBlue(), qAlpha() */ /*! @@ -3218,12 +3199,17 @@ const uint qt_inv_premul_factor[256] = { channel is \c ff, i.e opaque. For more information, see the \l{QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing} section. - \sa QColor::rgb(), QColor::rgba() + Here are some examples of how QRgb values can be created: + + \snippet code/src_gui_painting_qcolor.cpp QRgb + + \sa qRgb(), qRgba(), QColor::rgb(), QColor::rgba() */ /*! \namespace QColorConstants \inmodule QtGui + \since 5.14 \brief The QColorConstants namespace contains QColor predefined constants. |