Color.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes License Agreement
  3. -- Last updated May 1, 2019. Replaces all prior versions.
  4. --
  5. -- Copyright (c) 2013-2019, Esoteric Software LLC
  6. --
  7. -- Integration of the Spine Runtimes into software or otherwise creating
  8. -- derivative works of the Spine Runtimes is permitted under the terms and
  9. -- conditions of Section 2 of the Spine Editor License Agreement:
  10. -- http://esotericsoftware.com/spine-editor-license
  11. --
  12. -- Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. -- or otherwise create derivative works of the Spine Runtimes (collectively,
  14. -- "Products"), provided that each user of the Products must obtain their own
  15. -- Spine Editor license and redistribution of the Products in any form must
  16. -- include this license and copyright notice.
  17. --
  18. -- THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. -- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. -- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. -- NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. -- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. -- INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. -- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. -- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. -------------------------------------------------------------------------------
  29. local setmetatable = setmetatable
  30. local utils = require "spine-lua.utils"
  31. local Color = {}
  32. Color.__index = Color
  33. function Color.new ()
  34. local self = {
  35. r = 0, g = 0, b = 0, a = 0
  36. }
  37. setmetatable(self, Color)
  38. return self
  39. end
  40. function Color.newWith (r, g, b, a)
  41. local self = {
  42. r = r, g = g, b = b, a = a
  43. }
  44. setmetatable(self, Color)
  45. return self
  46. end
  47. function Color:set(r, g, b, a)
  48. self.r = r
  49. self.g = g
  50. self.b = b
  51. self.a = a
  52. end
  53. function Color:setFrom(color)
  54. self.r = color.r
  55. self.g = color.g
  56. self.b = color.b
  57. self.a = color.a
  58. end
  59. function Color:add(r, g, b, a)
  60. self.r = self.r + r
  61. self.g = self.g + g
  62. self.b = self.b + b
  63. self.a = self.a + a
  64. self:clamp()
  65. end
  66. function Color:clamp()
  67. self.r = utils.clamp(self.r, 0, 1)
  68. self.g = utils.clamp(self.g, 0, 1)
  69. self.b = utils.clamp(self.b, 0, 1)
  70. self.a = utils.clamp(self.a, 0, 1)
  71. end
  72. return Color