Class: Unmagic::Color::Component
- Inherits:
-
Data
- Object
- Data
- Unmagic::Color::Component
- Includes:
- Comparable
- Defined in:
- lib/unmagic/color.rb
Overview
Base unit for RGB components (0-255)
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#*(other) ⇒ Component
New component.
-
#+(other) ⇒ Component
New component.
-
#-(other) ⇒ Component
New component.
-
#/(other) ⇒ Component
New component.
-
#<=>(other) ⇒ Integer?
Comparison result.
-
#abs ⇒ Component
Absolute value.
-
#initialize(value:) ⇒ Component
constructor
A new instance of Component.
-
#to_f ⇒ Float
Component value as float.
-
#to_i ⇒ Integer
Component value as integer.
Constructor Details
#initialize(value:) ⇒ Component
Returns a new instance of Component.
145 146 147 |
# File 'lib/unmagic/color.rb', line 145 def initialize(value:) super(value: value.to_i.clamp(0, 255)) end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value
141 142 143 |
# File 'lib/unmagic/color.rb', line 141 def value @value end |
Instance Method Details
#*(other) ⇒ Component
Returns New component.
166 167 168 |
# File 'lib/unmagic/color.rb', line 166 def *(other) self.class.new(value: value * other.to_f) end |
#+(other) ⇒ Component
Returns New component.
178 179 180 |
# File 'lib/unmagic/color.rb', line 178 def +(other) self.class.new(value: value + other.to_f) end |
#-(other) ⇒ Component
Returns New component.
184 185 186 |
# File 'lib/unmagic/color.rb', line 184 def -(other) self.class.new(value: value - other.to_f) end |
#/(other) ⇒ Component
Returns New component.
172 173 174 |
# File 'lib/unmagic/color.rb', line 172 def /(other) self.class.new(value: value / other.to_f) end |
#<=>(other) ⇒ Integer?
Returns Comparison result.
157 158 159 160 161 162 |
# File 'lib/unmagic/color.rb', line 157 def <=>(other) case other when Component, Numeric value <=> other.to_f end end |
#abs ⇒ Component
Returns Absolute value.
189 190 191 |
# File 'lib/unmagic/color.rb', line 189 def abs self.class.new(value: value.abs) end |
#to_f ⇒ Float
Returns Component value as float.
153 |
# File 'lib/unmagic/color.rb', line 153 def to_f = value.to_f |
#to_i ⇒ Integer
Returns Component value as integer.
150 |
# File 'lib/unmagic/color.rb', line 150 def to_i = value |