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

View File

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