Advertisement
mateus_araujo

Classe Lua Color

Aug 25th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.54 KB | None | 0 0
  1. -------[Classe de Cores por Lynezx | rev 2 - 27/08/2016]-------
  2. Color = {Random = function()
  3.             return Color(math.random(0, 255), math.random(0, 255), math.random(0, 255));
  4.       end,
  5.       Error = function(codigo, parametro)
  6.       local msgs = {"[Color Class] - Invalid hex parameter [%s].",
  7.                   "[Color Class] - Invalid byte parameter [%s]. Value must be a number from 0 to 255.",
  8.                   "[Color Class] - Invalid integer parameter [%s]. Value must be a number from 0 to 16777215.",
  9.                   "[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."
  10.              };
  11.             error(string.format((msgs[codigo] or "[Color Class] - Unknown error"), parametro or ""));
  12.       end
  13. };
  14. setmetatable(Color, {
  15.       __call = function(t, ...)
  16.             local args = {...};
  17.             local instancia = {
  18.                   _int = 0,
  19.                   _hex = "000000",
  20.                   _r = 0,
  21.                   _g = 0,
  22.                   _b = 0
  23.             };
  24.             setmetatable(instancia, {
  25.                   __index = function(self, k)
  26.                         return rawget(self, '_' .. k);
  27.                   end,
  28.                   __newindex = function(self, k, v)
  29.                         local alterado = false;
  30.                         local val;
  31.                         if (k == 'int') then
  32.                               --Integer
  33.                               if not(pcall((function()
  34.                                     val = tonumber(v);
  35.                               end))) or (val == nil or val < 0 or val > 0xffffff) then
  36.                                     Color.Error(3, type(v) .. ' ' .. tostring(v));
  37.                               end
  38.                               rawset(self, '_int', val);
  39.                               alterado = true;
  40.                         elseif (k == 'hex') then
  41.                               --Hex
  42.                               if not(pcall((function()
  43.                                     val = tonumber("0x" .. string.gsub(v, '#', '', 1));
  44.                               end))) or val == nil then
  45.                                     Color.Error(1, type(v) .. ' ' .. tostring(v));
  46.                               end
  47.                               rawset(self, '_int', val);
  48.                               alterado = true;
  49.                         elseif (k == 'r' or k == 'g' or k == 'b') then
  50.                               --Byte
  51.                               if not(pcall((function()
  52.                                     val = tonumber(v);
  53.                               end))) or (val == nil or val < 0 or val > 0xff) then
  54.                                     Color.Error(2, type(v) .. ' ' .. tostring(v));
  55.                               end
  56.                               rawset(self, '_' .. k, val);
  57.                               rawset(self, '_int', bit32.lshift(self.r, 16) + bit32.lshift(self.g, 8) + self.b);
  58.                               alterado = true;
  59.                         elseif not alterado then
  60.                               rawset(self, k, val);
  61.                         end
  62.                         --Finalização
  63.                         if alterado then
  64.                               rawset(self, '_r', bit32.band(bit32.rshift(self.int, 16), 0xFF));
  65.                               rawset(self, '_g', bit32.band(bit32.rshift(self.int, 8), 0xFF));
  66.                               rawset(self, '_b', bit32.band(self.int, 0xFF));
  67.                               rawset(self, '_hex', string.format('%02x%02x%02x', self.r, self.g, self.b));
  68.                         end
  69.                   end
  70.             });
  71.             if (#args == 1 and type(args[1]) == 'string') then
  72.                   instancia.hex = args[1];
  73.             elseif (#args == 1 and type(args[1]) == 'number') then
  74.                   instancia.int = args[1];
  75.             elseif (#args == 1 and type(args[1]) == 'table' and type(args[1].int) == 'number') then
  76.                   instancia.int = args[1].int;
  77.             elseif (#args == 1) then
  78.                   Color.Error(4, type(args[1]) .. ' ' .. tostring(args[1]));
  79.             elseif (#args == 3) then
  80.                   instancia.r = args[1];
  81.                   instancia.g = args[2];
  82.                   instancia.b = args[3];
  83.             end
  84.             return instancia;
  85.       end
  86. });
  87. Color.GAME_BACKGROUND = Color('6a7495');
  88. Color.UI_BACKGROUND = Color('0e242d');
  89. Color.UI_BACKGROUND2 = Color('1c3c41');
  90. -------[Classe de Cores por Lynezx | rev 2 - 27/08/2016]-------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement