From afe8e2f4bbfbf8385adee574f308bc4ddbc2eef0 Mon Sep 17 00:00:00 2001 From: Beelzebub <34854689+Be1zebub@users.noreply.github.com> Date: Sun, 28 May 2023 23:11:08 +0300 Subject: [PATCH 1/4] color math --- garrysmod/lua/includes/util/color.lua | 112 ++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/garrysmod/lua/includes/util/color.lua b/garrysmod/lua/includes/util/color.lua index 69d241fedd..314fa796b7 100644 --- a/garrysmod/lua/includes/util/color.lua +++ b/garrysmod/lua/includes/util/color.lua @@ -46,6 +46,86 @@ function COLOR:__tostring() end +--[[--------------------------------------------------------- + Returns unary color +-----------------------------------------------------------]] +function COLOR:__unm() + + local col = self:Copy() + col:Invert() + + return col + +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 -----------------------------------------------------------]] @@ -105,3 +185,35 @@ function COLOR:ToTable() return { self.r, self.g, self.b, self.a } end + +function COLOR:Invert() + + self.r = math.abs( 255 - self.r ) + self.g = math.abs( 255 - self.g ) + self.b = math.abs( 255 - self.b ) + +end + +function COLOR:Copy() + + return Color( self.r, self.g, self.b, self.a ) + +end + +function COLOR:Normalize() + + self.r = self.r / 255 + self.g = self.r / 255 + self.b = self.r / 255 + self.a = self.r / 255 + +end + +function COLOR:GetNormalized() + + local col = self:Copy() + col:Normalize() + + return col + +end From d9e9ed749c0058fb9106292646f9cd559c735e10 Mon Sep 17 00:00:00 2001 From: Beelzebub <34854689+Be1zebub@users.noreply.github.com> Date: Tue, 30 May 2023 05:00:08 +0300 Subject: [PATCH 2/4] fix typo --- garrysmod/lua/includes/util/color.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/garrysmod/lua/includes/util/color.lua b/garrysmod/lua/includes/util/color.lua index 314fa796b7..cfee460301 100644 --- a/garrysmod/lua/includes/util/color.lua +++ b/garrysmod/lua/includes/util/color.lua @@ -203,9 +203,9 @@ end function COLOR:Normalize() self.r = self.r / 255 - self.g = self.r / 255 - self.b = self.r / 255 - self.a = self.r / 255 + self.g = self.g / 255 + self.b = self.b / 255 + self.a = self.a / 255 end From f75d8a62f010e380e3d8db34e2ef05c8129a362d Mon Sep 17 00:00:00 2001 From: Beelzebub <34854689+Be1zebub@users.noreply.github.com> Date: Tue, 30 May 2023 16:52:56 +0300 Subject: [PATCH 3/4] fix :Invert & :__unm, add :GetInverted --- garrysmod/lua/includes/util/color.lua | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/garrysmod/lua/includes/util/color.lua b/garrysmod/lua/includes/util/color.lua index cfee460301..099d90d98f 100644 --- a/garrysmod/lua/includes/util/color.lua +++ b/garrysmod/lua/includes/util/color.lua @@ -51,10 +51,7 @@ end -----------------------------------------------------------]] function COLOR:__unm() - local col = self:Copy() - col:Invert() - - return col + return self:GetInverted() end @@ -186,17 +183,26 @@ function COLOR:ToTable() end +function COLOR:Copy() + + return Color( self.r, self.g, self.b, self.a ) + +end + function COLOR:Invert() - self.r = math.abs( 255 - self.r ) - self.g = math.abs( 255 - self.g ) - self.b = math.abs( 255 - self.b ) + self.r = 255 - self.r + self.g = 255 - self.g + self.b = 255 - self.b end -function COLOR:Copy() +function COLOR:GetInverted() - return Color( self.r, self.g, self.b, self.a ) + local col = self:Copy() + col:Invert() + + return col end From 428cc42c605f3fff510c28db088af38c2c7931b8 Mon Sep 17 00:00:00 2001 From: Beelzebub <34854689+Be1zebub@users.noreply.github.com> Date: Wed, 31 May 2023 03:07:38 +0300 Subject: [PATCH 4/4] add :Add, :Sub, :Mul, :Div --- garrysmod/lua/includes/util/color.lua | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/garrysmod/lua/includes/util/color.lua b/garrysmod/lua/includes/util/color.lua index 099d90d98f..63f545956c 100644 --- a/garrysmod/lua/includes/util/color.lua +++ b/garrysmod/lua/includes/util/color.lua @@ -223,3 +223,39 @@ function COLOR:GetNormalized() 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