class ColorContrast::Contrast

Public Instance Methods

perform(value) click to toggle source
# File lib/color_contrast.rb, line 13
def perform(value)
  raise InvalidColorError, "Not a valid RGB value or HEX value" unless valid_rgb?(value) || valid_hex?(value)

  value = is_rgb?(value) ? value.delete(" ").split(",").map(&:to_i) : get_rgb(value)
  value = get_yiq(value)

  value >= 128 ? "dark" : "light"
end

Private Instance Methods

get_rgb(hex) click to toggle source
# File lib/color_contrast.rb, line 24
def get_rgb(hex)
  hex = hex.gsub(/[^0-9A-Z]/i, "")
  hex = hex.length == 3 ? short_to_long_hex(hex) : hex
  bigint = Integer(hex, 16)

  r = (bigint >> 16) & 255
  g = (bigint >> 8) & 255
  b = bigint & 255

  [r, g, b]
end
get_yiq(rgb) click to toggle source
# File lib/color_contrast.rb, line 36
def get_yiq(rgb)
  r = rgb[0]
  g = rgb[1]
  b = rgb[2]

  ((r * 299) + (g * 587) + (b * 114)) / 1000
end
is_hex?(hex)
Alias for: valid_hex?
is_rgb?(rgb)
Alias for: valid_rgb?
short_to_long_hex(hex) click to toggle source
# File lib/color_contrast.rb, line 44
def short_to_long_hex(hex)
  hex.chars.map { |char| char * 2}.join
end
valid_hex?(hex) click to toggle source
# File lib/color_contrast.rb, line 48
def valid_hex?(hex)
  hex && hex.match?(/\A(\h{3}|\h{6})\z/)
end
Also aliased as: is_hex?
valid_rgb?(rgb) click to toggle source
# File lib/color_contrast.rb, line 54
def valid_rgb?(rgb)
  rgb && rgb.match?(/\A([0-255]{1,3}),\s?([0-255]{1,3}),\s?([0-255]{1,3})\z/)
end
Also aliased as: is_rgb?