Advertisement
doobbyyus

Color3 Module

Feb 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. local Color3 = {__type = 'Color3'};
  2.  
  3. _G.Color3 = Color3
  4.  
  5. function Color3.new(r, g, b)
  6. local self = setmetatable({}, Color3)
  7.  
  8. self.r = r or 0
  9. self.g = g or 0
  10. self.b = b or 0
  11.  
  12. function self:lerp(e, a)
  13. local function lerp(s, e, a)
  14. return s + (e - s) * a
  15. end
  16.  
  17. return Color3.new(
  18. lerp(self.r, e.r, a),
  19. lerp(self.g, e.g, a),
  20. lerp(self.b, e.b, a)
  21. )
  22. end
  23.  
  24. return self
  25. end
  26.  
  27. function Color3.fromRGB(r, g, b)
  28. local self = setmetatable({}, Color3)
  29.  
  30. self.r = r/255
  31. self.g = g/255
  32. self.b = b/255
  33.  
  34. function self:lerp(c, a)
  35. local function lerp(s, e, a)
  36. return s + (e - s) * a
  37. end
  38.  
  39. return Color3.new(
  40. lerp(self.r, c.r, a),
  41. lerp(self.g, c.g, a),
  42. lerp(self.b, c.b, a)
  43. )
  44. end
  45.  
  46. return self
  47. end
  48.  
  49. function Color3.fromHEX(hex)
  50. local function hex2rgb(hex)
  51. hex = hex:gsub("#","")
  52. return tonumber("0x"..hex:sub(1,2)),tonumber("0x"..hex:sub(3,4)),tonumber("0x"..hex:sub(5,6))
  53. end
  54.  
  55. local r, g, b = hex2rgb(hex)
  56. return Color3.new(r/255, g/255, b/255)
  57. end
  58.  
  59. function Color3:__tostring()
  60. return self.r .. ', ' .. self.g.. ', ' .. self.b
  61. end
  62.  
  63. function Color3:__add(c)
  64. return Color3.new(
  65. self.r + c.r,
  66. self.g + c.g,
  67. self.b + c.b
  68. )
  69. end
  70.  
  71. function Color3:__sub(c)
  72. return Color3.new(
  73. self.r - c.r,
  74. self.g - c.g,
  75. self.b - c.b
  76. )
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement