Class: Colors::RGB
- Inherits:
-
AbstractColor
- Object
- AbstractColor
- Colors::RGB
- Includes:
- Helper
- Defined in:
- lib/colors/rgb.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#b ⇒ Object
(also: #blue)
Returns the value of attribute b.
-
#g ⇒ Object
(also: #green)
Returns the value of attribute g.
-
#r ⇒ Object
(also: #red)
Returns the value of attribute r.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #components ⇒ Object (also: #rgb_components)
- #desaturate(factor) ⇒ Object
- #hsl_components ⇒ Object
-
#initialize(r, g, b) ⇒ RGB
constructor
A new instance of RGB.
- #to_hex_string ⇒ Object
- #to_hsl ⇒ Object
- #to_hsla(alpha: 1.0) ⇒ Object
- #to_husl ⇒ Object
- #to_rgb ⇒ Object
- #to_rgba(alpha: 1.0) ⇒ Object
- #to_xterm256 ⇒ Object
- #to_xyz ⇒ Object
Methods inherited from AbstractColor
Constructor Details
#initialize(r, g, b) ⇒ RGB
Returns a new instance of RGB.
25 26 27 |
# File 'lib/colors/rgb.rb', line 25 def initialize(r, g, b) @r, @g, @b = canonicalize(r, g, b) end |
Instance Attribute Details
#b ⇒ Object Also known as: blue
Returns the value of attribute b.
29 30 31 |
# File 'lib/colors/rgb.rb', line 29 def b @b end |
#g ⇒ Object Also known as: green
Returns the value of attribute g.
29 30 31 |
# File 'lib/colors/rgb.rb', line 29 def g @g end |
#r ⇒ Object Also known as: red
Returns the value of attribute r.
29 30 31 |
# File 'lib/colors/rgb.rb', line 29 def r @r end |
Class Method Details
.parse(hex_string) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/colors/rgb.rb', line 5 def self.parse(hex_string) = "must be a hexadecimal string of `#rrggbb` or `#rgb` form" unless hex_string.respond_to?(:to_str) raise ArgumentError, "#{}: #{hex_string.inspect}" end hex_string = hex_string.to_str hexes = hex_string.match(/\A#(\h+)\z/) { $1 } case hexes&.length when 3 # rgb r, g, b = hexes.scan(/\h/).map {|h| h.hex * 17 } new(r, g, b) when 6 # rrggbb r, g, b = hexes.scan(/\h{2}/).map(&:hex) new(r, g, b) else raise ArgumentError, "#{}: #{hex_string.inspect}" end end |
Instance Method Details
#==(other) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/colors/rgb.rb', line 57 def ==(other) case other when RGBA other == self when RGB r == other.r && g == other.g && b == other.b else super end end |
#components ⇒ Object Also known as: rgb_components
31 32 33 |
# File 'lib/colors/rgb.rb', line 31 def components [r, g, b] end |
#desaturate(factor) ⇒ Object
68 69 70 |
# File 'lib/colors/rgb.rb', line 68 def desaturate(factor) to_hsl.desaturate(factor).to_rgb end |
#hsl_components ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/colors/rgb.rb', line 94 def hsl_components m1, m2 = [r, g, b].minmax c = m2 - m1 hh = case when c == 0 0r when m2 == r ((g - b) / c) % 6r when m2 == g ((b - r) / c + 2) % 6r when m2 == b ((r - g) / c + 4) % 6r end h = 60r * hh l = 0.5r * m2 + 0.5r * m1 s = if l == 1 || l == 0 0r else c / (1 - (2*l - 1).abs) end [h, s, l] end |
#to_hex_string ⇒ Object
72 73 74 |
# File 'lib/colors/rgb.rb', line 72 def to_hex_string "##{components.map {|c| "%02x" % (255*c).round.to_i }.join('')}" end |
#to_hsl ⇒ Object
85 86 87 |
# File 'lib/colors/rgb.rb', line 85 def to_hsl HSL.new(*hsl_components) end |
#to_hsla(alpha: 1.0) ⇒ Object
89 90 91 92 |
# File 'lib/colors/rgb.rb', line 89 def to_hsla(alpha: 1.0) alpha = canonicalize_component(alpha, :alpha) HSLA.new(*hsl_components, alpha) end |
#to_husl ⇒ Object
117 118 119 120 121 122 |
# File 'lib/colors/rgb.rb', line 117 def to_husl c = RGB.new(r, g, b).to_xyz l, u, v = c.luv_components(WHITE_POINT_D65) h, s, l = Convert.luv_to_husl(l, u, v) HUSL.new(h, s.clamp(0r, 1r).to_r, l.clamp(0r, 1r).to_r) end |
#to_rgb ⇒ Object
76 77 78 |
# File 'lib/colors/rgb.rb', line 76 def to_rgb self end |
#to_rgba(alpha: 1.0) ⇒ Object
80 81 82 83 |
# File 'lib/colors/rgb.rb', line 80 def to_rgba(alpha: 1.0) alpha = canonicalize_component(alpha, :alpha) RGBA.new(r, g, b, alpha) end |
#to_xterm256 ⇒ Object
128 129 130 |
# File 'lib/colors/rgb.rb', line 128 def to_xterm256 Xterm256.new(*Convert.rgb_to_xterm256(r, g, b)) end |
#to_xyz ⇒ Object
124 125 126 |
# File 'lib/colors/rgb.rb', line 124 def to_xyz XYZ.new(*Convert.rgb_to_xyz(r, g, b)) end |