QColor: port string-parsing from out parameters to optional<>
Makes for easier reading of code, and allows qt_get_hex_rgb(), which is called from different TUs, to be marked as PURE. Change-Id: Ie7d4e5a164ca1daf521e18ff47f17885bc1443c1 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
parent
a992f4b4c0
commit
ed5978effe
@ -754,15 +754,12 @@ inline bool operator<(const char *name, const XPMRGBData &data)
|
|||||||
inline bool operator<(const XPMRGBData &data, const char *name)
|
inline bool operator<(const XPMRGBData &data, const char *name)
|
||||||
{ return qstrcmp(data.name, name) < 0; }
|
{ return qstrcmp(data.name, name) < 0; }
|
||||||
|
|
||||||
static inline bool qt_get_named_xpm_rgb(const char *name_no_space, QRgb *rgb)
|
static inline std::optional<QRgb> qt_get_named_xpm_rgb(const char *name_no_space)
|
||||||
{
|
{
|
||||||
const XPMRGBData *r = std::lower_bound(xpmRgbTbl, xpmRgbTbl + xpmRgbTblSize, name_no_space);
|
const XPMRGBData *r = std::lower_bound(xpmRgbTbl, xpmRgbTbl + xpmRgbTblSize, name_no_space);
|
||||||
if ((r != xpmRgbTbl + xpmRgbTblSize) && !(name_no_space < *r)) {
|
if ((r != xpmRgbTbl + xpmRgbTblSize) && !(name_no_space < *r))
|
||||||
*rgb = r->value;
|
return r->value;
|
||||||
return true;
|
return {};
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
@ -941,9 +938,9 @@ static bool read_xpm_body(
|
|||||||
buf.truncate(((buf.length()-1) / 4 * 3) + 1); // remove alpha channel left by imagemagick
|
buf.truncate(((buf.length()-1) / 4 * 3) + 1); // remove alpha channel left by imagemagick
|
||||||
}
|
}
|
||||||
if (buf[0] == '#') {
|
if (buf[0] == '#') {
|
||||||
qt_get_hex_rgb(buf, &c_rgb);
|
c_rgb = qt_get_hex_rgb(buf).value_or(0);
|
||||||
} else {
|
} else {
|
||||||
qt_get_named_xpm_rgb(buf, &c_rgb);
|
c_rgb = qt_get_named_xpm_rgb(buf).value_or(0);
|
||||||
}
|
}
|
||||||
if (ncols <= 256) {
|
if (ncols <= 256) {
|
||||||
image.setColor(currentColor, 0xff000000 | c_rgb);
|
image.setColor(currentColor, 0xff000000 | c_rgb);
|
||||||
|
@ -48,6 +48,7 @@
|
|||||||
#include "private/qtools_p.h"
|
#include "private/qtools_p.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
@ -80,10 +81,10 @@ static inline int hex2int(const char *s, int n)
|
|||||||
return result;
|
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] != '#')
|
if (name[0] != '#')
|
||||||
return false;
|
return {};
|
||||||
name++;
|
name++;
|
||||||
--len;
|
--len;
|
||||||
int a, r, g, b;
|
int a, r, g, b;
|
||||||
@ -97,7 +98,7 @@ static bool get_hex_rgb(const char *name, size_t len, QRgba64 *rgb)
|
|||||||
g = hex2int(name + 3, 3);
|
g = hex2int(name + 3, 3);
|
||||||
b = hex2int(name + 6, 3);
|
b = hex2int(name + 6, 3);
|
||||||
if (r == -1 || g == -1 || b == -1)
|
if (r == -1 || g == -1 || b == -1)
|
||||||
return false;
|
return {};
|
||||||
r = (r << 4) | (r >> 8);
|
r = (r << 4) | (r >> 8);
|
||||||
g = (g << 4) | (g >> 8);
|
g = (g << 4) | (g >> 8);
|
||||||
b = (b << 4) | (b >> 8);
|
b = (b << 4) | (b >> 8);
|
||||||
@ -117,38 +118,33 @@ static bool get_hex_rgb(const char *name, size_t len, QRgba64 *rgb)
|
|||||||
} else {
|
} else {
|
||||||
r = g = b = -1;
|
r = g = b = -1;
|
||||||
}
|
}
|
||||||
if ((uint)r > 65535 || (uint)g > 65535 || (uint)b > 65535 || (uint)a > 65535) {
|
if (uint(r) > 65535 || uint(g) > 65535 || uint(b) > 65535 || uint(a) > 65535)
|
||||||
*rgb = 0;
|
return {};
|
||||||
return false;
|
return qRgba64(r, g ,b, a);
|
||||||
}
|
|
||||||
*rgb = qRgba64(r, g ,b, a);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool qt_get_hex_rgb(const char *name, QRgb *rgb)
|
std::optional<QRgb> qt_get_hex_rgb(const char *name)
|
||||||
{
|
{
|
||||||
QRgba64 rgba64;
|
if (std::optional<QRgba64> rgba64 = get_hex_rgb(name, qstrlen(name)))
|
||||||
if (!get_hex_rgb(name, qstrlen(name), &rgba64))
|
return rgba64->toArgb32();
|
||||||
return false;
|
return {};
|
||||||
*rgb = rgba64.toArgb32();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
if (len > 13)
|
||||||
return false;
|
return {};
|
||||||
char tmp[16];
|
char tmp[16];
|
||||||
for (size_t i = 0; i < len; ++i)
|
for (size_t i = 0; i < len; ++i)
|
||||||
tmp[i] = str[i].toLatin1();
|
tmp[i] = str[i].toLatin1();
|
||||||
tmp[len] = 0;
|
tmp[len] = 0;
|
||||||
return get_hex_rgb(tmp, len, rgb);
|
return get_hex_rgb(tmp, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool get_hex_rgb(QAnyStringView name, QRgba64 *rgb)
|
static std::optional<QRgba64> get_hex_rgb(QAnyStringView name)
|
||||||
{
|
{
|
||||||
return name.visit([&rgb] (auto name) {
|
return name.visit([] (auto name) {
|
||||||
return get_hex_rgb(name.data(), name.size(), rgb);
|
return get_hex_rgb(name.data(), name.size());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -337,14 +333,12 @@ inline bool operator<(const char *name, const RGBData &data)
|
|||||||
inline bool operator<(const RGBData &data, const char *name)
|
inline bool operator<(const RGBData &data, const char *name)
|
||||||
{ return qstrcmp(data.name, name) < 0; }
|
{ 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);
|
const RGBData *r = std::lower_bound(rgbTbl, rgbTbl + rgbTblSize, name_no_space);
|
||||||
if ((r != rgbTbl + rgbTblSize) && !(name_no_space < *r)) {
|
if ((r != rgbTbl + rgbTblSize) && !(name_no_space < *r))
|
||||||
*rgb = r->value;
|
return r->value;
|
||||||
return true;
|
return {};
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
@ -353,10 +347,10 @@ static char to_char(char ch) noexcept { return ch; }
|
|||||||
static char to_char(QChar ch) noexcept { return ch.toLatin1(); }
|
static char to_char(QChar ch) noexcept { return ch.toLatin1(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool get_named_rgb(QAnyStringView name, QRgb* rgb)
|
static std::optional<QRgb> get_named_rgb(QAnyStringView name)
|
||||||
{
|
{
|
||||||
if (name.size() > 255)
|
if (name.size() > 255)
|
||||||
return false;
|
return {};
|
||||||
char name_no_space[256];
|
char name_no_space[256];
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
name.visit([&pos, &name_no_space] (auto name) {
|
name.visit([&pos, &name_no_space] (auto name) {
|
||||||
@ -367,7 +361,7 @@ static bool get_named_rgb(QAnyStringView name, QRgb* rgb)
|
|||||||
});
|
});
|
||||||
name_no_space[pos] = 0;
|
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
|
#endif // QT_NO_COLORNAMES
|
||||||
@ -1028,11 +1022,11 @@ QColor QColor::fromString(QAnyStringView name) noexcept
|
|||||||
return {};
|
return {};
|
||||||
|
|
||||||
if (name.front() == u'#') {
|
if (name.front() == u'#') {
|
||||||
if (QRgba64 r; get_hex_rgb(name, &r))
|
if (std::optional<QRgba64> r = get_hex_rgb(name))
|
||||||
return QColor::fromRgba64(r);
|
return QColor::fromRgba64(*r);
|
||||||
#ifndef QT_NO_COLORNAMES
|
#ifndef QT_NO_COLORNAMES
|
||||||
} else if (QRgb r; get_named_rgb(name, &r)) {
|
} else if (std::optional<QRgb> r = get_named_rgb(name)) {
|
||||||
return QColor::fromRgba(r);
|
return QColor::fromRgba(*r);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,9 +54,11 @@
|
|||||||
#include <QtGui/private/qtguiglobal_p.h>
|
#include <QtGui/private/qtguiglobal_p.h>
|
||||||
#include "QtGui/qrgb.h"
|
#include "QtGui/qrgb.h"
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
bool qt_get_hex_rgb(const char *, QRgb *);
|
std::optional<QRgb> qt_get_hex_rgb(const char *) Q_DECL_PURE_FUNCTION;
|
||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user