Module: ColorContrastCalc
- Defined in:
- lib/color_contrast_calc.rb,
lib/color_contrast_calc/shim.rb,
lib/color_contrast_calc/color.rb,
lib/color_contrast_calc/utils.rb,
lib/color_contrast_calc/sorter.rb,
lib/color_contrast_calc/checker.rb,
lib/color_contrast_calc/version.rb,
lib/color_contrast_calc/converter.rb,
lib/color_contrast_calc/deprecated.rb,
lib/color_contrast_calc/threshold_finder.rb,
lib/color_contrast_calc/transparency_calc.rb,
lib/color_contrast_calc/color_function_parser.rb,
lib/color_contrast_calc/invalid_color_representation_error.rb
Defined Under Namespace
Modules: Checker, ColorFunctionParser, Converter, Deprecated, Rgb, Shim, Sorter, ThresholdFinder, TransparencyCalc, Utils Classes: Color, InvalidColorRepresentationError
Constant Summary collapse
- VERSION =
'0.9.0'
Class Method Summary collapse
-
.color_from(color_value, name = nil) ⇒ Color
Return an instance of Color.
-
.contrast_ratio(color1, color2) ⇒ Float
Calculate the contrast ratio of given colors.
-
.contrast_ratio_with_opacity(foreground, background, base = Color::WHITE) ⇒ Float
Calculate the contrast ratio of transparent colors.
-
.higher_contrast_base_color_for(color, light_base: Color::WHITE, dark_base: Color::BLACK) ⇒ String, ...
Select from two colors the one of which the contrast ratio is higher than the other’s, against a given color.
-
.hsl_colors(s: 100, l: 50, h_interval: 1) ⇒ Array<Color>
Return a list of colors which share the same saturation and lightness.
-
.named_colors(frozen: true) ⇒ Array<Color>
Return an array of named colors.
-
.sort(colors, color_order = 'hSL', key_mapper = nil, &key_mapper_block) ⇒ Array<Color>, Array<String>
Sort colors in the order specified by
color_order
. -
.web_safe_colors(frozen: true) ⇒ Array<Color>
Return an array of web safe colors.
Class Method Details
.color_from(color_value, name = nil) ⇒ Color
Return an instance of Color.
As color_value
, you can pass a predefined color name, an RGB value represented as an array of integers like [255, 255, 0], or a string such as a hex code like “#ffff00”. name
is assigned to the returned instance.
29 30 31 |
# File 'lib/color_contrast_calc.rb', line 29 def self.color_from(color_value, name = nil) Color.color_from(color_value, name) end |
.contrast_ratio(color1, color2) ⇒ Float
Calculate the contrast ratio of given colors.
The definition of contrast ratio is given at https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
Please note that this method may be slow, as it internally creates Color instances.
75 76 77 |
# File 'lib/color_contrast_calc.rb', line 75 def self.contrast_ratio(color1, color2) Color.as_color(color1).contrast_ratio_against(Color.as_color(color2)) end |
.contrast_ratio_with_opacity(foreground, background, base = Color::WHITE) ⇒ Float
Calculate the contrast ratio of transparent colors.
For the calculation, you have to specify three colors because when both of two colors to be compared are transparent, the third color put under them filters through them.
95 96 97 98 99 100 101 102 103 |
# File 'lib/color_contrast_calc.rb', line 95 def self.contrast_ratio_with_opacity(foreground, background, base = Color::WHITE) params = [foreground, background, base].map do |c| color = Color.as_color(c) color.rgb + [color.opacity] end TransparencyCalc.contrast_ratio(*params) end |
.higher_contrast_base_color_for(color, light_base: Color::WHITE, dark_base: Color::BLACK) ⇒ String, ...
Select from two colors the one of which the contrast ratio is higher than the other’s, against a given color.
Note that this method is tentatively provided and may be changed later including its name.
123 124 125 126 127 128 129 |
# File 'lib/color_contrast_calc.rb', line 123 def self.higher_contrast_base_color_for(color, light_base: Color::WHITE, dark_base: Color::BLACK) ratio_with_light = contrast_ratio(color, light_base) ratio_with_dark = contrast_ratio(color, dark_base) ratio_with_light < ratio_with_dark ? dark_base : light_base end |
.hsl_colors(s: 100, l: 50, h_interval: 1) ⇒ Array<Color>
Return a list of colors which share the same saturation and lightness.
By default, so-called pure colors are returned.
166 167 168 |
# File 'lib/color_contrast_calc.rb', line 166 def self.hsl_colors(s: 100, l: 50, h_interval: 1) Color::List.hsl_colors(s: s, l: l, h_interval: h_interval) end |
.named_colors(frozen: true) ⇒ Array<Color>
Return an array of named colors.
You can find the color names at www.w3.org/TR/SVG/types.html#ColorKeywords
139 140 141 142 |
# File 'lib/color_contrast_calc.rb', line 139 def self.named_colors(frozen: true) named_colors = Color::List::NAMED_COLORS frozen ? named_colors : named_colors.dup end |
.sort(colors, color_order = 'hSL', key_mapper = nil, &key_mapper_block) ⇒ Array<Color>, Array<String>
Sort colors in the order specified by color_order
.
Sort colors given as an array of Color instances or hex color codes. (alias of Sorter.sort())
You can specify sorting order by giving a color_order
string, such as “HSL” or “RGB”. A component of color_order
on the left side has a higher sorting precedence, and an uppercase letter means descending order.
52 53 54 55 56 |
# File 'lib/color_contrast_calc.rb', line 52 def self.sort(colors, color_order = 'hSL', key_mapper = nil, &key_mapper_block) key_mapper = key_mapper_block if !key_mapper && key_mapper_block Sorter.sort(colors, color_order, key_mapper) end |
.web_safe_colors(frozen: true) ⇒ Array<Color>
Return an array of web safe colors.
150 151 152 153 |
# File 'lib/color_contrast_calc.rb', line 150 def self.web_safe_colors(frozen: true) colors = Color::List::WEB_SAFE_COLORS frozen ? colors : colors.dup end |