Class: Color::RGB

Inherits:
Base
  • Object
show all
Defined in:
lib/color.rb

Instance Method Summary collapse

Methods inherited from Base

#+, #-, #==, inherited, #initialize, #primary_components, #to_a, #to_s

Constructor Details

This class inherits a constructor from Color::Base

Instance Method Details

#css=(string) ⇒ Object



87
88
89
90
# File 'lib/color.rb', line 87

def css=(string)
  string.gsub!('#', '')
  self.r, self.g, self.b = string.split(//).in_groups_of(string.length/3).map {|a| a.join.to_i(16).to_f / (16 ** (string.length/3) - 1).to_f}
end

#to_cssObject



109
110
111
# File 'lib/color.rb', line 109

def to_css
  "%02X" * 3 % @components.map {|c| c * 255}
end

#to_hslObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/color.rb', line 92

def to_hsl
  max = primary_components.max
  min = primary_components.min
  compute_hue = proc do |numerator, degrees|
    degrees(60) * (numerator / (max - min)) + degrees(degrees)
  end
  l = (max + min)/2.0
  s = (epsilon(l) || epsilon(max - min)) ? 0 :
    l <= 0.5 ? (max - min) / (max + min) :
      (max - min) / (2 - (max + min))
  h = epsilon(max - min) ? 0 :
      max == r ? compute_hue[g - b, g >= b ? 0 : 360] :
      max == g ? compute_hue[b - r, 120] :
                 compute_hue[r - g, 240]
  HSL.new(h, s, l, a)
end

#to_rgbObject



113
114
115
# File 'lib/color.rb', line 113

def to_rgb
  self
end