pepejik

Untitled

Jan 2nd, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.51 KB | None | 0 0
  1. local palette = {0x000000, 0x000040, 0x000080, 0x0000BF, 0x0000FF, 0x002400, 0x002440, 0x002480, 0x0024BF, 0x0024FF, 0x004900, 0x004940, 0x004980, 0x0049BF, 0x0049FF, 0x006D00, 0x006D40, 0x006D80, 0x006DBF, 0x006DFF, 0x009200, 0x009240, 0x009280, 0x0092BF, 0x0092FF, 0x00B600, 0x00B640, 0x00B680, 0x00B6BF, 0x00B6FF, 0x00DB00, 0x00DB40, 0x00DB80, 0x00DBBF, 0x00DBFF, 0x00FF00, 0x00FF40, 0x00FF80, 0x00FFBF, 0x00FFFF, 0x0F0F0F, 0x1E1E1E, 0x2D2D2D, 0x330000, 0x330040, 0x330080, 0x3300BF, 0x3300FF, 0x332400, 0x332440, 0x332480, 0x3324BF, 0x3324FF, 0x334900, 0x334940, 0x334980, 0x3349BF, 0x3349FF, 0x336D00, 0x336D40, 0x336D80, 0x336DBF, 0x336DFF, 0x339200, 0x339240, 0x339280, 0x3392BF, 0x3392FF, 0x33B600, 0x33B640, 0x33B680, 0x33B6BF, 0x33B6FF, 0x33DB00, 0x33DB40, 0x33DB80, 0x33DBBF, 0x33DBFF, 0x33FF00, 0x33FF40, 0x33FF80, 0x33FFBF, 0x33FFFF, 0x3C3C3C, 0x4B4B4B, 0x5A5A5A, 0x660000, 0x660040, 0x660080, 0x6600BF, 0x6600FF, 0x662400, 0x662440, 0x662480, 0x6624BF, 0x6624FF, 0x664900, 0x664940, 0x664980, 0x6649BF, 0x6649FF, 0x666D00, 0x666D40, 0x666D80, 0x666DBF, 0x666DFF, 0x669200, 0x669240, 0x669280, 0x6692BF, 0x6692FF, 0x66B600, 0x66B640, 0x66B680, 0x66B6BF, 0x66B6FF, 0x66DB00, 0x66DB40, 0x66DB80, 0x66DBBF, 0x66DBFF, 0x66FF00, 0x66FF40, 0x66FF80, 0x66FFBF, 0x66FFFF, 0x696969, 0x787878, 0x878787, 0x969696, 0x990000, 0x990040, 0x990080, 0x9900BF, 0x9900FF, 0x992400, 0x992440, 0x992480, 0x9924BF, 0x9924FF, 0x994900, 0x994940, 0x994980, 0x9949BF, 0x9949FF, 0x996D00, 0x996D40, 0x996D80, 0x996DBF, 0x996DFF, 0x999200, 0x999240, 0x999280, 0x9992BF, 0x9992FF, 0x99B600, 0x99B640, 0x99B680, 0x99B6BF, 0x99B6FF, 0x99DB00, 0x99DB40, 0x99DB80, 0x99DBBF, 0x99DBFF, 0x99FF00, 0x99FF40, 0x99FF80, 0x99FFBF, 0x99FFFF, 0xA5A5A5, 0xB4B4B4, 0xC3C3C3, 0xCC0000, 0xCC0040, 0xCC0080, 0xCC00BF, 0xCC00FF, 0xCC2400, 0xCC2440, 0xCC2480, 0xCC24BF, 0xCC24FF, 0xCC4900, 0xCC4940, 0xCC4980, 0xCC49BF, 0xCC49FF, 0xCC6D00, 0xCC6D40, 0xCC6D80, 0xCC6DBF, 0xCC6DFF, 0xCC9200, 0xCC9240, 0xCC9280, 0xCC92BF, 0xCC92FF, 0xCCB600, 0xCCB640, 0xCCB680, 0xCCB6BF, 0xCCB6FF, 0xCCDB00, 0xCCDB40, 0xCCDB80, 0xCCDBBF, 0xCCDBFF, 0xCCFF00, 0xCCFF40, 0xCCFF80, 0xCCFFBF, 0xCCFFFF, 0xD2D2D2, 0xE1E1E1, 0xF0F0F0, 0xFF0000, 0xFF0040, 0xFF0080, 0xFF00BF, 0xFF00FF, 0xFF2400, 0xFF2440, 0xFF2480, 0xFF24BF, 0xFF24FF, 0xFF4900, 0xFF4940, 0xFF4980, 0xFF49BF, 0xFF49FF, 0xFF6D00, 0xFF6D40, 0xFF6D80, 0xFF6DBF, 0xFF6DFF, 0xFF9200, 0xFF9240, 0xFF9280, 0xFF92BF, 0xFF92FF, 0xFFB600, 0xFFB640, 0xFFB680, 0xFFB6BF, 0xFFB6FF, 0xFFDB00, 0xFFDB40, 0xFFDB80, 0xFFDBBF, 0xFFDBFF, 0xFFFF00, 0xFFFF40, 0xFFFF80, 0xFFFFBF, 0xFFFFFF}
  2. local mathFloor, mathMax, mathMin, mathModf = math.floor, math.max, math.min, math.modf
  3. local integerToRGB, RGBToInteger, blend, transition, to8Bit
  4.  
  5. local color = {}
  6.  
  7. --------------------------------------------------------------------------------
  8.  
  9. -- Optimized Lua 5.3 bitwise support
  10. if computer.getArchitecture and computer.getArchitecture() ~= "Lua 5.2" then
  11. integerToRGB, RGBToInteger, blend, transition, to8Bit = load([[
  12. local mathHuge, palette = math.huge, select(1, ...)
  13.  
  14. return
  15. function(integerColor)
  16. return integerColor >> 16, integerColor >> 8 & 0xFF, integerColor & 0xFF
  17. end,
  18.  
  19. function(r, g, b)
  20. return r << 16 | g << 8 | b
  21. end,
  22.  
  23. function(color1, color2, transparency)
  24. local invertedTransparency = 1 - transparency
  25.  
  26. return
  27. ((color2 >> 16) * invertedTransparency + (color1 >> 16) * transparency) // 1.0 << 16 |
  28. ((color2 >> 8 & 0xFF) * invertedTransparency + (color1 >> 8 & 0xFF) * transparency) // 1.0 << 8 |
  29. ((color2 & 0xFF) * invertedTransparency + (color1 & 0xFF) * transparency) // 1.0
  30. end,
  31.  
  32. function(color1, color2, position)
  33. local r1, g1, b1 = color1 >> 16, color1 >> 8 & 0xFF, color1 & 0xFF
  34.  
  35. return
  36. (r1 + ((color2 >> 16) - r1) * position) // 1.0 << 16 |
  37. (g1 + ((color2 >> 8 & 0xFF) - g1) * position) // 1.0 << 8 |
  38. (b1 + ((color2 & 0xFF) - b1) * position) // 1.0
  39. end,
  40.  
  41. function(color24Bit)
  42. local r, g, b, closestDelta, closestIndex, delta, paletteColor, paletteR, paletteG, paletteB = color24Bit >> 16, color24Bit >> 8 & 0xFF, color24Bit & 0xFF, mathHuge, 1
  43.  
  44. for i = 1, #palette do
  45. paletteColor = palette[i]
  46.  
  47. if color24Bit == paletteColor then
  48. return i - 1
  49. else
  50. paletteR, paletteG, paletteB = paletteColor >> 16, paletteColor >> 8 & 0xFF, paletteColor & 0xFF
  51.  
  52. delta = (paletteR - r) ^ 2 + (paletteG - g) ^ 2 + (paletteB - b) ^ 2
  53.  
  54. if delta < closestDelta then
  55. closestDelta, closestIndex = delta, i
  56. end
  57. end
  58. end
  59.  
  60. return closestIndex - 1
  61. end
  62. ]], "=lua53colorfunctions")(palette)
  63. else
  64. integerToRGB, RGBToInteger, blend, transition, to8Bit = load([[
  65. local mathHuge, palette = math.huge, select(1, ...)
  66.  
  67. return
  68. function(integerColor)
  69. local r = integerColor / 65536
  70. r = r - r % 1
  71.  
  72. local g = (integerColor - r * 65536) / 256
  73. g = g - g % 1
  74.  
  75. return r, g, integerColor - r * 65536 - g * 256
  76. end,
  77.  
  78. function(r, g, b)
  79. return r * 65536 + g * 256 + b
  80. end,
  81.  
  82. function(color1, color2, transparency)
  83. local invertedTransparency = 1 - transparency
  84.  
  85. local r1 = color1 / 65536
  86. r1 = r1 - r1 % 1
  87.  
  88. local g1 = (color1 - r1 * 65536) / 256
  89. g1 = g1 - g1 % 1
  90.  
  91. local r2 = color2 / 65536
  92. r2 = r2 - r2 % 1
  93.  
  94. local g2 = (color2 - r2 * 65536) / 256
  95. g2 = g2 - g2 % 1
  96.  
  97. local r, g, b =
  98. r2 * invertedTransparency + r1 * transparency,
  99. g2 * invertedTransparency + g1 * transparency,
  100. (color2 - r2 * 65536 - g2 * 256) * invertedTransparency + (color1 - r1 * 65536 - g1 * 256) * transparency
  101.  
  102. return
  103. (r - r % 1) * 65536 +
  104. (g - g % 1) * 256 +
  105. (b - b % 1)
  106. end,
  107.  
  108. function(color1, color2, position)
  109. local r1 = color1 / 65536
  110. r1 = r1 - r1 % 1
  111.  
  112. local g1 = (color1 - r1 * 65536) / 256
  113. g1 = g1 - g1 % 1
  114.  
  115. local b1 = color1 - r1 * 65536 - g1 * 256
  116.  
  117. local r2 = color2 / 65536
  118. r2 = r2 - r2 % 1
  119.  
  120. local g2 = (color2 - r2 * 65536) / 256
  121. g2 = g2 - g2 % 1
  122.  
  123. local r, g, b =
  124. r1 + (r2 - r1) * position,
  125. g1 + (g2 - g1) * position,
  126. b1 + (color2 - r2 * 65536 - g2 * 256 - b1) * position
  127.  
  128. return
  129. (r - r % 1) * 65536 +
  130. (g - g % 1) * 256 +
  131. (b - b % 1)
  132. end,
  133.  
  134. function(color24Bit)
  135. local closestDelta, closestIndex, delta, paletteColor, paletteR, paletteG, paletteB = mathHuge, 1
  136.  
  137. local r = color24Bit / 65536
  138. r = r - r % 1
  139.  
  140. local g = (color24Bit - r * 65536) / 256
  141. g = g - g % 1
  142.  
  143. local b = color24Bit - r * 65536 - g * 256
  144.  
  145. for index = 1, #palette do
  146. paletteColor = palette[index]
  147.  
  148. if color24Bit == paletteColor then
  149. return index - 1
  150. else
  151. paletteR = paletteColor / 65536
  152. paletteR = paletteR - paletteR % 1
  153. paletteG = (paletteColor - paletteR * 65536) / 256
  154. paletteG = paletteG - paletteG % 1
  155. paletteB = paletteColor - paletteR * 65536 - paletteG * 256
  156.  
  157. delta = (paletteR - r) ^ 2 + (paletteG - g) ^ 2 + (paletteB - b) ^ 2
  158.  
  159. if delta < closestDelta then
  160. closestDelta, closestIndex = delta, index
  161. end
  162. end
  163. end
  164.  
  165. return closestIndex - 1
  166. end
  167. ]], "=lua52colorfunctions")(palette)
  168. end
  169.  
  170. --------------------------------------------------------------------------------
  171.  
  172. local function RGBToHSB(r, g, b)
  173. local max, min = mathMax(r, g, b), mathMin(r, g, b)
  174.  
  175. if max == min then
  176. return 0, max == 0 and 0 or (1 - min / max), max / 255
  177. elseif max == r and g >= b then
  178. return 60 * (g - b) / (max - min), max == 0 and 0 or (1 - min / max), max / 255
  179. elseif max == r and g < b then
  180. return 60 * (g - b) / (max - min) + 360, max == 0 and 0 or (1 - min / max), max / 255
  181. elseif max == g then
  182. return 60 * (b - r) / (max - min) + 120, max == 0 and 0 or (1 - min / max), max / 255
  183. elseif max == b then
  184. return 60 * (r - g) / (max - min) + 240, max == 0 and 0 or (1 - min / max), max / 255
  185. else
  186. return 0, max == 0 and 0 or (1 - min / max), max / 255
  187. end
  188. end
  189.  
  190. local function HSBToRGB(h, s, b)
  191. local integer, fractional = mathModf(h / 60)
  192. local p, q, t = b * (1 - s), b * (1 - s * fractional), b * (1 - (1 - fractional) * s)
  193.  
  194. if integer == 0 then
  195. return mathFloor(b * 255), mathFloor(t * 255), mathFloor(p * 255)
  196. elseif integer == 1 then
  197. return mathFloor(q * 255), mathFloor(b * 255), mathFloor(p * 255)
  198. elseif integer == 2 then
  199. return mathFloor(p * 255), mathFloor(b * 255), mathFloor(t * 255)
  200. elseif integer == 3 then
  201. return mathFloor(p * 255), mathFloor(q * 255), mathFloor(b * 255)
  202. elseif integer == 4 then
  203. return mathFloor(t * 255), mathFloor(p * 255), mathFloor(b * 255)
  204. else
  205. return mathFloor(b * 255), mathFloor(p * 255), mathFloor(q * 255)
  206. end
  207. end
  208.  
  209. local function integerToHSB(integerColor)
  210. return RGBToHSB(integerToRGB(integerColor))
  211. end
  212.  
  213. local function HSBToInteger(h, s, b)
  214. return RGBToInteger(HSBToRGB(h, s, b))
  215. end
  216.  
  217. --------------------------------------------------------------------------------
  218.  
  219. local function to24Bit(color8Bit)
  220. return palette[color8Bit + 1]
  221. end
  222.  
  223. local function optimize(color24Bit)
  224. return to24Bit(to8Bit(color24Bit))
  225. end
  226.  
  227. --------------------------------------------------------------------------------
  228.  
  229. return {
  230. RGBToInteger = RGBToInteger,
  231. integerToRGB = integerToRGB,
  232. RGBToHSB = RGBToHSB,
  233. HSBToRGB = HSBToRGB,
  234. integerToHSB = integerToHSB,
  235. HSBToInteger = HSBToInteger,
  236. blend = blend,
  237. transition = transition,
  238. to8Bit = to8Bit,
  239. to24Bit = to24Bit,
  240. optimize = optimize,
  241. }
Add Comment
Please, Sign In to add comment