From a992f4b4c01883e34505e0eb4d03ee239fb839d1 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 15 Feb 2022 16:03:59 +0100 Subject: QColor: remove setColorFromString() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: MÃ¥rten Nordheim --- src/gui/painting/qcolor.cpp | 39 +++++++++------------------------------ src/gui/painting/qcolor.h | 1 - 2 files changed, 9 insertions(+), 31 deletions(-) (limited to 'src/gui/painting') 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 { -- cgit v1.2.3