Class: Mittsu::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/mittsu/math/color.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Color

Returns a new instance of Color.



7
8
9
10
11
12
13
14
# File 'lib/mittsu/math/color.rb', line 7

def initialize(*args)
  case args.length
  when 3 then self.set_rgb(*args)
  when 1 then self.set(args.first)
  when 0 then self.set_rgb(1.0, 1.0, 1.0)
  else raise ArgumentError, "Arguments must be (r, g, b), (color), or none"
  end
end

Instance Attribute Details

#bObject

Returns the value of attribute b.



5
6
7
# File 'lib/mittsu/math/color.rb', line 5

def b
  @b
end

#gObject

Returns the value of attribute g.



5
6
7
# File 'lib/mittsu/math/color.rb', line 5

def g
  @g
end

#rObject

Returns the value of attribute r.



5
6
7
# File 'lib/mittsu/math/color.rb', line 5

def r
  @r
end

Instance Method Details

#add(color) ⇒ Object



176
177
178
179
180
181
# File 'lib/mittsu/math/color.rb', line 176

def add(color)
  @r += color.r
  @g += color.g
  @b += color.b
  self
end

#add_colors(color1, color2) ⇒ Object



183
184
185
186
187
188
# File 'lib/mittsu/math/color.rb', line 183

def add_colors(color1, color2)
  @r = color1.r + color2.r
  @g = color1.g + color2.g
  @b = color1.b + color2.b
  self
end

#add_scalar(s) ⇒ Object



190
191
192
193
194
195
# File 'lib/mittsu/math/color.rb', line 190

def add_scalar(s)
  @r += s
  @g += s
  @b += s
  self
end

#cloneObject



236
237
238
# File 'lib/mittsu/math/color.rb', line 236

def clone
  Mittsu::Color.new(@r, @g, @b)
end

#convert_gamma_to_linearObject



113
114
115
116
117
118
119
# File 'lib/mittsu/math/color.rb', line 113

def convert_gamma_to_linear
  rr, gg, bb = @r, @g, @b
  @r = rr * rr
  @g = gg * gg
  @b = bb * bb
  self
end

#convert_linear_to_gammaObject



121
122
123
124
125
126
# File 'lib/mittsu/math/color.rb', line 121

def convert_linear_to_gamma
  @r = Math.sqrt(@r)
  @g = Math.sqrt(@g)
  @b = Math.sqrt(@b)
  self
end

#copy(color) ⇒ Object



91
92
93
94
95
96
# File 'lib/mittsu/math/color.rb', line 91

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

#copy_gamma_to_linear(color, gamma_factor = 2.0) ⇒ Object



98
99
100
101
102
103
# File 'lib/mittsu/math/color.rb', line 98

def copy_gamma_to_linear(color, gamma_factor = 2.0)
  @r = color.r ** gamma_factor
  @g = color.g ** gamma_factor
  @b = color.b ** gamma_factor
  self
end

#copy_linear_to_gamma(color, gamma_factor = 2.0) ⇒ Object



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

def copy_linear_to_gamma(color, gamma_factor = 2.0)
  safe_inverse = (gamma_factor > 0) ? (1.0 / gamma_factor) : 1.0
  @r = color.r ** safe_inverse
  @g = color.g ** safe_inverse
  @b = color.b ** safe_inverse
  self
end

#equals(c) ⇒ Object



218
219
220
# File 'lib/mittsu/math/color.rb', line 218

def equals(c)
  (c.r == @r) && (c.g == @g) && (c.b == @b)
end

#from_array(array) ⇒ Object



222
223
224
225
226
227
# File 'lib/mittsu/math/color.rb', line 222

def from_array(array)
  @r = array[0]
  @g = array[1]
  @b = array[2]
  self
end

#hexObject



128
129
130
# File 'lib/mittsu/math/color.rb', line 128

def hex
  (@r * 255).to_i << 16 ^ (@g * 255).to_i << 8 ^ (@b * 255).to_i << 0
end

#hex_stringObject



132
133
134
# File 'lib/mittsu/math/color.rb', line 132

def hex_string
  ('000000' + self.hex.to_s(16))[-6..-1]
end

#hsl(target = nil) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/mittsu/math/color.rb', line 136

