Advertisement
HappySunChild

Color3Library-OpenComputers

Aug 7th, 2022 (edited)
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. local color3 = {} -- more human readable and simpler way than guessing hex
  2.  
  3. function color3.getHexFromRGB(rgb)
  4.     local hexadecimal = "0x"
  5.  
  6.     for i, value in pairs(rgb) do
  7.         local hex = ''
  8.  
  9.         while value > 0 do
  10.             local index = math.fmod(value, 16) + 1
  11.             value = math.floor(value / 16)
  12.             hex = string.sub("0123456789ABCDEF", index, index) .. hex
  13.         end
  14.  
  15.         if (string.len(hex) == 0) then
  16.             hex = "00"
  17.         elseif (string.len(hex) == 1) then
  18.             hex = "0" .. hex
  19.         end
  20.  
  21.         hexadecimal = hexadecimal .. hex
  22.     end
  23.  
  24.     return tonumber(hexadecimal)
  25. end
  26.  
  27. function color3.getRGBFromHSV(hsv)
  28.     local r, g, b
  29.     local h, s, v = hsv[1], hsv[2], hsv[3]
  30.  
  31.     local i = math.floor(h * 6)
  32.     local f = h * 6 - i
  33.     local p = v * (1 - s)
  34.     local q = v * (1 - f * s)
  35.     local t = v * (1 - (1 - f) * s)
  36.  
  37.     i = i % 6
  38.  
  39.     if i == 0 then r, g, b = v, t, p
  40.     elseif i == 1 then r, g, b = q, v, p
  41.     elseif i == 2 then r, g, b = p, v, t
  42.     elseif i == 3 then r, g, b = p, q, v
  43.     elseif i == 4 then r, g, b = t, p, v
  44.     elseif i == 5 then r, g, b = v, p, q
  45.     end
  46.  
  47.     return color3.fromRGB(math.floor(r * 255), math.floor(g * 255), math.floor(b * 255))
  48. end
  49.  
  50. function color3.new(redPercentile, greenPercentile, bluePercentile)
  51.     return color3.getHexFromRGB({ redPercentile * 255, greenPercentile * 255, bluePercentile * 255 })
  52. end
  53.  
  54. function color3.fromHSV(hue, saturation, value)
  55.     return color3.getRGBFromHSV({ hue, saturation, value })
  56. end
  57.  
  58. function color3.fromRGB(red, green, blue)
  59.     return color3.getHexFromRGB({ red, green, blue })
  60. end
  61.  
  62. return color3
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement