Reputation: 7382
What I'm trying to do is apply a dynamic background color to black text. The color is calculated as a result of a hash of the text.
The problem is, all too often the color comes out too dark to be able to read the text.
How can I lighten the color to keep it in a decent visual range (not too dark, not too light)?
The color can't be brighter than beige or darker than teal.
(Keep in mind that Blue at 255 is darker than Green at 255, because the human eye is most sensitive to Green and the least sensitive to Blue)
Upvotes: 3
Views: 172
Reputation: 114461
You can pick the color in the HSL space
def color(Hmin=0.0, Hmax=360.0,
Smin=0.0, Smax=1.0,
Lmin=0.0, Lmax=1.0):
H = (Hmin + random.random()*(Hmax - Hmin)) % 360.0
S = Smin + random.random()*(Smax - Smin)
L = Lmin + random.random()*(Lmax - Lmin)
# Compute full-brightness, full-saturation color wheel point
if 0.0 <= H < 60.0:
R, G, B = (1.0, H/60.0, 0.0) # R -> Y
elif 60.0 <= H < 120.0:
R, G, B = (1-(H-60.0)/60.0, 1.0, 0.0) # Y -> G
elif 120.0 <= H < 180.0:
R, G, B = (0.0, 1.0, (H-120.0)/60.0) # G -> C
elif 180.0 <= H < 240.0:
R, G, B = (0.0, 1.0-(H-180.0)/60.0, 1.0) # C -> B
elif 240.0 <= H < 300.0:
R, G, B = ((H-240.0)/60.0, 0.0, 1.0) # B -> M
else:
R, G, B = (1.0, 0.0, 1.0-(H-300.0)/60.0) # M -> R
# Compute amount of gray
k = (1.0 - S) * L
# Return final RGB
return (k + R*(L-k), k + G*(L-k), k + B*(L-k))
Upvotes: 2
Reputation: 98425
QColor
supports the HSL representation. You want to limit the range of lightness:
QColor limitLightness(const QColor & color) {
auto hsl = src.toHsl();
auto h = hsl.hslHueF();
auto s = hsl.hslSaturationF();
auto l = hsl.lightnessF();
qreal const lMin = 0.25;
qreal const lMax = 0.75;
return QColor::fromHslF(h, s, qBound(lMin, lMax, l));
}
Upvotes: 2