Advertisement
CrazedProgrammer

RGB API

Apr 25th, 2015
1,587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. -- RGB API version 1.0 by CrazedProgrammer
  2. -- You can find info and documentation on these pages:
  3. --
  4. -- You may use this in your ComputerCraft programs and modify it without asking.
  5. -- However, you may not publish this API under your name without asking me.
  6. -- If you have any suggestions, bug reports or questions then please send an email to:
  7. local hex = {"F0F0F0", "F2B233", "E57FD8", "99B2F2", "DEDE6C", "7FCC19", "F2B2CC", "4C4C4C", "999999", "4C99B2", "B266E5", "3366CC", "7F664C", "57A64E", "CC4C4C", "191919"}
  8. local rgb = {}
  9. for i=1,16,1 do
  10.   rgb[i] = {tonumber(hex[i]:sub(1, 2), 16), tonumber(hex[i]:sub(3, 4), 16), tonumber(hex[i]:sub(5, 6), 16)}
  11. end
  12. local rgb2 = {}
  13. for i=1,16,1 do
  14.   rgb2[i] = {}
  15.   for j=1,16,1 do
  16.     rgb2[i][j] = {(rgb[i][1] * 34 + rgb[j][1] * 20) / 54, (rgb[i][2] * 34 + rgb[j][2] * 20) / 54, (rgb[i][3] * 34 + rgb[j][3] * 20) / 54}
  17.   end
  18. end
  19.  
  20. colors.fromRGB = function (r, g, b)
  21.   local dist = 1e100
  22.   local d = 1e100
  23.   local color = -1
  24.   for i=1,16,1 do
  25.     d = math.sqrt((math.max(rgb[i][1], r) - math.min(rgb[i][1], r)) ^ 2 + (math.max(rgb[i][2], g) - math.min(rgb[i][2], g)) ^ 2 + (math.max(rgb[i][3], b) - math.min(rgb[i][3], b)) ^ 2)
  26.     if d < dist then
  27.       dist = d
  28.       color = i - 1
  29.     end
  30.   end
  31.   return 2 ^ color
  32. end
  33.  
  34. colors.toRGB = function(color)
  35.   return unpack(rgb[math.floor(math.log(color) / math.log(2) + 1)])
  36. end
  37.  
  38. colors.fromRGB2 = function (r, g, b)
  39.   local dist = 1e100
  40.   local d = 1e100
  41.   local color1 = -1
  42.   local color2 = -1
  43.   for i=1,16,1 do
  44.     for j=1,16,1 do
  45.       d = math.sqrt((math.max(rgb2[i][j][1], r) - math.min(rgb2[i][j][1], r)) ^ 2 + (math.max(rgb2[i][j][2], g) - math.min(rgb2[i][j][2], g)) ^ 2 + (math.max(rgb2[i][j][3], b) - math.min(rgb2[i][j][3], b)) ^ 2)
  46.       if d < dist then
  47.         dist = d
  48.         color1 = i - 1
  49.         color2 = j - 1
  50.       end
  51.     end
  52.   end
  53.   return 2 ^ color1, 2 ^ color2
  54. end
  55.  
  56. colors.toRGB2 = function(color1, color2, str)
  57.   local c1 = math.floor(math.log(color1) / math.log(2) + 1)
  58.   local c2 = math.floor(math.log(color2) / math.log(2) + 1)
  59.   return math.floor(rgb2[c1][c2][1]), math.floor(rgb2[c1][c2][2]), math.floor(rgb2[c1][c2][3])
  60. end
  61.  
  62. colours.fromRGB = colors.fromRGB
  63. colours.toRGB = colors.toRGB
  64. colours.fromRGB2 = colors.fromRGB2
  65. colours.toRGB2 = colors.toRGB2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement