Class: Osb::Color
- Inherits:
-
Object
- Object
- Osb::Color
- Defined in:
- lib/osb/color.rb
Overview
Represents an RGB color.
Instance Attribute Summary collapse
-
#b ⇒ Object
Blue value.
-
#g ⇒ Object
Green value.
-
#r ⇒ Object
Red value.
Class Method Summary collapse
-
.from_hex(hex) ⇒ Color
Create a Color object from hex string.
-
.from_hsl(h, s, l) ⇒ Color
Converts an HSL color value to RGB.
- .hue_to_rgb(p, q, t_) ⇒ Float private
Instance Method Summary collapse
-
#!=(color) ⇒ Object
Returns whether 2 colors are not equal.
-
#initialize(r, g, b) ⇒ Color
constructor
A new instance of Color.
Constructor Details
#initialize(r, g, b) ⇒ Color
Returns a new instance of Color.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/osb/color.rb', line 17 def initialize(r, g, b) Internal.assert_type!(r, Integer, "r") Internal.assert_value!(r, 0..255, "r") Internal.assert_type!(g, Integer, "g") Internal.assert_value!(g, 0..255, "g") Internal.assert_type!(b, Integer, "b") Internal.assert_value!(b, 0..255, "b") @r = r @g = g @b = b end |
Instance Attribute Details
#b ⇒ Object
Returns Blue value.
7 8 9 |
# File 'lib/osb/color.rb', line 7 def b @b end |
#g ⇒ Object
Returns Green value.
7 8 9 |
# File 'lib/osb/color.rb', line 7 def g @g end |
#r ⇒ Object
Returns Red value.
7 8 9 |
# File 'lib/osb/color.rb', line 7 def r @r end |
Class Method Details
.from_hex(hex) ⇒ Color
Create a Color object from hex string.
90 91 92 93 94 95 96 |
# File 'lib/osb/color.rb', line 90 def self.from_hex(hex) Internal.assert_type!(hex, String, "hex") hex.gsub!("#", "") components = hex.scan(/.{2}/) components.collect { |component| component.to_i(16) } end |
.from_hsl(h, s, l) ⇒ Color
Converts an HSL color value to RGB.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/osb/color.rb', line 45 def self.from_hsl(h, s, l) Internal.assert_type!(h, Integer, "h") Internal.assert_type!(s, Integer, "s") Internal.assert_type!(l, Integer, "l") h = h / 360.0 s = s / 100.0 l = l / 100.0 r = 0.0 g = 0.0 b = 0.0 if s == 0.0 r = l.to_f g = l.to_f b = l.to_f else q = l < 0.5 ? l * (1 + s) : l + s - l * s p = 2 * l - q r = hue_to_rgb(p, q, h + 1 / 3.0) g = hue_to_rgb(p, q, h) b = hue_to_rgb(p, q, h - 1 / 3.0) end Color.new((r * 255).round, (g * 255).round, (b * 255).round) end |
.hue_to_rgb(p, q, t_) ⇒ Float
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
78 79 80 81 82 83 84 85 |
# File 'lib/osb/color.rb', line 78 def self.hue_to_rgb(p, q, t_) t = t_ + 1 if t_ < 0 t = t_ - 1 if t_ > 1 return(p + (q - p) * 6 * t) if t < 1 / 6.0 return q if t < 1 / 2.0 return(p + (q - p) * (2 / 3.0 - t) * 6) if t < 2 / 3.0 return p end |
Instance Method Details
#!=(color) ⇒ Object
Returns whether 2 colors are not equal.
34 35 36 37 38 |
# File 'lib/osb/color.rb', line 34 def !=(color) Internal.assert_type!(color, Color, "color") color.r != self.r && color.g != self.g && color.b != self.b end |