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

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.

Parameters:

  • fixed_color (String, Array<Integer, Float>)

    The color that remains unchanged

  • color_to_adjust (String, Array<Integer, Float>)

    The color of which the lightness is to be adjusted

  • level (Integer, Float) (defaults to: 4.5)

    The contrast ratio to be satisfied, such as 3.0, 4.5, 7.0

Returns:

  • (Color, nil)

    The color of which the contrast ratio against fixed_color equals or is slightly greater than the specified level.



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.

Parameters:

  • color1 (String, Array<Integer>)

    A color given as an array of integers or a string. The string can be a predefined color name, hex color code, rgb/hsl/hwb functions. Yellow, for example, can be given as [255, 255, 0], “#ffff00”, “rgb(255, 255, 255)”, “hsl(60deg, 100% 50%)” or “hwb(60deg 0% 0%)”.

  • color2 (String, Array<Integer>)

    Same as for color1.

Returns:

  • (Float)

    Contrast ratio



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.

Parameters:

  • foreground (String, Array<Integer>, Color)

    The uppermost color such as “rgb(255, 255, 0, 0.5)” or “hsl(60 100% 50% / 50%)”

  • background (String, Array<Integer>, Color)

    The color placed between the others

  • base (String, Array<Integer>, Color) (defaults to: Color::WHITE)

    The color placed in the bottom. When the backgound is completely opaque, this color is ignored.

Returns:

  • (Float)

    Contrast ratio



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.

Parameters:

  • color (String, Array<Integer>)

    A color given as an array of integers or a string. The string can be a name of predefined color, hex color code, rgb/hsl/hwb functions. Yellow, for example, can be given as [255, 255, 0], “#ffff00”, “rgb(255, 255, 255)”, “hsl(60deg, 100% 50%)” or “hwb(60deg 0% 0%)”.

Returns:

  • (Float)

    Relative luminance of the passed color.



64
65
66
# File 'lib/css_color_contrast.rb', line 64

def self.relative_luminance(color)
  Color.as_color(color).relative_luminance
end