From 1846eebea3077e2c837a1b01fa6fdf9911030d38 Mon Sep 17 00:00:00 2001 From: Ana Date: Mon, 20 Jun 2016 17:09:39 +0200 Subject: [PATCH] Changing the text color of tracks --- app/helpers/application_helper.rb | 12 ++++++++++++ app/views/admin/tracks/index.html.haml | 3 +-- app/views/proposal/show.html.haml | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f78197e246..ad88edec7b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -286,4 +286,16 @@ def sign_in_path def unread_notifications(user) Comment.accessible_by(current_ability).find_since_last_login(user) end + + # Returns black or white deppending on what of them contrast more with the + # given color. Useful to print text in a coloured background. + # hexcolor is a hex color of 7 characters, being the first one '#'. + # Reference: https://24ways.org/2010/calculating-color-contrast + def contrast_color(hexcolor) + r = hexcolor[1..2].to_i(16) + g = hexcolor[3..4].to_i(16) + b = hexcolor[5..6].to_i(16) + yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000 + (yiq >= 128) ? 'black' : 'white' + end end diff --git a/app/views/admin/tracks/index.html.haml b/app/views/admin/tracks/index.html.haml index 73b7979d79..f2de8a38aa 100644 --- a/app/views/admin/tracks/index.html.haml +++ b/app/views/admin/tracks/index.html.haml @@ -22,7 +22,7 @@ %p = truncate(track.description) %td - %span.label{style: "background-color: #{track.color};"} + %span.label{style: "background-color: #{track.color}; color: #{ contrast_color(track.color) }"} = track.color %td .btn-group{role: "group"} @@ -34,4 +34,3 @@ .row .col-md-12.text-right = link_to 'New Track', new_admin_conference_program_track_path(@conference.short_title), class: 'btn btn-success' - diff --git a/app/views/proposal/show.html.haml b/app/views/proposal/show.html.haml index f0e79f9c6d..19483e4e64 100644 --- a/app/views/proposal/show.html.haml +++ b/app/views/proposal/show.html.haml @@ -70,7 +70,7 @@ %dt Track: %dd - if @event.track - %span.label{:style =>"background-color: #{@event.track.color};"} + %span.label{:style =>"background-color: #{@event.track.color}; color: #{ contrast_color(@event.track.color) }"} = @event.track.name .col-md-12 %dt Difficulty: