Advertisement
Guest User

Untitled

a guest
Dec 29th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | None | 0 0
  1. -- Saved color codes, saving codes in here
  2. -- in case they are needed another time.
  3. local savedColors = {};
  4.  
  5. local HEX_BASE = 16; -- 0 - F = 16.
  6. -- Hex digits (10 - 15) and a check function.
  7. local hexDigits = {'A', 'B', 'C', 'D', 'E', 'F'};
  8. local function isHexDigit(hexStr)
  9.     for _, v in ipairs(hexDigits) do
  10.         if v == hexStr then return true end;
  11.     end
  12.     return false;
  13. end
  14. -- Function to convert a decimal number to
  15. -- a hexadecimal number.
  16. local function decToHex(dec)
  17.     local hex = "";
  18.     while dec ~= 0 do
  19.         local temp = dec % HEX_BASE
  20.         if temp >= 10 then
  21.             temp = hexDigits[temp-9];
  22.         end
  23.         hex = temp .. hex;
  24.         dec = math.floor(dec / HEX_BASE);
  25.     end
  26.     if isHexDigit(hex) or tonumber(hex) and tonumber(hex) < 10 then
  27.         hex = "0" .. hex;
  28.     elseif hex == "" then
  29.         hex = "00";
  30.     end
  31.     return hex;
  32. end
  33. -- Function to create the color code usable
  34. -- in WoW.
  35. local function createColorCode(...)
  36.     local colorCode = "";
  37.     local colorTable = {select(1, ...)};
  38.     for _, v in ipairs(colorTable) do
  39.         if tonumber(v) == nil then
  40.             return false;
  41.         end
  42.         colorCode = colorCode .. decToHex(v);
  43.     end
  44.     return "|cff" .. colorCode;
  45. end
  46. -- This is the function to call to get the color
  47. -- code associated with the RGB code.
  48. -- RGB is a representation of a color mix of three
  49. -- colors: R (red), G (green) and B (blue).
  50. -- Numbers vary between 0 and 255.
  51. -- Examples:
  52. --  255, 0, 0 = red. Only red is used, so obv. we get red.
  53. --  150, 0, 0 = dark red. Only red is used, however slightly,
  54. --  so we get a darker color.
  55. --  0, 255, 0 = green.
  56. --  255, 0, 255 = purple. A mix of red and blue = purple.
  57. function RGB(red, green, blue)
  58.     local key = red .. green .. blue;
  59.     local color = savedColors[key];
  60.     if color == nil then
  61.         color = createColorCode(red, green, blue);
  62.         savedColors[key] = color;
  63.     end
  64.     return color;
  65. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement