Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------[Classe de Cores por Lynezx | rev 2 - 27/08/2016]-------
- Color = {Random = function()
- return Color(math.random(0, 255), math.random(0, 255), math.random(0, 255));
- end,
- Error = function(codigo, parametro)
- local msgs = {"[Color Class] - Invalid hex parameter [%s].",
- "[Color Class] - Invalid byte parameter [%s]. Value must be a number from 0 to 255.",
- "[Color Class] - Invalid integer parameter [%s]. Value must be a number from 0 to 16777215.",
- "[Color Class] - Invalid parameter for Color constructor [%s]. Value must be a hex string; a color integer; a R, G, B bytes sequence or nothing."
- };
- error(string.format((msgs[codigo] or "[Color Class] - Unknown error"), parametro or ""));
- end
- };
- setmetatable(Color, {
- __call = function(t, ...)
- local args = {...};
- local instancia = {
- _int = 0,
- _hex = "000000",
- _r = 0,
- _g = 0,
- _b = 0
- };
- setmetatable(instancia, {
- __index = function(self, k)
- return rawget(self, '_' .. k);
- end,
- __newindex = function(self, k, v)
- local alterado = false;
- local val;
- if (k == 'int') then
- --Integer
- if not(pcall((function()
- val = tonumber(v);
- end))) or (val == nil or val < 0 or val > 0xffffff) then
- Color.Error(3, type(v) .. ' ' .. tostring(v));
- end
- rawset(self, '_int', val);
- alterado = true;
- elseif (k == 'hex') then
- --Hex
- if not(pcall((function()
- val = tonumber("0x" .. string.gsub(v, '#', '', 1));
- end))) or val == nil then
- Color.Error(1, type(v) .. ' ' .. tostring(v));
- end
- rawset(self, '_int', val);
- alterado = true;
- elseif (k == 'r' or k == 'g' or k == 'b') then
- --Byte
- if not(pcall((function()
- val = tonumber(v);
- end))) or (val == nil or val < 0 or val > 0xff) then
- Color.Error(2, type(v) .. ' ' .. tostring(v));
- end
- rawset(self, '_' .. k, val);
- rawset(self, '_int', bit32.lshift(self.r, 16) + bit32.lshift(self.g, 8) + self.b);
- alterado = true;
- elseif not alterado then
- rawset(self, k, val);
- end
- --Finalização
- if alterado then
- rawset(self, '_r', bit32.band(bit32.rshift(self.int, 16), 0xFF));
- rawset(self, '_g', bit32.band(bit32.rshift(self.int, 8), 0xFF));
- rawset(self, '_b', bit32.band(self.int, 0xFF));
- rawset(self, '_hex', string.format('%02x%02x%02x', self.r, self.g, self.b));
- end
- end
- });
- if (#args == 1 and type(args[1]) == 'string') then
- instancia.hex = args[1];
- elseif (#args == 1 and type(args[1]) == 'number') then
- instancia.int = args[1];
- elseif (#args == 1 and type(args[1]) == 'table' and type(args[1].int) == 'number') then
- instancia.int = args[1].int;
- elseif (#args == 1) then
- Color.Error(4, type(args[1]) .. ' ' .. tostring(args[1]));
- elseif (#args == 3) then
- instancia.r = args[1];
- instancia.g = args[2];
- instancia.b = args[3];
- end
- return instancia;
- end
- });
- Color.GAME_BACKGROUND = Color('6a7495');
- Color.UI_BACKGROUND = Color('0e242d');
- Color.UI_BACKGROUND2 = Color('1c3c41');
- -------[Classe de Cores por Lynezx | rev 2 - 27/08/2016]-------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement