Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math for Color class #1971

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions garrysmod/lua/includes/util/color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,83 @@ function COLOR:__tostring()

end

--[[---------------------------------------------------------
Returns unary color
-----------------------------------------------------------]]
function COLOR:__unm()
Be1zebub marked this conversation as resolved.
Show resolved Hide resolved

return self:GetInverted()

end

--[[---------------------------------------------------------
Returns colors summ
-----------------------------------------------------------]]

function COLOR:__add( other )

return Color( self.r + other.r, self.g + other.g, self.b + other.b, self.a + other.a )

end

--[[---------------------------------------------------------
Returns colors subtraction
-----------------------------------------------------------]]

function COLOR:__sub(other)

return Color( self.r - other.r, self.g - other.g, self.b - other.b, self.a - other.a )

end

--[[---------------------------------------------------------
Returns color multiplication with another color or number
-----------------------------------------------------------]]

function COLOR:__mul(other)

if ( type( other ) == "number" ) then
return Color( self.r * other, self.g * other, self.b * other, self.a * other )
end

return Color( self.r * other.r, self.g * other.g, self.b * other.b, self.a * other.a )

end

--[[---------------------------------------------------------
Returns color division with another color or number
-----------------------------------------------------------]]

function COLOR:__div( other )

if ( type( other ) == "number" ) then
return Color( self.r / other, self.g / other, self.b / other, self.a / other )
end

return Color( self.r / other.r, self.g / other.g, self.b / other.b, self.a / other.a )

end

--[[---------------------------------------------------------
Returns is color darker than other
-----------------------------------------------------------]]

function COLOR:__lt( other )

return select( 3, self:ToHSL() ) < select( 3, other:ToHSL() )

end

--[[---------------------------------------------------------
Returns is color darker than other or have the same lightness
-----------------------------------------------------------]]

function COLOR:__le( other )

return select( 3, self:ToHSL() ) <= select( 3, other:ToHSL() )

end

--[[---------------------------------------------------------
Compares two colors
-----------------------------------------------------------]]
Expand Down Expand Up @@ -105,3 +182,80 @@ function COLOR:ToTable()
return { self.r, self.g, self.b, self.a }

end

function COLOR:Copy()

return Color( self.r, self.g, self.b, self.a )

end

function COLOR:Invert()

self.r = 255 - self.r
self.g = 255 - self.g
self.b = 255 - self.b

end

function COLOR:GetInverted()

local col = self:Copy()
col:Invert()

return col

end

function COLOR:Normalize()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use case of Normalize? Currently it just divides the color components by 255, i.e., if you ignore copying, it is equivalent to color / 255. Wouldn't clamping the components to the range of 0…255 make more sense for a function like this?


self.r = self.r / 255
self.g = self.g / 255
self.b = self.b / 255
self.a = self.a / 255

end

function COLOR:GetNormalized()

local col = self:Copy()
col:Normalize()

return col

end

function COLOR:Add( other )

self.r = self.r + other.r
self.g = self.g + other.g
self.b = self.b + other.b
self.a = self.a + other.a

end

function COLOR:Sub( other )

self.r = self.r - other.r
self.g = self.g - other.g
self.b = self.b - other.b
self.a = self.a - other.a

end

function COLOR:Mul( multiplier )

self.r = self.r * multiplier
self.g = self.g * multiplier
self.b = self.b * multiplier
self.a = self.a * multiplier

end

function COLOR:Div( divisor )

self.r = self.r / divisor
self.g = self.g / divisor
self.b = self.b / divisor
self.a = self.a / divisor

end