Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Commit

Permalink
Use contrast color for tag text
Browse files Browse the repository at this point in the history
It's hard to read tags with light background colors and white text.
This patch uses YIQ method to set text color to black or white,
depending on which is the more contrasting to the background color.
  • Loading branch information
Peter Kovac committed Dec 13, 2017
1 parent 1aeabe0 commit d0d4f35
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
26 changes: 22 additions & 4 deletions app/helpers/tags_helper.rb
Expand Up @@ -12,20 +12,29 @@ module TagsHelper
# * open_only - Boolean. Whenever link to the filter with "open" issues
# only limit.
def render_tag_link(tag, options = {})
use_colors = RedmineTags.settings[:issues_use_colors].to_i > 0
if use_colors
tag_bg_color = tag_color(tag)
tag_fg_color = tag_fg_color(tag_bg_color)
tag_style = "background-color: #{tag_bg_color}; color: #{tag_fg_color}"
end

filters = [[:tags, '=', tag.name]]
filters << [:status_id, 'o'] if options[:open_only]
if options[:use_search]
content = link_to tag, { controller: 'search', action: 'index',
id: @project, q: tag.name, wiki_pages: true, issues: true }
content = link_to tag, { controller: 'search', action: 'index',
id: @project, q: tag.name, wiki_pages: true, issues: true,
style: tag_style }
else
content = link_to_filter tag.name, filters, project_id: @project
end
if options[:show_count]
content << content_tag('span', "(#{ tag.count })", class: 'tag-count')
end
style = if RedmineTags.settings[:issues_use_colors].to_i > 0

style = if use_colors
{ class: 'tag-label-color',
style: "background-color: #{ tag_color tag }" }
style: tag_style }
else
{ class: 'tag-label' }
end
Expand All @@ -37,6 +46,15 @@ def tag_color(tag)
"##{ Digest::MD5.hexdigest(tag_name)[0..5] }"
end

def tag_fg_color(bg_color)
# calculate contrast text color according to YIQ method
# http://24ways.org/2010/calculating-color-contrast/
r = bg_color[1..2].hex
g = bg_color[3..4].hex
b = bg_color[5..6].hex
(r * 299 + g * 587 + b * 114) >= 128000 ? "black" : "white"
end

# Renders list of tags
# Clouds are rendered as block <tt>div</tt> with internal <tt>span</t> per tag.
# Lists are rendered as unordered lists <tt>ul</tt>. Lists are ordered by
Expand Down
2 changes: 1 addition & 1 deletion assets/stylesheets/redmine_tags.css
Expand Up @@ -12,7 +12,7 @@ ul.tags li { margin: .25em 0px; }

table.list tr.issue .tag-label-color a,
.tags .tag-label-color a,
.issue .tag-label-color a { color: white; }
.issue .tag-label-color a { color: inherit; }

tr.issue td.tags {
text-align: left;
Expand Down
23 changes: 23 additions & 0 deletions config/locales/sk.yml
@@ -0,0 +1,23 @@
# Preložil samo.forus@microstep-mis.com
sk:
tags: Tagy
field_tags: Tagy
field_tag_list: Tagy
setting_issue_tags: Tagy úlohy
issues_sidebar: Zobraziť tagy v bočnom paneli ako
issues_show_count: Počet zobrazených úloh
issues_open_only: Zobraziť len otvorené úlohy
issues_sort_by: Zoradiť tagy podľa
issues_use_colors: Použiť farby

issue_tags_sidebar_none: Nezobrazovať
issue_tags_sidebar_list: Zoznam
issue_tags_sidebar_cloud: Cloud
issue_tags_sidebar_simple_cloud: Jednoduchý cloud

issues_sort_by_name: Názov
issues_sort_by_count: Počet úloh
issues_sort_order_asc: Vzostupne
issues_sort_order_desc: Zostupne

auto_complete_new_tag: Pridať...

0 comments on commit d0d4f35

Please sign in to comment.