Module: CssColorContrast
- Defined in:
- lib/css_color_contrast.rb,
lib/css_color_contrast/cli.rb,
lib/css_color_contrast/color.rb,
lib/css_color_contrast/version.rb,
lib/css_color_contrast/command_interpreter.rb
Overview
Provide methods to calculate the contrast ratio related values from given colors.
Defined Under Namespace
Modules: Cli, CommandInterpreter Classes: Color, Error
Constant Summary collapse
- VERSION =
'0.2.0'
Class Method Summary collapse
-
.adjust_lightness(fixed_color, color_to_adjust, level = 4.5) ⇒ Color?
Adjust the lightness of the second color to satisfy a specified level of contrast ratio.
-
.ratio(color1, color2) ⇒ Float
Calculate the contrast ratio between given colors.
-
.ratio_with_opacity(foreground, background, base = Color::WHITE) ⇒ Float
Calculate the contrast ratio between two transparent colors.
-
.relative_luminance(color) ⇒ Float
Calculate the relative luminance of a color.
Class Method Details
.adjust_lightness(fixed_color, color_to_adjust, level = 4.5) ⇒ Color?
Adjust the lightness of the second color to satisfy a specified level of contrast ratio.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/css_color_contrast.rb', line 81 def self.adjust_lightness(fixed_color, color_to_adjust, level = 4.5) fixed, to_adjust = [fixed_color, color_to_adjust].map do |color| Color.as_color(color) end adjusted = fixed.find_lightness_threshold(to_adjust, level) if_satisfied = fixed.contrast_ratio_against(adjusted) >= level if_satisfied ? adjusted : nil end |
.ratio(color1, color2) ⇒ Float
Calculate the contrast ratio between given colors.
The contrast ratio is defined at https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef.
27 28 29 |
# File 'lib/css_color_contrast.rb', line 27 def self.ratio(color1, color2) ColorContrastCalc.contrast_ratio(color1, color2) end |
.ratio_with_opacity(foreground, background, base = Color::WHITE) ⇒ Float
Calculate the contrast ratio between two transparent colors.
For the calculation of contrast ratio between foreground and background colors, you need another color which is placed below the former two colors, because the third color filters through the overlaid colors.
48 49 50 |
# File 'lib/css_color_contrast.rb', line 48 def self.ratio_with_opacity(foreground, background, base = Color::WHITE) ColorContrastCalc.contrast_ratio_with_opacity(foreground, background, base) end |
.relative_luminance(color) ⇒ Float
Calculate the relative luminance of a color.
The definition of relative luminance is given at https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef.
64 65 66 |
# File 'lib/css_color_contrast.rb', line 64 def self.relative_luminance(color) Color.as_color(color).relative_luminance end |