def hsl(target = nil)
  # h,s,l ranges are in 0.0 - 1.0
  hsl = target || { h: 0.0, s: 0.0, l: 0.0 }
  rr, gg, bb = @r, @g, @b
  max = [r, g, b].max
  min = [r, g, b].min
  hue, saturation = nil, nil
  lightness = (min + max) / 2.0
  if min == max
    hue = 0.0
    saturation = 0.0
  else
    delta = max - min
    saturation = lightness <= 0.5 ? delta / (max + min) : delta / (2.0 - max - min)
    case max
    when rr then hue = (gg - bb) / delta + (gg < bb ? 6.0 : 0.0)
    when gg then hue = (bb - rr) / delta + 2.0
    when bb then hue = (rr - gg) / delta + 4.0
    end
    hue /= 6.0
  end
  hsl[:h] = hue
  hsl[:s] = saturation
  hsl[:l] = lightness
  hsl
end

#lerp(color, alpha) ⇒ Object



211
212
213
214
215
216
# File 'lib/mittsu/math/color.rb', line 211

def lerp(color, alpha)
  @r += (color.r - @r) * alpha
  @g += (color.g - @g) * alpha
  @b += (color.b - @b) * alpha
  self
end

#multiply(color) ⇒ Object



197
198
199
200
201
202
# File 'lib/mittsu/math/color.rb', line 197

def multiply(color)
  @r *= color.r
  @g *= color.g
  @b *= color.b
  self
end

#multiply_scalar(s) ⇒ Object



204
205
206
207
208
209
# File 'lib/mittsu/math/color.rb', line 204

def multiply_scalar(s)
  @r *= s
  @g *= s
  @b *= s
  self
end

#offset_hsl(h, s, l) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/mittsu/math/color.rb', line 167

def offset_hsl(h, s, l)
  hsl = self.hsl
  hsl[:h] += h
  hsl[:s] += s
  hsl[:l] += l
  self.set_hsl(hsl[:h], hsl[:s], hsl[:l])
  self
end

#set(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mittsu/math/color.rb', line 16

def set(value)
  case value
  when Color
    self.copy(value)
  when Fixnum
    self.set_hex(value)
  when String
    self.set_style(value)
  else
    raise ArgumentError, "Arguments must be Color, Fixnum or String"
  end
  self
end

#set_hex(hex) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mittsu/math/color.rb', line 30

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

#set_hsl(h, s, l) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mittsu/math/color.rb', line 45

def set_hsl(h, s, l)
  # h,s,l ranges are in 0.0 - 1.0
  if s.zero?
    @r = @g = @b = l
  else
    p = l <= 0.5 ? l * (1.0 + s) : l + s - (l * s)
    q = (2.0 * l) - p
    @r = hue2rgb(q, p, h + 1.0 / 3.0)
    @g = hue2rgb(q, p, h)
    @b = hue2rgb(q, p, h - 1.0 / 3.0)
  end
  self
end

#set_rgb(r, g, b) ⇒ Object



38
39
40
41
42
43
# File 'lib/mittsu/math/color.rb', line 38

def set_rgb(r, g, b)
  @r = r.to_f
  @g = g.to_f
  @b = b.to_f
  self
end

#set_style(style) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mittsu/math/color.rb', line 59

def set_style(style)
  # rgb(255,0,0)
  if /^rgb\((\d+), ?(\d+), ?(\d+)\)$/i =~ style
    @r = [255.0, $1.to_f].min / 255.0
    @g = [255.0, $2.to_f].min / 255.0
    @b = [255.0, $3.to_f].min / 255.0
    return self
  end
  # rgb(100%,0%,0%)
  if /^rgb\((\d+)\%, ?(\d+)\%, ?(\d+)\%\)$/i =~ style
    @r = [100.0, $1.to_f].min / 100.0
    @g = [100.0, $2.to_f].min / 100.0
    @b = [100.0, $3.to_f].min / 100.0
    return self
  end
  # #ff0000
  if /^\#([0-9a-f]{6})$/i =~ style
    self.set_hex($1.hex)
    return self
  end
  # #f00
  if /^\#([0-9a-f])([0-9a-f])([0-9a-f])$/i =~ style
    self.set_hex(($1 + $1 + $2 + $2 + $3 + $3).hex)
    return self
  end
  # red
  if /^(\w+)$/i =~ style
    self.set_hex(Mittsu::ColorKeywords[style])
    return self
  end
end

#styleObject



163
164
165
# File 'lib/mittsu/math/color.rb', line 163

def style
  "rgb(#{ (@r * 255).to_i },#{ (@g * 255).to_i },#{ (@b * 255).to_i })"
end

#to_array(array = [], offset = 0) ⇒ Object



229
230
231
232
233
234
# File 'lib/mittsu/math/color.rb', line 229

def to_array(array = [], offset = 0)
  array[offset] = @r
  array[offset + 1] = @g
  array[offset + 2] = @b
  array
end