Utility.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. --[[
  2. MIT License
  3. Copyright (c) 2019 Mitchell Davis <coding.jackalope@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. --]]
  20. local Utility = {}
  21. function Utility.MakeColor(Color)
  22. local Copy = {0.0, 0.0, 0.0, 1.0}
  23. if Color ~= nil then
  24. Copy[1] = Color[1]
  25. Copy[2] = Color[2]
  26. Copy[3] = Color[3]
  27. Copy[4] = Color[4]
  28. end
  29. return Copy
  30. end
  31. function Utility.HSVtoRGB(H, S, V)
  32. if S == 0.0 then
  33. return V, V, V
  34. end
  35. H = math.fmod(H, 1.0) / (60.0/360.0)
  36. local I = math.floor(H)
  37. local F = H - I
  38. local P = V * (1.0 - S)
  39. local Q = V * (1.0 - S * F)
  40. local T = V * (1.0 - S * (1.0 - F))
  41. local R, G, B = 0, 0, 0
  42. if I == 0 then
  43. R, G, B = V, T, P
  44. elseif I == 1 then
  45. R, G, B = Q, V, P
  46. elseif I == 2 then
  47. R, G, B = P, V, T
  48. elseif I == 3 then
  49. R, G, B = P, Q, V
  50. elseif I == 4 then
  51. R, G, B = T, P, V
  52. else
  53. R, G, B = V, P, Q
  54. end
  55. return R, G, B
  56. end
  57. function Utility.RGBtoHSV(R, G, B)
  58. local K = 0.0
  59. if G < B then
  60. local T = G
  61. G = B
  62. B = T
  63. K = -1.0
  64. end
  65. if R < G then
  66. local T = R
  67. R = G
  68. G = T
  69. K = -2.0 / 6.0 - K
  70. end
  71. local Chroma = R - (G < B and G or B)
  72. local H = math.abs(K + (G - B) / (6.0 * Chroma + 1e-20))
  73. local S = Chroma / (R + 1e-20)
  74. local V = R
  75. return H, S, V
  76. end
  77. function Utility.HasValue(Table, Value)
  78. for I, V in ipairs(Table) do
  79. if V == Value then
  80. return true
  81. end
  82. end
  83. return false
  84. end
  85. function Utility.Remove(Table, Value)
  86. for I, V in ipairs(Table) do
  87. if V == Value then
  88. table.remove(Table, I)
  89. break
  90. end
  91. end
  92. end
  93. function Utility.CopyValues(A, B)
  94. if type(A) ~= "table" or type(B) ~= "table" then
  95. return
  96. end
  97. for K, V in pairs(A, B) do
  98. local Other = B[K]
  99. if Other ~= nil then
  100. A[K] = Utility.Copy(Other)
  101. end
  102. end
  103. end
  104. function Utility.Copy(Original)
  105. local Copy = nil
  106. if type(Original) == "table" then
  107. Copy = {}
  108. for K, V in next, Original, nil do
  109. Copy[Utility.Copy(K)] = Utility.Copy(V)
  110. end
  111. else
  112. Copy = Original
  113. end
  114. return Copy
  115. end
  116. function Utility.Contains(Table, Value)
  117. if Table == nil then
  118. return false
  119. end
  120. for I, V in ipairs(Table) do
  121. if Value == V then
  122. return true
  123. end
  124. end
  125. return false
  126. end
  127. function Utility.TableCount(Table)
  128. local Result = 0
  129. if Table ~= nil then
  130. for K, V in pairs(Table) do
  131. Result = Result + 1
  132. end
  133. end
  134. return Result
  135. end
  136. function Utility.IsWindows()
  137. return love.system.getOS() == "Windows"
  138. end
  139. function Utility.IsOSX()
  140. return love.system.getOS() == "OS X"
  141. end
  142. return Utility