summaryrefslogtreecommitdiff
path: root/libs/canvas/utils.cc
diff options
context:
space:
mode:
authorPaul Davis <paul@linuxaudiosystems.com>2014-11-05 11:07:20 -0500
committerPaul Davis <paul@linuxaudiosystems.com>2014-11-05 11:13:28 -0500
commitb02ea3d435282d397e4f05d514b030352af6a36e (patch)
tree32a6e1499d827134ab8e1dad24e6b739c3de03fc /libs/canvas/utils.cc
parent70e95f951dc58b2b1b2857956391e1cfc2ba27fc (diff)
alter implementation of ArdourCanvas::contrasting_text_color() to use gamma-adjusted luminance values rather than simple contrast
Diffstat (limited to 'libs/canvas/utils.cc')
-rw-r--r--libs/canvas/utils.cc62
1 files changed, 38 insertions, 24 deletions
diff --git a/libs/canvas/utils.cc b/libs/canvas/utils.cc
index b7571a7844..be509cdbe8 100644
--- a/libs/canvas/utils.cc
+++ b/libs/canvas/utils.cc
@@ -253,35 +253,49 @@ ArdourCanvas::distance_to_segment_squared (Duple const & p, Duple const & p1, Du
return ((dpqx * dpqx) + (dpqy * dpqy));
}
-uint32_t
-ArdourCanvas::contrasting_text_color (uint32_t c)
+// Inverse of sRGB "gamma" function.
+static inline double
+inv_gam_sRGB (double c)
{
- double r, g, b, a;
- ArdourCanvas::color_to_rgba (c, r, g, b, a);
+ if (c <= 0.04045) {
+ return c/12.92;
+ } else {
+ return pow(((c+0.055)/(1.055)),2.4);
+ }
+}
- const double black_r = 0.0;
- const double black_g = 0.0;
- const double black_b = 0.0;
+// sRGB "gamma" function
+static inline int
+gam_sRGB(double v)
+{
+ if (v <= 0.0031308) {
+ v *= 12.92;
+ } else {
+ v = 1.055 * pow (v, 1.0 / 2.4) - 0.055;
+ }
+ return int (v*255+.5);
+}
- const double white_r = 1.0;
- const double white_g = 1.0;
- const double white_b = 1.0;
+static double
+luminance (uint32_t c)
+{
+ // sRGB luminance(Y) values
+ const double rY = 0.212655;
+ const double gY = 0.715158;
+ const double bY = 0.072187;
- /* Use W3C contrast guideline calculation */
+ double r, g, b, a;
- double white_contrast = (max (r, white_r) - min (r, white_r)) +
- (max (g, white_g) - min (g, white_g)) +
- (max (b, white_b) - min (b, white_b));
+ ArdourCanvas::color_to_rgba (c, r, g, b, a);
+
+ return (gam_sRGB (rY*inv_gam_sRGB(r) + gY*inv_gam_sRGB(g) + bY*inv_gam_sRGB(b))) / 255.0;
+}
- double black_contrast = (max (r, black_r) - min (r, black_r)) +
- (max (g, black_g) - min (g, black_g)) +
- (max (b, black_b) - min (b, black_b));
+uint32_t
+ArdourCanvas::contrasting_text_color (uint32_t c)
+{
+ static const uint32_t white = ArdourCanvas::rgba_to_color (1.0, 1.0, 1.0, 1.0);
+ static const uint32_t black = ArdourCanvas::rgba_to_color (0.0, 0.0, 0.0, 1.0);
- if (white_contrast > black_contrast) {
- /* use white */
- return ArdourCanvas::rgba_to_color (1.0, 1.0, 1.0, 1.0);
- } else {
- /* use black */
- return ArdourCanvas::rgba_to_color (0.0, 0.0, 0.0, 1.0);
- }
+ return (luminance (c) < 0.50) ? white : black;
}