Class: Three::Color

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/three/math/color.rb

Constant Summary collapse

DEFAULT =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r = DEFAULT, g = nil, b = nil) ⇒ Color

Returns a new instance of Color.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/three/math/color.rb', line 11

def initialize(r = DEFAULT, g = nil, b = nil)
  @on_change_callback = proc {}

  if r.equal?(DEFAULT)
    set_rgb(1, 1, 1)
  elsif g.nil? && b.nil?
    set(r)
  else
    set_rgb(r, g, b)
  end
end

Instance Attribute Details

#bObject

Returns the value of attribute b.



7
8
9
# File 'lib/three/math/color.rb', line 7

def b
  @b
end

#gObject

Returns the value of attribute g.



7
8
9
# File 'lib/three/math/color.rb', line 7

def g
  @g
end

#rObject

Returns the value of attribute r.



7
8
9
# File 'lib/three/math/color.rb', line 7

def r
  @r
end

Instance Method Details

#==(other) ⇒ Object



100
101
102
# File 'lib/three/math/color.rb', line 100

def ==(other)
  other.is_a?(self.class) && equals?(other)
end

#cloneObject



83
84
85
# File 'lib/three/math/color.rb', line 83

def clone
  self.class.new(@r, @g, @b)
end

#copy(color) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/three/math/color.rb', line 75

def copy(color)
  @r = color.r
  @g = color.g
  @b = color.b
  changed!
  self
end

#deconstructObject



116
117
118
# File 'lib/three/math/color.rb', line 116

def deconstruct
  to_a
end

#each {|@r| ... } ⇒ Object

Yields:

  • (@r)


104
105
106
107
108
109
110
# File 'lib/three/math/color.rb', line 104

def each
  return enum_for(:each) unless block_given?

  yield @r
  yield @g
  yield @b
end

#equals?(color, epsilon: 0.0) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
# File 'lib/three/math/color.rb', line 94

def equals?(color, epsilon: 0.0)
  (@r - color.r).abs <= epsilon &&
    (@g - color.g).abs <= epsilon &&
    (@b - color.b).abs <= epsilon
end

#hexObject



87
88
89
90
91
92
# File 'lib/three/math/color.rb', line 87

def hex
  r = (@r * 255).round.clamp(0, 255)
  g = (@g * 255).round.clamp(0, 255)
  b = (@b * 255).round.clamp(0, 255)
  (r << 16) ^ (g << 8) ^ b
end

#on_change(&callback) ⇒ Object Also known as: _on_change



120
121
122
123
# File 'lib/three/math/color.rb', line 120

def on_change(&callback)
  @on_change_callback = callback || proc {}
  self
end

#set(value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/three/math/color.rb', line 38

def set(value)
  case value
  when Color
    copy(value)
  when Integer
    set_hex(value)
  when String
    set_style(value)
  else
    set_rgb(value, value, value)
  end
end

#set_hex(hex) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/three/math/color.rb', line 59

def set_hex(hex)
  hex = hex.to_i
  @r = ((hex >> 16) & 255) / 255.0
  @g = ((hex >> 8) & 255) / 255.0
  @b = (hex & 255) / 255.0
  changed!
  self
end

#set_rgb(r, g, b) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/three/math/color.rb', line 51

def set_rgb(r, g, b)
  @r = r
  @g = g
  @b = b
  changed!
  self
end

#set_style(style) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
73
# File 'lib/three/math/color.rb', line 68

def set_style(style)
  value = style.start_with?("#") ? style[1..] : style
  raise ArgumentError, "unsupported color style: #{style}" unless value&.match?(/\A[0-9a-fA-F]{6}\z/)

  set_hex(value.to_i(16))
end

#to_aObject



112
113
114
# File 'lib/three/math/color.rb', line 112

def to_a
  [@r, @g, @b]
end