Class: Unmagic::Color::Component

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
lib/unmagic/color.rb

Overview

Base unit for RGB components (0-255)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:) ⇒ Component

Returns a new instance of Component.

Parameters:

  • value (Numeric)

    Component value (0-255)



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

#valueObject (readonly)

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



141
142
143
# File 'lib/unmagic/color.rb', line 141

def value
  @value
end

Instance Method Details

#*(other) ⇒ Component

Returns New component.

Parameters:

  • other (Numeric)

    Multiplier

Returns:



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.

Parameters:

  • other (Numeric)

    Value to add

Returns:



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.

Parameters:

  • other (Numeric)

    Value to subtract

Returns:



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.

Parameters:

  • other (Numeric)

    Divisor

Returns:



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.

Parameters:

  • other (Component, Numeric)

    Value to compare

Returns:

  • (Integer, nil)

    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

#absComponent

Returns Absolute value.

Returns:



189
190
191
# File 'lib/unmagic/color.rb', line 189

def abs
  self.class.new(value: value.abs)
end

#to_fFloat

Returns Component value as float.

Returns:

  • (Float)

    Component value as float



153
# File 'lib/unmagic/color.rb', line 153

def to_f = value.to_f

#to_iInteger

Returns Component value as integer.

Returns:

  • (Integer)

    Component value as integer



150
# File 'lib/unmagic/color.rb', line 150

def to_i = value