Class: Selenium::WebDriver::Support::Color
- Inherits:
-
Object
- Object
- Selenium::WebDriver::Support::Color
- Defined in:
- lib/selenium/webdriver/support/color.rb
Constant Summary collapse
- RGB_PATTERN =
/^\s*rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)\s*$/- RGB_PCT_PATTERN =
/^\s*rgb\(\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(\d{1,3}|\d{1,2}\.\d+)%\s*\)\s*$/- RGBA_PATTERN =
/^\s*rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(0|1|0\.\d+)\s*\)\s*$/- RGBA_PCT_PATTERN =
/^\s*rgba\(\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(0|1|0\.\d+)\s*\)\s*$/- HEX_PATTERN =
pXDigit or h only works on Ruby 1.9
/#([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})/- HEX3_PATTERN =
pXDigit or h only works on Ruby 1.9
/#([A-Fa-f0-9])([A-Fa-f0-9])([A-Fa-f0-9])/- HSL_PATTERN =
/^\s*hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)\s*$/- HSLA_PATTERN =
/^\s*hsla\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*,\s*(0|1|0\.\d+)\s*\)\s*$/
Instance Attribute Summary collapse
-
#alpha ⇒ Object
readonly
Returns the value of attribute alpha.
-
#blue ⇒ Object
readonly
Returns the value of attribute blue.
-
#green ⇒ Object
readonly
Returns the value of attribute green.
-
#red ⇒ Object
readonly
Returns the value of attribute red.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
- #hex ⇒ Object
-
#initialize(red, green, blue, alpha = 1) ⇒ Color
constructor
A new instance of Color.
- #rgb ⇒ Object
- #rgba ⇒ Object
Constructor Details
#initialize(red, green, blue, alpha = 1) ⇒ Color
Returns a new instance of Color.
92 93 94 95 96 97 |
# File 'lib/selenium/webdriver/support/color.rb', line 92 def initialize(red, green, blue, alpha = 1) @red = Integer(red) @green = Integer(green) @blue = Integer(blue) @alpha = Float(alpha) end |
Instance Attribute Details
#alpha ⇒ Object (readonly)
Returns the value of attribute alpha.
31 32 33 |
# File 'lib/selenium/webdriver/support/color.rb', line 31 def alpha @alpha end |
#blue ⇒ Object (readonly)
Returns the value of attribute blue.
31 32 33 |
# File 'lib/selenium/webdriver/support/color.rb', line 31 def blue @blue end |
#green ⇒ Object (readonly)
Returns the value of attribute green.
31 32 33 |
# File 'lib/selenium/webdriver/support/color.rb', line 31 def green @green end |
#red ⇒ Object (readonly)
Returns the value of attribute red.
31 32 33 |
# File 'lib/selenium/webdriver/support/color.rb', line 31 def red @red end |
Class Method Details
.from_hsl(h, s, l, a) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/selenium/webdriver/support/color.rb', line 54 def self.from_hsl(h, s, l, a) h = Float(h) / 360 s = Float(s) / 100 l = Float(l) / 100 a = Float(a || 1) if s == 0 r = l g = r b = r else luminocity2 = (l < 0.5) ? l * (1 + s) : l + s - l * s luminocity1 = 2 * l - luminocity2 hue_to_rgb = lambda do |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 r = hue_to_rgb.call(luminocity1, luminocity2, h + 1.0 / 3.0) g = hue_to_rgb.call(luminocity1, luminocity2, h) b = hue_to_rgb.call(luminocity1, luminocity2, h - 1.0 / 3.0) end new (r * 255).round, (g * 255).round, (b * 255).round, a end |
.from_string(str) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/selenium/webdriver/support/color.rb', line 33 def self.from_string(str) case str when RGB_PATTERN new $1, $2, $3 when RGB_PCT_PATTERN new(*[$1, $2, $3].map { |e| Float(e) / 100 * 255 }) when RGBA_PATTERN new $1, $2, $3, $4 when RGBA_PCT_PATTERN new(*[$1, $2, $3].map { |e| Float(e) / 100 * 255 } << $4) when HEX_PATTERN new(*[$1, $2, $3].map { |e| e.to_i(16) }) when HEX3_PATTERN new(*[$1, $2, $3].map { |e| (e * 2).to_i(16) }) when HSL_PATTERN, HSLA_PATTERN from_hsl($1, $2, $3, $4) else raise ArgumentError, "could not convert #{str.inspect} into color" end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
99 100 101 102 103 104 |
# File 'lib/selenium/webdriver/support/color.rb', line 99 def ==(other) return true if equal?(other) return false unless other.kind_of?(self.class) [red, green, blue, alpha] == [other.red, other.green, other.blue, other.alpha] end |
#hash ⇒ Object
107 108 109 |
# File 'lib/selenium/webdriver/support/color.rb', line 107 def hash [red, green, blue, alpha].hash ^ self.class.hash end |
#hex ⇒ Object
120 121 122 |
# File 'lib/selenium/webdriver/support/color.rb', line 120 def hex "#%02x%02x%02x" % [red, green, blue] end |
#rgb ⇒ Object
111 112 113 |
# File 'lib/selenium/webdriver/support/color.rb', line 111 def rgb "rgb(#{red}, #{green}, #{blue})" end |
#rgba ⇒ Object
115 116 117 118 |
# File 'lib/selenium/webdriver/support/color.rb', line 115 def rgba a = alpha == 1 ? '1' : alpha "rgba(#{red}, #{green}, #{blue}, #{a})" end |