summaryrefslogtreecommitdiff
path: root/libs/canvas/utils.cc
diff options
context:
space:
mode:
authorPaul Davis <paul@linuxaudiosystems.com>2014-06-30 10:38:45 -0400
committerPaul Davis <paul@linuxaudiosystems.com>2014-06-30 10:38:45 -0400
commit471570705d58ee88f852009fee18f51562c34292 (patch)
tree0542689fdc3e48a349888822fcd0968123068dd5 /libs/canvas/utils.cc
parent4c1f4011fd367c0f5bb7cbc820861914644ef3c4 (diff)
move contrasting_text_color() into ArdourCanvas
Diffstat (limited to 'libs/canvas/utils.cc')
-rw-r--r--libs/canvas/utils.cc32
1 files changed, 32 insertions, 0 deletions
diff --git a/libs/canvas/utils.cc b/libs/canvas/utils.cc
index bdc8fad039..99516c849b 100644
--- a/libs/canvas/utils.cc
+++ b/libs/canvas/utils.cc
@@ -220,3 +220,35 @@ 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)
+{
+ double r, g, b, a;
+ ArdourCanvas::color_to_rgba (c, r, g, b, a);
+
+ const double black_r = 0.0;
+ const double black_g = 0.0;
+ const double black_b = 0.0;
+
+ const double white_r = 1.0;
+ const double white_g = 1.0;
+ const double white_b = 1.0;
+
+ /* Use W3C contrast guideline calculation */
+
+ 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));
+
+ 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));
+
+ 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);
+ }
+}