Class: Mittsu::Color
Constant Summary
collapse
- ELEMENTS =
{ r: 0, g: 1, b: 2 }
- DIMENSIONS =
ELEMENTS.count
Instance Attribute Summary
Attributes inherited from Vector
#elements
Instance Method Summary
collapse
Methods inherited from Vector3
#apply_axis_angle, #apply_euler, #apply_matrix3, #apply_matrix4, #apply_projection, #apply_quaternion, #cross, #cross_vectors, #distance_to_squared, #dot, #from_attribute, #length_manhattan, #project, #set_from_matrix_column, #set_from_matrix_position, #set_from_matrix_scale, #transform_direction, #unproject, #x, #x=, #y, #y=, #z, #z=
Methods inherited from Vector
#==, #[], #[]=, #add, #add_scalar, #add_vectors, #angle_to, #ceil, #clamp, #clamp_scalar, #clone, #copy, #distance_to, #divide, #divide_scalar, #each_dimension, #floor, #from_array, #length, #length_sq, #lerp, #lerp_vectors, #max, #min, #multiply, #multiply_scalar, #multiply_vectors, #negate, #normalize, #project_on_plane, #project_on_vector, #reflect, #round, #round_to_zero, #set_length, #sub, #sub_scalar, #sub_vectors, #to_array, #to_s
Constructor Details
#initialize(*args) ⇒ Color
Returns a new instance of Color.
9
10
11
12
13
14
15
16
17
|
# File 'lib/mittsu/math/color.rb', line 9
def initialize(*args)
super(0, 0, 0)
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 Method Details
#convert_gamma_to_linear ⇒ Object
115
116
117
118
119
120
121
|
# File 'lib/mittsu/math/color.rb', line 115
def convert_gamma_to_linear
rr, gg, bb = self.r, self.g, self.b
self.r = rr * rr
self.g = gg * gg
self.b = bb * bb
self
end
|
#convert_linear_to_gamma ⇒ Object
123
124
125
126
127
128
|
# File 'lib/mittsu/math/color.rb', line 123
def convert_linear_to_gamma
self.r = Math.sqrt(self.r)
self.g = Math.sqrt(self.g)
self.b = Math.sqrt(self.b)
self
end
|
#copy_gamma_to_linear(color, gamma_factor = 2.0) ⇒ Object
100
101
102
103
104
105
|
# File 'lib/mittsu/math/color.rb', line 100
def copy_gamma_to_linear(color, gamma_factor = 2.0)
self.r = color.r ** gamma_factor
self.g = color.g ** gamma_factor
self.b = color.b ** gamma_factor
self
end
|
#copy_linear_to_gamma(color, gamma_factor = 2.0) ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/mittsu/math/color.rb', line 107
def copy_linear_to_gamma(color, gamma_factor = 2.0)
safe_inverse = (gamma_factor > 0) ? (1.0 / gamma_factor) : 1.0
self.r = color.r ** safe_inverse
self.g = color.g ** safe_inverse
self.b = color.b ** safe_inverse
self
end
|
#hex ⇒ Object
130
131
132
|
# File 'lib/mittsu/math/color.rb', line 130
def hex
(self.r * 255).to_i << 16 ^ (self.g * 255).to_i << 8 ^ (self.b * 255).to_i << 0
end
|
#hex_string ⇒ Object
134
135
136
|
# File 'lib/mittsu/math/color.rb', line 134
def hex_string
('000000' + self.hex.to_s(16))[-6..-1]
end
|
#hsl(target = nil) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/mittsu/math/color.rb', line 138
def hsl(target = nil)
hsl = target || { h: 0.0, s: 0.0, l: 0.0 }
rr, gg, bb = self.r, self.g, self.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
|
#offset_hsl(h, s, l) ⇒ Object
169
170
171
172
173
174
175
176
|
# File 'lib/mittsu/math/color.rb', line 169
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
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/mittsu/math/color.rb', line 27
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
41
42
43
44
45
46
47
|
# File 'lib/mittsu/math/color.rb', line 41
def set_hex(hex)
hex = hex.floor
self.r = (hex >> 16 & 255) / 255.0
self.g = (hex >> 8 & 255) / 255.0
self.b = (hex & 255) / 255.0
self
end
|
#set_hsl(h, s, l) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/mittsu/math/color.rb', line 54
def set_hsl(h, s, l)
if s.zero?
self.r = self.g = self.b = l
else
p = l <= 0.5 ? l * (1.0 + s) : l + s - (l * s)
q = (2.0 * l) - p
self.r = hue2rgb(q, p, h + 1.0 / 3.0)
self.g = hue2rgb(q, p, h)
self.b = hue2rgb(q, p, h - 1.0 / 3.0)
end
self
end
|
#set_rgb(r, g, b) ⇒ Object
49
50
51
52
|
# File 'lib/mittsu/math/color.rb', line 49
def set_rgb(r, g, b)
@elements = [r.to_f, g.to_f, b.to_f]
self
end
|
#set_style(style) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/mittsu/math/color.rb', line 68
def set_style(style)
if /^rgb\((\d+), ?(\d+), ?(\d+)\)$/i =~ style
self.r = [255.0, $1.to_f].min / 255.0
self.g = [255.0, $2.to_f].min / 255.0
self.b = [255.0, $3.to_f].min / 255.0
return self
end
if /^rgb\((\d+)\%, ?(\d+)\%, ?(\d+)\%\)$/i =~ style
self.r = [100.0, $1.to_f].min / 100.0
self.g = [100.0, $2.to_f].min / 100.0
self.b = [100.0, $3.to_f].min / 100.0
return self
end
if /^\#([0-9a-f]{6})$/i =~ style
self.set_hex($1.hex)
return self
end
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
if /^(\w+)$/i =~ style
self.set_hex(Mittsu::ColorKeywords[style])
return self
end
end
|
#style ⇒ Object
165
166
167
|
# File 'lib/mittsu/math/color.rb', line 165
def style
"rgb(#{ (self.r * 255).to_i },#{ (self.g * 255).to_i },#{ (self.b * 255).to_i })"
end
|