mirror of
https://github.com/lifestorm/wnsrc.git
synced 2025-12-16 21:33:46 +03:00
142 lines
3.5 KiB
Lua
142 lines
3.5 KiB
Lua
--[[
|
|
| This file was obtained through the combined efforts
|
|
| of Madbluntz & Plymouth Antiquarian Society.
|
|
|
|
|
| Credits: lifestorm, Gregory Wayne Rossel JR.,
|
|
| Maloy, DrPepper10 @ RIP, Atle!
|
|
|
|
|
| Visit for more: https://plymouth.thetwilightzone.ru/
|
|
--]]
|
|
|
|
|
|
local COLOR = {}
|
|
COLOR.__index = COLOR
|
|
|
|
--[[---------------------------------------------------------
|
|
Register our metatable to make it accessible using FindMetaTable
|
|
-----------------------------------------------------------]]
|
|
|
|
debug.getregistry().Color = COLOR
|
|
|
|
--[[---------------------------------------------------------
|
|
To easily create a color table
|
|
-----------------------------------------------------------]]
|
|
function Color( r, g, b, a )
|
|
|
|
a = a or 255
|
|
return setmetatable( { r = math.min( tonumber(r), 255 ), g = math.min( tonumber(g), 255 ), b = math.min( tonumber(b), 255 ), a = math.min( tonumber(a), 255 ) }, COLOR )
|
|
|
|
end
|
|
|
|
--[[---------------------------------------------------------
|
|
Change the alpha of a color
|
|
-----------------------------------------------------------]]
|
|
function ColorAlpha( c, a )
|
|
|
|
return Color( c.r, c.g, c.b, a )
|
|
|
|
end
|
|
|
|
--[[---------------------------------------------------------
|
|
Checks if the given varible is a color object
|
|
-----------------------------------------------------------]]
|
|
function IsColor( obj )
|
|
|
|
return getmetatable(obj) == COLOR
|
|
|
|
end
|
|
|
|
|
|
--[[---------------------------------------------------------
|
|
Returns color as a string
|
|
-----------------------------------------------------------]]
|
|
function COLOR:__tostring()
|
|
|
|
return string.format( "%d %d %d %d", self.r, self.g, self.b, self.a )
|
|
|
|
end
|
|
|
|
--[[---------------------------------------------------------
|
|
Compares two colors
|
|
-----------------------------------------------------------]]
|
|
function COLOR:__eq( c )
|
|
|
|
return self.r == c.r and self.g == c.g and self.b == c.b and self.a == c.a
|
|
|
|
end
|
|
|
|
--[[---------------------------------------------------------
|
|
Converts a color to HSL color space
|
|
-----------------------------------------------------------]]
|
|
function COLOR:ToHSL()
|
|
|
|
return ColorToHSL( self )
|
|
|
|
end
|
|
|
|
--[[---------------------------------------------------------
|
|
Converts a color to HSV
|
|
-----------------------------------------------------------]]
|
|
function COLOR:ToHSV()
|
|
|
|
return ColorToHSV( self )
|
|
|
|
end
|
|
|
|
--[[---------------------------------------------------------
|
|
Converts color to vector - loss of precision / alpha lost
|
|
-----------------------------------------------------------]]
|
|
function COLOR:ToVector()
|
|
|
|
return Vector( self.r / 255, self.g / 255, self.b / 255 )
|
|
|
|
end
|
|
|
|
--[[---------------------------------------------------------
|
|
Unpacks the color into four variables
|
|
-----------------------------------------------------------]]
|
|
function COLOR:Unpack()
|
|
|
|
return self.r, self.g, self.b, self.a
|
|
|
|
end
|
|
|
|
function COLOR:Lerp( target_clr, frac )
|
|
|
|
return Color( Lerp( frac, self.r, target_clr.r ), Lerp( frac, self.g, target_clr.g ), Lerp( frac, self.b, target_clr.b ), Lerp( frac, self.a, target_clr.a ) )
|
|
|
|
end
|
|
|
|
function COLOR:SetUnpacked( r, g, b, a )
|
|
|
|
self.r = r or 255
|
|
self.g = g or 255
|
|
self.b = b or 255
|
|
self.a = a or 255
|
|
|
|
end
|
|
|
|
function COLOR:ToTable()
|
|
|
|
return { self.r, self.g, self.b, self.a }
|
|
|
|
end
|
|
|
|
local imat = FindMetaTable( "IMaterial" )
|
|
|
|
if ( imat.GetColor4Part ) then
|
|
function imat:GetColor( ... )
|
|
return Color( self:GetColor4Part( ... ) )
|
|
end
|
|
|
|
else
|
|
|
|
-- For those clients that do not have the above function yet
|
|
-- TODO: Delete me
|
|
local oldGetColor = imat.GetColor
|
|
function imat:GetColor( ... )
|
|
local tbl = oldGetColor( self, ... )
|
|
return Color( tbl.r, tbl.g, tbl.b, tbl.a )
|
|
end
|
|
end
|