Class: RGB
Overview
A representation of a colour in the RGB colour space
Stores the numbers internally as floats between 0 and 1
Instance Attribute Summary collapse
-
#b ⇒ Object
Returns the value of attribute b.
-
#g ⇒ Object
Returns the value of attribute g.
-
#r ⇒ Object
Returns the value of attribute r.
Instance Method Summary collapse
- #hex ⇒ Object
-
#initialize(r = 0.0, g = 0.0, b = 0.0) ⇒ RGB
constructor
A new instance of RGB.
- #to_hsv ⇒ Object
- #web_hex ⇒ Object
- #web_safe(depth = 1) ⇒ Object
Methods included from Colour
#analogous, #complementary, #gradient_to, #rotate_hue, #split_complementary, #to_rgb, #to_s, #triadic
Constructor Details
#initialize(r = 0.0, g = 0.0, b = 0.0) ⇒ RGB
Returns a new instance of RGB.
8 9 10 11 12 |
# File 'lib/rgb.rb', line 8 def initialize(r=0.0, g=0.0, b=0.0) @r = r.abs.to_f @g = g.abs.to_f @b = b.abs.to_f end |
Instance Attribute Details
#b ⇒ Object
Returns the value of attribute b.
5 6 7 |
# File 'lib/rgb.rb', line 5 def b @b end |
#g ⇒ Object
Returns the value of attribute g.
5 6 7 |
# File 'lib/rgb.rb', line 5 def g @g end |
#r ⇒ Object
Returns the value of attribute r.
5 6 7 |
# File 'lib/rgb.rb', line 5 def r @r end |
Instance Method Details
#hex ⇒ Object
41 42 43 |
# File 'lib/rgb.rb', line 41 def hex eval(sprintf("0x%02X%02X%02X", r*255, g*255, b*255)) end |
#to_hsv ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rgb.rb', line 14 def to_hsv min = [@r, @g, @b].min max = [@r, @g, @b].max delta = max - min if(max == 0) then s = 0 #Techically if s = 0, then v is undefined else s = delta / max end if(@r == max) then #Yellow and Magenta h = (@g - @b) / delta elsif (g == max) then #Cyan and Yellow h = 2 + (@b - @r) / delta else #Magenta and Cyan h = 4 + (@r - @g) / delta end h *= 60 HSV.new((h + 360) % 360,s,max) end |
#web_hex ⇒ Object
37 38 39 |
# File 'lib/rgb.rb', line 37 def web_hex sprintf("#%02X%02X%02X", r*255, g*255, b*255) end |
#web_safe(depth = 1) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/rgb.rb', line 45 def web_safe(depth=1) "#" + sprintf("%X", r*15) * depth + sprintf("%X", g*15) * depth + sprintf("%X", b*15) * depth end |