-
-
Save K4thos/6893e5b458cbfa1358f318988199a242 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---@module 'common.Class' | |
---Color object class. | |
---@class Color : Class | |
---@operator add(Color): Color | |
---@operator sub(Color): Color | |
---@operator mul(Color): Color | |
---@field r number @Red value in RGB color model. (int: 0:255) | |
---@field g number @Green value in RGB color model. (int: 0:255) | |
---@field b number @Blue value in RGB color model. (int: 0:255) | |
---@field src number @Source alpha. (int: 0:255) | |
---@field dst number @Destination alpha. (int: 0:255) | |
Color = Class:new('Color') | |
do | |
--;=========================================================== | |
--; PROPERTIES | |
--;=========================================================== | |
Color.r = 255 | |
Color.g = 255 | |
Color.b = 255 | |
Color.src = 255 | |
Color.dst = 255 | |
--;=========================================================== | |
--; CONSTRUCTORS | |
--;=========================================================== | |
---Constructor for creating new object instance. | |
---@param t table<string, number> @Table storing data for object creation. | |
---@return Color @New object instance. | |
function Color:new(t) | |
t = t or {} | |
setmetatable(t, self) | |
return t | |
end | |
--;=========================================================== | |
--; METHODS | |
--;=========================================================== | |
---Add operator overloading. Adds RGB. (color + color) | |
---@param colorA Color @First arg. | |
---@param colorB Color @Second arg. | |
---@return Color @New object instance. | |
function Color.__add(colorA, colorB) | |
local r = math.max(0, math.min(colorA.r + colorB.r, 255)) | |
local g = math.max(0, math.min(colorA.g + colorB.g, 255)) | |
local b = math.max(0, math.min(colorA.b + colorB.b, 255)) | |
return Color:new({r = r, g = g, b = b, src = colorA.src, dst = colorA.dst}) | |
end | |
---Sub operator overloading. Subtracts RGB. (color - color) | |
---@param colorA Color @First arg. | |
---@param colorB Color @Second arg. | |
---@return Color @New object instance. | |
function Color.__sub(colorA, colorB) | |
local r = math.max(0, math.min(colorA.r - colorB.r, 255)) | |
local g = math.max(0, math.min(colorA.g - colorB.g, 255)) | |
local b = math.max(0, math.min(colorA.b - colorB.b, 255)) | |
return Color:new({r = r, g = g, b = b, src = colorA.src, dst = colorA.dst}) | |
end | |
---Mul operator overloading. Multiplies RGB. (color * color) | |
---@param colorA Color @First arg. | |
---@param colorB Color @Second arg. | |
---@return Color @New object instance. | |
function Color.__mul(colorA, colorB) | |
local r = (colorA.r / 255) * (colorB.r / 255) * 255 | |
local g = (colorA.g / 255) * (colorB.g / 255) * 255 | |
local b = (colorA.b / 255) * (colorB.b / 255) * 255 | |
return Color:new({r = r, g = g, b = b, src = colorA.src, dst = colorA.dst}) | |
end | |
---Eq operator overloading. Compares r, g, b, src, and dst (color == color) | |
---@param colorA Color @First arg. | |
---@param colorB Color @Second arg. | |
---@return boolean @True if objects are the same. | |
function Color.__eq(colorA, colorB) | |
if colorA.r == colorB.r and colorA.g == colorB.g and colorA.b == colorB.b and colorA.src == colorB.src and colorA.dst == colorB.dst then | |
return true | |
else | |
return false | |
end | |
end | |
---Creates a new object instance from *hex* value. | |
---@param hex string @Color values as hex string. | |
---@return Color @New object instance. | |
function Color:fromHex(hex) | |
hex = tostring(hex) | |
if hex:sub(0, 1) == "#" then hex = hex:sub(2, -1) end | |
if hex:sub(0, 2) == "0x" then hex = hex:sub(3, -1) end | |
local r = tonumber(hex:sub(1, 2), 16) | |
local g = tonumber(hex:sub(3, 4), 16) | |
local b = tonumber(hex:sub(5, 6), 16) | |
local src = tonumber(hex:sub(7, 8), 16) or 255 | |
local dst = tonumber(hex:sub(9, 10), 16) or 0 | |
return Color:new({r = r, g = g, b = b, src = src, dst = dst}) | |
end | |
---Creates string of *Color* converted to hex. | |
---@return string @Color values as hex string. | |
function Color:toHex() | |
local r = string.format("%x", self.r) | |
local g = string.format("%x", self.g) | |
local b = string.format("%x", self.b) | |
local src = string.format("%x", self.src) | |
local dst = string.format("%x", self.dst) | |
local hex = tostring((r:len() < 2 and "0") .. r .. (g:len() < 2 and "0") .. g .. (b:len() < 2 and "0") .. b .. (src:len() < 2 and "0") .. src .. (dst:len() < 2 and "0") .. dst) | |
return hex | |
end | |
---Returns RGB and alpha values stored in Color object. | |
---@return number, number, number, number, number @Red, Green, Blue values in RGB color model, source and destination alpha. | |
function Color:unpack() | |
return tonumber(self.r) or 255, tonumber(self.g) or 255, tonumber(self.b) or 255, tonumber(self.src) or 255, tonumber(self.dst) or 255 | |
end | |
end | |
return Color |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment