diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2014-06-30 10:38:45 -0400 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2014-06-30 10:38:45 -0400 |
commit | 471570705d58ee88f852009fee18f51562c34292 (patch) | |
tree | 0542689fdc3e48a349888822fcd0968123068dd5 /libs/canvas | |
parent | 4c1f4011fd367c0f5bb7cbc820861914644ef3c4 (diff) |
move contrasting_text_color() into ArdourCanvas
Diffstat (limited to 'libs/canvas')
-rw-r--r-- | libs/canvas/canvas/utils.h | 2 | ||||
-rw-r--r-- | libs/canvas/utils.cc | 32 |
2 files changed, 34 insertions, 0 deletions
diff --git a/libs/canvas/canvas/utils.h b/libs/canvas/canvas/utils.h index cd9d884475..e269ca215c 100644 --- a/libs/canvas/canvas/utils.h +++ b/libs/canvas/canvas/utils.h @@ -31,5 +31,7 @@ namespace ArdourCanvas { extern LIBCANVAS_API void set_source_rgba (Cairo::RefPtr<Cairo::Context>, Color); Distance LIBCANVAS_API distance_to_segment_squared (Duple const & p, Duple const & p1, Duple const & p2, double& t, Duple& at); + + uint32_t LIBCANVAS_API contrasting_text_color (uint32_t c); } 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); + } +} |