Module: ColorContrastCalc::Utils
- Extended by:
- Hwb
- Defined in:
- lib/color_contrast_calc/utils.rb
Overview
Utility functions that provide basic operations on colors.
This module provides basic operations on colors given as RGB values (including their hex code presentations) or HSL values.
Defined Under Namespace
Modules: Hwb
Class Method Summary collapse
-
.hex_to_hsl(hex_code) ⇒ Array<Float>
Convert hex color code to HSL value.
-
.hex_to_rgb(hex_code) ⇒ Array<Integer>
Convert a hex color code string to a RGB value.
-
.hsl_to_hex(hsl) ⇒ String
Convert HSL value to hex color code.
-
.hsl_to_rgb(hsl) ⇒ Array<Integer>
Convert HSL value to RGB value.
-
.normalize_hex(code, prefix = true) ⇒ String
Normalize a hex color code to a 6 digits, lowercased one.
-
.rgb_to_hex(rgb) ⇒ String
Convert a RGB value to a hex color code.
-
.rgb_to_hsl(rgb) ⇒ Array<Float>
Convert RGB value to HSL value.
-
.same_hex_color?(hex1, hex2) ⇒ true, false
Check if given two hex color codes represent a same color.
-
.uppercase?(str) ⇒ true, false
Check if a given string is consists of uppercase letters.
-
.valid_hex?(hex_code) ⇒ true, false
Check if a given string is a valid representation of RGB color.
-
.valid_hsl?(hsl) ⇒ true, false
Check if a given array is a valid representation of HSL color.
-
.valid_rgb?(rgb) ⇒ true, false
Check if a given array is a valid representation of RGB color.
Methods included from Hwb
Class Method Details
.hex_to_hsl(hex_code) ⇒ Array<Float>
Convert hex color code to HSL value.
177 178 179 |
# File 'lib/color_contrast_calc/utils.rb', line 177 def self.hex_to_hsl(hex_code) rgb_to_hsl(hex_to_rgb(hex_code)) end |
.hex_to_rgb(hex_code) ⇒ Array<Integer>
Convert a hex color code string to a RGB value.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/color_contrast_calc/utils.rb', line 29 def self.hex_to_rgb(hex_code) hex_part = hex_code.start_with?('#') ? hex_code[1..-1] : hex_code case hex_part.length when 3 hex_part.chars.map {|c| c.hex * 17 } when 6 [0, 2, 4].map {|i| hex_part[i, 2].hex } end end |
.hsl_to_hex(hsl) ⇒ String
Convert HSL value to hex color code.
107 108 109 |
# File 'lib/color_contrast_calc/utils.rb', line 107 def self.hsl_to_hex(hsl) rgb_to_hex(hsl_to_rgb(hsl)) end |
.hsl_to_rgb(hsl) ⇒ Array<Integer>
Convert HSL value to RGB value.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/color_contrast_calc/utils.rb', line 76 def self.hsl_to_rgb(hsl) # https://www.w3.org/TR/css3-color/#hsl-color h = hsl[0] / 360.0 s = hsl[1] / 100.0 l = hsl[2] / 100.0 m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s m1 = l * 2 - m2 [h + 1 / 3.0, h, h - 1 / 3.0].map do |adjusted_h| (hue_to_rgb(m1, m2, adjusted_h) * 255).round end end |
.normalize_hex(code, prefix = true) ⇒ String
Normalize a hex color code to a 6 digits, lowercased one.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/color_contrast_calc/utils.rb', line 49 def self.normalize_hex(code, prefix = true) if code.length < 6 hex_part = code.start_with?('#') ? code[1..-1] : code code = hex_part.chars.map {|c| c * 2 }.join end lowered = code.downcase return lowered if prefix == lowered.start_with?('#') prefix ? "##{lowered}" : lowered[1..-1] end |
.rgb_to_hex(rgb) ⇒ String
Convert a RGB value to a hex color code.
66 67 68 |
# File 'lib/color_contrast_calc/utils.rb', line 66 def self.rgb_to_hex(rgb) format('#%02x%02x%02x', *rgb) end |
.rgb_to_hsl(rgb) ⇒ Array<Float>
Convert RGB value to HSL value.
117 118 119 |
# File 'lib/color_contrast_calc/utils.rb', line 117 def self.rgb_to_hsl(rgb) [rgb_to_hue(rgb), rgb_to_saturation(rgb), rgb_to_lightness(rgb)] end |
.same_hex_color?(hex1, hex2) ⇒ true, false
Check if given two hex color codes represent a same color.
225 226 227 |
# File 'lib/color_contrast_calc/utils.rb', line 225 def self.same_hex_color?(hex1, hex2) normalize_hex(hex1) == normalize_hex(hex2) end |
.uppercase?(str) ⇒ true, false
Check if a given string is consists of uppercase letters.
236 237 238 |
# File 'lib/color_contrast_calc/utils.rb', line 236 def self.uppercase?(str) !/[[:lower:]]/.match?(str) end |
.valid_hex?(hex_code) ⇒ true, false
Check if a given string is a valid representation of RGB color.
212 213 214 |
# File 'lib/color_contrast_calc/utils.rb', line 212 def self.valid_hex?(hex_code) HEX_RE.match?(hex_code) end |
.valid_hsl?(hsl) ⇒ true, false
Check if a given array is a valid representation of HSL color.
198 199 200 201 202 203 204 |
# File 'lib/color_contrast_calc/utils.rb', line 198 def self.valid_hsl?(hsl) return false unless hsl.length == 3 hsl.each_with_index do |c, i| return false if !c.is_a?(Numeric) || c < 0 || c > HSL_UPPER_LIMIT[i] end true end |
.valid_rgb?(rgb) ⇒ true, false
Check if a given array is a valid representation of RGB color.
187 188 189 190 |
# File 'lib/color_contrast_calc/utils.rb', line 187 def self.valid_rgb?(rgb) rgb.length == 3 && rgb.all? {|c| c.is_a?(Integer) && c >= 0 && c <= 255 } end |