Class: Color::Base
- Inherits:
-
Object
- Object
- Color::Base
- Defined in:
- lib/color.rb
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #==(other) ⇒ Object
-
#initialize(*components) ⇒ Base
constructor
A new instance of Base.
- #primary_components ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*components) ⇒ Base
Returns a new instance of Base.
36 37 38 39 40 41 42 43 44 |
# File 'lib/color.rb', line 36 def initialize(*components) if components[0].is_a?(String) @components = [] self.css = components[0] else @components = components.flatten.map(&method(:constrain)) end @components << 1.0 if @components.size == 3 end |
Class Method Details
.inherited(subclass) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/color.rb', line 15 def self.inherited(subclass) components = subclass.name.sub('Color::', '').downcase.split(//).map(&:to_sym) components << :a components.each_with_index do |component, i| subclass.class_eval <<-"end;" def #{component}(value=nil) if value result = self.class.new(@components) result.#{component} = value result else @components[#{i}] end end def #{component}=(x) @components[#{i}] = constrain(x) end end; end end |
Instance Method Details
#+(other) ⇒ Object
58 59 60 61 62 |
# File 'lib/color.rb', line 58 def +(other) other_array = other.to_a array = component_indicies.map {|i| @components[i] + (other_array[i] || 0)} self.class.new(*array) end |
#-(other) ⇒ Object
54 55 56 |
# File 'lib/color.rb', line 54 def -(other) self + other.to_a.map(&:-@) end |
#==(other) ⇒ Object
64 65 66 |
# File 'lib/color.rb', line 64 def ==(other) other.class == self.class && component_indicies.all? {|i| epsilon(@components[i] - other.to_a[i])} end |
#primary_components ⇒ Object
46 47 48 |
# File 'lib/color.rb', line 46 def primary_components @components[0..-2] end |
#to_a ⇒ Object
50 51 52 |
# File 'lib/color.rb', line 50 def to_a @components end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/color.rb', line 68 def to_s "<#{self.class.name}: #{@components.join(", ")}>" end |