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)



123
124
125
# File 'lib/unmagic/color.rb', line 123

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



119
120
121
# File 'lib/unmagic/color.rb', line 119

def value
  @value
end

Instance Method Details

#*(other) ⇒ Component

Returns New component.

Parameters:

  • other (Numeric)

    Multiplier

Returns:



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

def *(other)
  self.class.new(value: value * other.to_f)
end

#+(other) ⇒ Component

Returns New component.

Parameters:

  • other (Numeric)

    Value to add

Returns:



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

def +(other)
  self.class.new(value: value + other.to_f)
end

#-(other) ⇒ Component

Returns New component.

Parameters:

  • other (Numeric)

    Value to subtract

Returns:



159
160
161
# File 'lib/unmagic/color.rb', line 159

def -(other)
  self.class.new(value: value - other.to_f)
end

#/(other) ⇒ Component

Returns New component.

Parameters:

  • other (Numeric)

    Divisor

Returns:



147
148
149
# File 'lib/unmagic/color.rb', line 147

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



132
133
134
135
136
137
# File 'lib/unmagic/color.rb', line 132

def <=>(other)
  case other
  when Component, Numeric
    value <=> other.to_f
  end
end

#absComponent

Returns Absolute value.

Returns:



164
165
166
# File 'lib/unmagic/color.rb', line 164

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

#to_fObject



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

def to_f = value.to_f

#to_iObject



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

def to_i = value