123456789_123456789_123456789_123456789_123456789_

Class: Selenium::WebDriver::Support::Color

Relationships & Source Files
Inherits: Object
Defined in: rb/lib/selenium/webdriver/support/color.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(red, green, blue, alpha = 1) ⇒ Color

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 112

def initialize(red, green, blue, alpha = 1)
  @red = Integer(red)
  @green = Integer(green)
  @blue = Integer(blue)
  @alpha = Float(alpha)
end

Class Method Details

.from_hsl(h, s, l, a)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 75

def self.from_hsl(h, s, l, a) # rubocop:disable Naming/MethodParameterName
  h = Float(h) / 360
  s = Float(s) / 100
  l = Float(l) / 100
  a = Float(a || 1)

  if s.zero?
    r = l
    g = r
    b = r
  else
    luminocity2 = l < 0.5 ? l * (s + 1) : l + s - (l * s)
    luminocity1 = (l * 2) - luminocity2

    r = hue_to_rgb(luminocity1, luminocity2, h + (1.0 / 3.0))
    g = hue_to_rgb(luminocity1, luminocity2, h)
    b = hue_to_rgb(luminocity1, luminocity2, h - (1.0 / 3.0))
  end

  new (r * 255).round, (g * 255).round, (b * 255).round, a
end

.from_string(str)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 50

def self.from_string(str)
  case str
  when RGB_PATTERN
    new Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)
  when RGB_PCT_PATTERN
    array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]
    new(*array.map { |e| Float(e) / 100 * 255 })
  when RGBA_PATTERN
    new Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3), Regexp.last_match(4)
  when RGBA_PCT_PATTERN
    array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]
    new(*array.map { |e| Float(e) / 100 * 255 } << Regexp.last_match(4))
  when HEX_PATTERN
    array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]
    new(*array.map { |e| e.to_i(16) })
  when HEX3_PATTERN
    array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]
    new(*array.map { |e| (e * 2).to_i(16) })
  when HSL_PATTERN, HSLA_PATTERN
    from_hsl(Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3), Regexp.last_match(4))
  else
    raise ArgumentError, "could not convert #{str.inspect} into color"
  end
end

.hue_to_rgb(lum1, lum2, hue)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 97

def self.hue_to_rgb(lum1, lum2, hue)
  hue += 1 if hue < 0.0
  hue -= 1 if hue > 1.0

  if hue < 1.0 / 6.0
    (lum1 + ((lum2 - lum1) * 6.0 * hue))
  elsif hue < 1.0 / 2.0
    lum2
  elsif hue < 2.0 / 3.0
    lum1 + ((lum2 - lum1) * ((2.0 / 3.0) - hue) * 6.0)
  else
    lum1
  end
end

Instance Attribute Details

#alpha (readonly)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 48

attr_reader :red, :green, :blue, :alpha

#blue (readonly)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 48

attr_reader :red, :green, :blue, :alpha

#green (readonly)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 48

attr_reader :red, :green, :blue, :alpha

#red (readonly)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 48

attr_reader :red, :green, :blue, :alpha

Instance Method Details

#==(other) Also known as: #eql?

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 119

def ==(other)
  return true if equal?(other)
  return false unless other.is_a?(self.class)

  [red, green, blue, alpha] == [other.red, other.green, other.blue, other.alpha]
end

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 125

alias eql? ==

#hash

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 127

def hash
  [red, green, blue, alpha, self.class].hash
end

#hex

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 140

def hex
  format '#%<red>02x%<green>02x%<blue>02x', red: red, green: green, blue: blue
end

#rgb

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 131

def rgb
  "rgb(#{red}, #{green}, #{blue})"
end

#rgba

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/support/color.rb', line 135

def rgba
  a = alpha == 1 ? '1' : alpha
  "rgba(#{red}, #{green}, #{blue}, #{a})"
end