summaryrefslogtreecommitdiffstats
path: root/src/gui/painting
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2022-02-15 16:03:59 +0100
committerMarc Mutz <marc.mutz@qt.io>2022-03-08 06:51:11 +0100
commita992f4b4c01883e34505e0eb4d03ee239fb839d1 (patch)
treef68937a52188bf6b7ae6c505743e1db008d15eb6 /src/gui/painting
parent78b6876974d2cea087cb229257097052dad5fcf7 (diff)
QColor: remove setColorFromString()
This private method doubled as the implementation of both fromString() and isValidColorName(). By reformulating isValidColorName() as fromString().isValid(), we can then turn setColorFromString() into fromString(), by returning the data, instead of setting it on *this through use of exported functions. Since we need to touch the if's anyway, to remove braces, use C++17 if-with-initializer to turn the second if into an else-of, saving one return {}. Change-Id: If3f8182a40c0c6d6ad514431b5870e69d0e95769 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/gui/painting')
-rw-r--r--src/gui/painting/qcolor.cpp39
-rw-r--r--src/gui/painting/qcolor.h1
2 files changed, 9 insertions, 31 deletions
diff --git a/src/gui/painting/qcolor.cpp b/src/gui/painting/qcolor.cpp
index 653e56a3c8..4e7cb78624 100644
--- a/src/gui/painting/qcolor.cpp
+++ b/src/gui/painting/qcolor.cpp
@@ -994,7 +994,7 @@ bool QColor::isValidColor(QLatin1String name) noexcept
*/
bool QColor::isValidColorName(QAnyStringView name) noexcept
{
- return name.size() && QColor().setColorFromString(name);
+ return fromString(name).isValid();
}
/*!
@@ -1024,40 +1024,19 @@ bool QColor::isValidColorName(QAnyStringView name) noexcept
*/
QColor QColor::fromString(QAnyStringView name) noexcept
{
- QColor c;
- c.setColorFromString(name);
- return c;
-}
-
-bool QColor::setColorFromString(QAnyStringView name) noexcept
-{
- if (!name.size()) {
- invalidate();
- return true;
- }
+ if (!name.size())
+ return {};
if (name.front() == u'#') {
- QRgba64 rgba;
- if (get_hex_rgb(name, &rgba)) {
- setRgba64(rgba);
- return true;
- } else {
- invalidate();
- return false;
- }
- }
-
+ if (QRgba64 r; get_hex_rgb(name, &r))
+ return QColor::fromRgba64(r);
#ifndef QT_NO_COLORNAMES
- QRgb rgb;
- if (get_named_rgb(name, &rgb)) {
- setRgba(rgb);
- return true;
- } else
+ } else if (QRgb r; get_named_rgb(name, &r)) {
+ return QColor::fromRgba(r);
#endif
- {
- invalidate();
- return false;
}
+
+ return {};
}
/*!
diff --git a/src/gui/painting/qcolor.h b/src/gui/painting/qcolor.h
index a2ff932460..5bb4da87cf 100644
--- a/src/gui/painting/qcolor.h
+++ b/src/gui/painting/qcolor.h
@@ -231,7 +231,6 @@ public:
private:
void invalidate() noexcept;
- bool setColorFromString(QAnyStringView) noexcept;
static constexpr bool isRgbaValid(int r, int g, int b, int a = 255) noexcept Q_DECL_CONST_FUNCTION
{