Minor improvements and style for CRgbColor, faster compare without always convertng to string

This commit is contained in:
Klaus Basan
2018-06-15 12:32:33 +02:00
parent 9438462280
commit b1fc56bb57
2 changed files with 37 additions and 45 deletions

View File

@@ -128,7 +128,7 @@ namespace BlackMisc
QString CRgbColor::hex(bool withHash) const QString CRgbColor::hex(bool withHash) const
{ {
if (!isValid()) { return ""; } if (!isValid()) { return ""; }
QString h(redHex() + greenHex() + blueHex()); const QString h(redHex() + greenHex() + blueHex());
return withHash ? "#" + h : h; return withHash ? "#" + h : h;
} }
@@ -137,14 +137,14 @@ namespace BlackMisc
if (color.isEmpty()) { return; } if (color.isEmpty()) { return; }
else if (isName) else if (isName)
{ {
QColor q(color); const QColor q(color);
m_r = q.red(); m_r = q.red();
m_g = q.green(); m_g = q.green();
m_b = q.blue(); m_b = q.blue();
} }
else else
{ {
QString c(color.trimmed()); const QString c(color.trimmed());
QColor q(c); QColor q(c);
if (setQColor(q)) { return; } if (setQColor(q)) { return; }
if (c.startsWith("#")) { this->setInvalid(); return; } if (c.startsWith("#")) { this->setInvalid(); return; }
@@ -165,9 +165,9 @@ namespace BlackMisc
if (*this == color) { return 0.0; } // avoid rounding if (*this == color) { return 0.0; } // avoid rounding
// all values 0-1 // all values 0-1
double rd = (normalizedRed() - color.normalizedRed()); const double rd = (normalizedRed() - color.normalizedRed());
double bd = (normalizedBlue() - color.normalizedBlue()); const double bd = (normalizedBlue() - color.normalizedBlue());
double gd = (normalizedGreen() - color.normalizedGreen()); const double gd = (normalizedGreen() - color.normalizedGreen());
return (rd * rd + bd * bd + gd * gd) / 3.0; return (rd * rd + bd * bd + gd * gd) / 3.0;
} }
@@ -181,67 +181,47 @@ namespace BlackMisc
QString CRgbColor::convertToQString(bool i18n) const QString CRgbColor::convertToQString(bool i18n) const
{ {
Q_UNUSED(i18n); Q_UNUSED(i18n);
return hex(); return this->hex();
} }
CVariant CRgbColor::propertyByIndex(const BlackMisc::CPropertyIndex &index) const CVariant CRgbColor::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
{ {
if (index.isMyself()) { return CVariant::from(*this); } if (index.isMyself()) { return CVariant::from(*this); }
ColumnIndex i = index.frontCasted<ColumnIndex>(); const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i) switch (i)
{ {
case IndexBlue: case IndexBlue: return CVariant::fromValue(blue());
return CVariant::fromValue(blue()); case IndexRed: return CVariant::fromValue(red());
case IndexRed: case IndexGreen: return CVariant::fromValue(green());
return CVariant::fromValue(red()); case IndexWebHex: return CVariant::fromValue(hex());
case IndexGreen: default: return CValueObject::propertyByIndex(index);
return CVariant::fromValue(green());
case IndexWebHex:
return CVariant::fromValue(hex());
default:
return CValueObject::propertyByIndex(index);
} }
} }
void CRgbColor::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) void CRgbColor::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant)
{ {
if (index.isMyself()) { (*this) = variant.to<CRgbColor>(); return; } if (index.isMyself()) { (*this) = variant.to<CRgbColor>(); return; }
ColumnIndex i = index.frontCasted<ColumnIndex>(); const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i) switch (i)
{ {
case IndexBlue: case IndexBlue: m_b = variant.toInt(); break;
this->m_b = variant.toInt(); case IndexRed: m_r = variant.toInt(); break;
break; case IndexGreen: m_g = variant.toInt(); break;
case IndexRed: case IndexWebHex: this->setByString(variant.toQString()); break;
this->m_r = variant.toInt(); default: CValueObject::setPropertyByIndex(index, variant); break;
break;
case IndexGreen:
this->m_g = variant.toInt();
break;
case IndexWebHex:
this->setByString(variant.toQString());
break;
default:
CValueObject::setPropertyByIndex(index, variant);
break;
} }
} }
int CRgbColor::comparePropertyByIndex(const CPropertyIndex &index, const CRgbColor &compareValue) const int CRgbColor::comparePropertyByIndex(const CPropertyIndex &index, const CRgbColor &compareValue) const
{ {
if (index.isMyself()) { return this->hex().compare(compareValue.hex(), Qt::CaseInsensitive); } if (index.isMyself()) { return this->compare(compareValue); }
ColumnIndex i = index.frontCasted<ColumnIndex>(); const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i) switch (i)
{ {
case IndexBlue: case IndexBlue: return Compare::compare(m_b, compareValue.m_b);
return Compare::compare(this->m_b, compareValue.m_b); case IndexRed: return Compare::compare(m_r, compareValue.m_r);
case IndexRed: case IndexGreen: return Compare::compare(m_g, compareValue.m_g);
return Compare::compare(this->m_r, compareValue.m_r); case IndexWebHex: return this->compare(compareValue);
case IndexGreen:
return Compare::compare(this->m_g, compareValue.m_g);
case IndexWebHex:
this->hex().compare(compareValue.hex(), Qt::CaseInsensitive);
break;
default: default:
Q_ASSERT_X(false, Q_FUNC_INFO, "Missing compare"); Q_ASSERT_X(false, Q_FUNC_INFO, "Missing compare");
break; break;
@@ -249,6 +229,15 @@ namespace BlackMisc
return 0; return 0;
} }
int CRgbColor::compare(const CRgbColor &color) const
{
int c = Compare::compare(m_r, color.m_r);
if (c != 0) { return c; }
c = Compare::compare(m_g, color.m_g);
if (c != 0) { return c; }
return Compare::compare(m_b, color.m_b);
}
double CRgbColor::colorRange() const double CRgbColor::colorRange() const
{ {
if (!this->isValid()) { return 255; } if (!this->isValid()) { return 255; }

View File

@@ -117,6 +117,9 @@ namespace BlackMisc
//! Compare for index //! Compare for index
int comparePropertyByIndex(const CPropertyIndex &index, const CRgbColor &compareValue) const; int comparePropertyByIndex(const CPropertyIndex &index, const CRgbColor &compareValue) const;
//! Compare with other color
int compare(const CRgbColor &color) const;
private: private:
int m_r = -1; int m_r = -1;
int m_g = -1; int m_g = -1;