Belzebub

lib/color.lua

Mar 17th, 2021 (edited)
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.82 KB | None | 0 0
  1. local hexadecimal, rgb, hex, index
  2. local pairs, tonumber = pairs, tonumber
  3. local string_len, math_fmod, math_floor = string.len, math.fmod, math.floor
  4.  
  5. local list = {}
  6. for i = 1, string_len("0123456789ABCDEF") do
  7.     list[i - 1] = string.sub("0123456789ABCDEF", i, i)
  8. end
  9.  
  10. local Color_Cache = {}
  11.  
  12. return function(r, g, b)
  13.     rgb = {r, g, b}
  14.  
  15.     if Color_Cache[rgb] then
  16.         return Color_Cache[rgb]
  17.     end
  18.  
  19.     hexadecimal = "0x"
  20.  
  21.     for _, value in ipairs(rgb) do
  22.         hex = ""
  23.  
  24.         while value > 0 do
  25.             index = math_fmod(value, 16)
  26.             value = math_floor(value / 16)
  27.             hex = list[index] .. hex           
  28.         end
  29.  
  30.         if string_len(hex) == 0 then
  31.             hex = "00"
  32.  
  33.         elseif string_len(hex) == 1 then
  34.             hex = "0".. hex
  35.         end
  36.  
  37.         hexadecimal = hexadecimal .. hex
  38.     end
  39.  
  40.     Color_Cache[rgb] = tonumber(hexadecimal)
  41.     return Color_Cache[rgb]
  42. end
Add Comment
Please, Sign In to add comment