Update user id color generation

Update the author color generation.  Now, instead of generating
an entire hex string based on the user id, the user id instead
is used to generate a hue value.  After this hue value is created,
there is some logic to tweak first the lightness and then saturation
values to achieve a readable color (in contrast to the background).

This change makes it so that user colors will not vary as wildly
between the different themes.

The values still are not cached and still do not update
initially when the theme is changed.  Both of these things
will be resolved.
This commit is contained in:
redsky17 2019-01-20 00:12:57 +00:00
parent 98102f4f09
commit b3f7c13e2f
2 changed files with 69 additions and 61 deletions

View file

@ -382,19 +382,6 @@ utils::linkColor()
return QPalette().color(QPalette::Link).name(); return QPalette().color(QPalette::Link).name();
} }
QString
utils::generateHexColor(const int hash)
{
QString colour("#");
for (int i = 0; i < 3; i++) {
int value = (hash >> (i * 8)) & 0xFF;
colour.append(("00" + QString::number(value, 16)).right(2));
}
// nhlog::ui()->debug("Hex Generated {} -> {}", QString::number(hash).toStdString(),
// colour.toStdString());
return colour.toUpper();
}
int int
utils::hashQString(const QString &input) utils::hashQString(const QString &input)
{ {
@ -404,8 +391,6 @@ utils::hashQString(const QString &input)
hash = input.at(i).digitValue() + ((hash << 5) - hash); hash = input.at(i).digitValue() + ((hash << 5) - hash);
} }
hash *= 13;
return hash; return hash;
} }
@ -417,59 +402,84 @@ utils::generateContrastingHexColor(const QString &input, const QString &backgrou
const qreal backgroundLum = luminance(background); const qreal backgroundLum = luminance(background);
// Create a color for the input // Create a color for the input
auto hash = hashQString(input); auto hash = hashQString(input);
auto colorHex = generateHexColor(hash); // create a hue value based on the hash of the input.
auto userHue = qAbs(hash % 360);
nhlog::ui()->debug(
"User Hue {} : {}", input.toStdString(), QString::number(userHue).toStdString());
// start with moderate saturation and lightness values.
auto sat = 220;
auto lightness = 125;
// converting to a QColor makes the luminance calc easier. // converting to a QColor makes the luminance calc easier.
QColor inputColor = QColor(colorHex); QColor inputColor = QColor::fromHsl(userHue, sat, lightness);
// attempt to score both the luminance and the contrast. // calculate the initial luminance and contrast of the
// contrast should have a higher precedence, but luminance // generated color. It's possible that no additional
// helps dictate how exciting the colors are. // work will be necessary.
auto colorLum = luminance(inputColor); auto lum = luminance(inputColor);
auto contrast = computeContrast(colorLum, backgroundLum); auto contrast = computeContrast(lum, backgroundLum);
// If the contrast or luminance don't meet our criteria, // If the contrast doesn't meet our criteria,
// try again and again until they do. After 10 tries, // try again and again until they do by modifying first
// the best-scoring color will be chosen. // the lightness and then the saturation of the color.
int att = 0; while (contrast < 5) {
while ((contrast < 5 || (colorLum < 0.05 || colorLum > 0.95)) && ++att < 10) { // if our lightness is at it's bounds, try changing
hash = hashQString(input) + ((hash << 2) * 13); // saturation instead.
auto newHex = generateHexColor(hash); if (lightness == 242 || lightness == 13) {
inputColor.setNamedColor(newHex); qreal newSat = qBound(26.0, sat * 1.25, 242.0);
auto tmpLum = luminance(inputColor); nhlog::ui()->info("newSat {}", QString::number(newSat).toStdString());
auto tmpContrast = computeContrast(tmpLum, backgroundLum);
// Prioritize contrast over luminance inputColor.setHsl(userHue, qFloor(newSat), lightness);
// If both values are better, it's a no brainer. auto tmpLum = luminance(inputColor);
if (tmpContrast > contrast && (tmpLum > 0.05 && tmpLum < 0.95)) { auto higherContrast = computeContrast(tmpLum, backgroundLum);
contrast = tmpContrast; if (higherContrast > contrast) {
colorHex = newHex; contrast = higherContrast;
colorLum = tmpLum; sat = newSat;
} } else {
// Otherwise, if we still can get a more newSat = qBound(26.0, sat / 1.25, 242.0);
// vibrant color and have met our contrast inputColor.setHsl(userHue, qFloor(newSat), lightness);
// threshold, pick the more vibrant color, tmpLum = luminance(inputColor);
// even if contrast will drop somewhat. auto lowerContrast = computeContrast(tmpLum, backgroundLum);
// choosing 50% luminance as ideal. if (lowerContrast > contrast) {
else if ((qAbs(tmpLum - 0.50) < qAbs(colorLum - 0.50)) && tmpContrast >= 5) { contrast = lowerContrast;
contrast = tmpContrast; sat = newSat;
colorHex = newHex; }
colorLum = tmpLum; }
} } else {
// Otherwise, just take the better contrast. qreal newLightness = qBound(13.0, lightness * 1.25, 242.0);
else if (tmpContrast > contrast) {
contrast = tmpContrast; inputColor.setHsl(userHue, sat, qFloor(newLightness));
colorHex = newHex;
colorLum = tmpLum; auto tmpLum = luminance(inputColor);
auto higherContrast = computeContrast(tmpLum, backgroundLum);
// Check to make sure we have actually improved contrast
if (higherContrast > contrast) {
contrast = higherContrast;
lightness = newLightness;
// otherwise, try going the other way instead.
} else {
newLightness = qBound(13.0, lightness / 1.25, 242.0);
inputColor.setHsl(userHue, sat, qFloor(newLightness));
tmpLum = luminance(inputColor);
auto lowerContrast = computeContrast(tmpLum, backgroundLum);
if (lowerContrast > contrast) {
contrast = lowerContrast;
lightness = newLightness;
}
}
} }
} }
// get the hex value of the generated color.
auto colorHex = inputColor.name();
nhlog::ui()->debug("Hex Generated for {}: [hex: {}, contrast: {}, luminance: {}]", nhlog::ui()->debug("Hex Generated for {}: [hex: {}, contrast: {}, luminance: {}]",
input.toStdString(), input.toStdString(),
colorHex.toStdString(), colorHex.toStdString(),
QString::number(contrast).toStdString(), QString::number(contrast).toStdString(),
QString::number(colorLum).toStdString()); QString::number(lum).toStdString());
return colorHex; return colorHex;
} }
@ -496,7 +506,9 @@ utils::luminance(const QColor &col)
v <= 0.03928 ? lumRgb[i] = v / 12.92 : lumRgb[i] = qPow((v + 0.055) / 1.055, 2.4); v <= 0.03928 ? lumRgb[i] = v / 12.92 : lumRgb[i] = qPow((v + 0.055) / 1.055, 2.4);
} }
return lumRgb[0] * 0.2126 + lumRgb[1] * 0.7152 + lumRgb[2] * 0.0722; auto lum = lumRgb[0] * 0.2126 + lumRgb[1] * 0.7152 + lumRgb[2] * 0.0722;
return lum;
} }
void void

View file

@ -229,10 +229,6 @@ markdownToHtml(const QString &text);
QString QString
linkColor(); linkColor();
//! Given an input integer, create a color string in #RRGGBB format
QString
generateHexColor(const int hash);
//! Returns the hash code of the input QString //! Returns the hash code of the input QString
int int
hashQString(const QString &input); hashQString(const QString &input);