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

Methods included from Hwb

hwb_to_rgb, rgb_to_hwb

Class Method Details

.hex_to_hsl(hex_code) ⇒ Array<Float>

Convert hex color code to HSL value.

Parameters:

  • hex_code (String)

    Hex color code such as “#ffff00”

Returns:

  • (Array<Float>)

    HSL value represented as an array of numbers



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.

Parameters:

  • hex_code (String)

    Hex color code such as “#ffff00”

Returns:

  • (Array<Integer>)

    RGB value represented as an array of integers



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.

Parameters:

  • hsl (Array<Float>)

    HSL value represented as an array of numbers

Returns:

  • (String)

    Hex color code such as “#ffff00”



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.

Parameters:

  • hsl (Array<Float>)

    HSL value represented as an array of numbers

Returns:

  • (Array<Integer>)

    RGB value represented as an array of integers



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.

Parameters:

  • code (String)

    Hex color code such as “#ffff00”, “#ff0” or “FFFF00”

  • prefix (true, false) (defaults to: true)

    If set to False, “#” at the head of result is removed

Returns:

  • (String)

    6-digit hexadecimal string in lowercase, with/without leading “#” depending on the value of prefix



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.

Parameters:

  • rgb (Array<Integer>)

    RGB value represented as an array of integers

Returns:

  • (String)

    Hex color code such as “#ffff00”



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.

Parameters:

  • rgb (Array<Integer>)

    RGB value represented as an array of integers

Returns:

  • (Array<Float>)

    HSL value represented as an array of numbers



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.

Parameters:

  • hex1 (String)

    RGB value in hex color code such as “#ffff00”, “#ffff00”, “#FFFF00” or “#ff0”

  • hex2 (String)

    RGB value in hex color code such as “#ffff00”, “#ffff00”, “#FFFF00” or “#ff0”

Returns:

  • (true, false)

    true if given two colors are same



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.

Parameters:

  • str (String)

    string to be checked

Returns:

  • (true, false)

    true if letters in the passed string are all in uppercase.



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.

Parameters:

  • hex_code (String)

    RGB value in hex color code such as “#ffff00”

Returns:

  • (true, false)

    true if a vaild hex color code is passed



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.

Parameters:

  • hsl (Array<Float>)

    HSL value represented as an array of numbers

Returns:

  • (true, false)

    true if a valid HSL value is passed



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.

Parameters:

  • rgb (Array<Integer>)

    RGB value represented as an array of integers

Returns:

  • (true, false)

    true if a valid RGB value is passed



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