1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
-- For luaJit compatibility.
if table.unpack == nil then
table.unpack = unpack
end
local Vector3 = Vector3 or require( "vector3" )
local Color = {}
local metatable = {
__index = Color,
__tostring = function( c )
return "{"..math.floor( c.r )..", "..math.floor( c.g )..", "..math.floor( c.b )..", "..math.floor( c.a ).."}"
end,
__add = function( c1, c2 )
return Color:new( c1.r + c2.r, c1.g + c2.g, c1.b + c2.b, c1.a + c2.a )
end,
__sub = function( c1, c2 )
return Color:new( c1.r - c2.r, c1.g - c2.g, c1.b - c2.b, c1.a - c2.a )
end,
__mul = function( c1, c2 )
return Color:new( c1.r * c2.r, c1.g * c2.g, c1.b * c2.b, c1.a * c2.a )
end,
__div = function( c1, c2 )
return Color:new( c1.r / c2.r, c1.g / c2.g, c1.b / c2.b, c1.a / c2.a )
end,
__mod = function( c, v )
return Color:new( c.r % v, c.g % v, c.b % v, c.a % v )
end,
__pow = function( c, v )
return Color:new( c.r ^ v, c.g ^ v, c.b ^ v, c.a ^ v )
end,
__len = function()
return 4
end,
__eq = function( c1, c2 )
return RL.FloatEquals( c1.r, c2.r )
and RL.FloatEquals( c1.g, c2.g )
and RL.FloatEquals( c1.b, c2.b )
and RL.FloatEquals( c1.a, c2.a )
end,
__concat = function( a, b )
return tostring( a )..tostring( b )
end,
}
function Color:new( r, g, b, a )
local object = setmetatable( {}, metatable )
object.r = r or 255
object.g = g or object.r
object.b = b or object.g
object.a = a or 255
return object
end
function Color:newT( t )
local object = setmetatable( {}, metatable )
object.r, object.g, object.b, object.a = table.unpack( t )
return object
end
function Color:newC( c )
local object = setmetatable( {}, metatable )
object.r = c.r
object.g = c.g
object.b = c.b
object.a = c.a
return object
end
function Color:set( r, g, b, a )
self.r = r or 255
self.g = g or self.r
self.b = b or self.g
self.a = a or 255
end
function Color:setT( t )
self.r, self.g, self.b, self.a = table.unpack( t )
end
function Color:setC( c )
self.r = c.r
self.g = c.g
self.b = c.b
self.a = c.a
end
function Color:serialize()
return "Color:new("..RL.Round( self.r )..","..RL.Round( self.g )..","..RL.Round( self.b )..","..RL.Round( self.a )..")"
end
function Color:arr()
return { self.r, self.g, self.b, self.a }
end
function Color:unpack()
return self.r, self.g, self.b, self.a
end
function Color:clone()
return Color:new( self.r, self.g, self.b, self.a )
end
function Color:scale( scalar )
return Color:new( math.floor( self.r * scalar ), math.floor( self.g * scalar ), math.floor( self.b * scalar ), math.floor( self.a * scalar ) )
end
function Color:fade( alpha )
return Color:newT( RL.Fade( self, alpha ) )
end
function Color:toHex()
return RL.ColorToInt( self )
end
function Color:fromHex( hexValue )
return Color:newT( RL.GetColor( hexValue ) )
end
function Color:getNormalized()
return RL.ColorNormalize( self )
end
function Color:fromNormalized( normalized )
return Color:newT( RL.ColorFromNormalized( normalized ) )
end
function Color:toHSV()
return Vector3:newT( RL.ColorToHSV( self ) )
end
function Color:fromHSV( hue, saturation, value )
return Color:newT( RL.ColorFromHSV( hue, saturation, value ) )
end
function Color:tint( tint )
return Color:newT( RL.ColorTint( self, tint ) )
end
function Color:brightness( factor )
return Color:newT( RL.ColorBrightness( self, factor ) )
end
function Color:contrast( contrast )
return Color:newT( RL.ColorContrast( self, contrast ) )
end
function Color:alphaBlend( dst, src, tint )
return Color:newT( RL.ColorAlphaBlend( dst, src, tint ) )
end
function Color:lerp( color, amount )
return Color:temp(
RL.Lerp( self.r, color.r, amount ),
RL.Lerp( self.g, color.g, amount ),
RL.Lerp( self.b, color.b, amount ),
RL.Lerp( self.a, color.a, amount )
):round()
end
function Color:round()
return Color:new(
RL.Round( self.r ),
RL.Round( self.g ),
RL.Round( self.b ),
RL.Round( self.a )
)
end
function Color:mix( c )
return Color:temp(
( self.r + c.r ) / 2,
( self.g + c.g ) / 2,
( self.b + c.b ) / 2,
( self.a + c.a ) / 2
):round()
end
function Color:invert()
return Color:new( 255 - self.r, 255 - self.g, 255 - self.b, self.a )
end
-- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100
local tempPool = {}
local curTemp = 1
for _ = 1, TEMP_COUNT do
table.insert( tempPool, Color:new( 255, 255, 255, 255 ) )
end
function Color:temp( r, g, b, a )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.r = r or 255
object.g = g or object.r
object.b = b or object.g
object.a = a or 255
return object
end
function Color:tempT( t )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.r, object.g, object.b, object.a = table.unpack( t )
return object
end
function Color:tempC( c )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.r = c.r
object.g = c.g
object.b = c.b
object.a = c.a
return object
end
function Color:getTempId()
return curTemp
end
return Color
|