forked from openSUSE/open-build-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openSUSE#16641 from danidoni/move-color-helpers-to…
…-its-own-file Move color helpers to its own file
- Loading branch information
Showing
3 changed files
with
37 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Webui::ColorHelper | ||
# Calculates a text color that will contrast well with the given color | ||
def contrast_text(color) | ||
red_component = color[1..2].to_i(16) | ||
green_component = color[3..4].to_i(16) | ||
blue_component = color[5..6].to_i(16) | ||
|
||
red = relative_luminance(red_component / 255.0) | ||
green = relative_luminance(green_component / 255.0) | ||
blue = relative_luminance(blue_component / 255.0) | ||
|
||
# https://www.w3.org/TR/WCAG20/#contrast-ratiodef | ||
((0.2126 * red) + (0.7152 * green) + (0.0722 * blue)) > 0.179 ? 'black' : 'white' | ||
end | ||
|
||
# https://www.w3.org/TR/WCAG20/#relativeluminancedef | ||
def relative_luminance(color) | ||
return color / 12.92 if color <= 0.03928 | ||
|
||
((color + 0.055) / 1.055)**2.4 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
RSpec.describe Webui::ColorHelper do | ||
describe '#contrast_text' do | ||
context 'when passing a light color' do | ||
it 'returns a black text' do | ||
expect(contrast_text('#ffffff')).to eql('black') | ||
end | ||
end | ||
|
||
context 'when passing a dark color' do | ||
it 'returns a white text' do | ||
expect(contrast_text('#000000')).to eql('white') | ||
end | ||
end | ||
end | ||
end |