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>
This commit is contained in:
Marc Mutz 2022-02-15 16:03:59 +01:00
parent 78b6876974
commit a992f4b4c0
2 changed files with 10 additions and 32 deletions

View File

@ -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
} else if (QRgb r; get_named_rgb(name, &r)) {
return QColor::fromRgba(r);
#endif
}
#ifndef QT_NO_COLORNAMES
QRgb rgb;
if (get_named_rgb(name, &rgb)) {
setRgba(rgb);
return true;
} else
#endif
{
invalidate();
return false;
}
return {};
}
/*!

View File

@ -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
{