Easing.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. namespace Terminal.Gui.TextEffects;
  2. using System;
  3. public delegate float EasingFunction (float progressRatio);
  4. public static class Easing
  5. {
  6. public static float Linear (float progressRatio)
  7. {
  8. return progressRatio;
  9. }
  10. public static float InSine (float progressRatio)
  11. {
  12. return 1 - (float)Math.Cos ((progressRatio * Math.PI) / 2);
  13. }
  14. public static float OutSine (float progressRatio)
  15. {
  16. return (float)Math.Sin ((progressRatio * Math.PI) / 2);
  17. }
  18. public static float InOutSine (float progressRatio)
  19. {
  20. return -(float)(Math.Cos (Math.PI * progressRatio) - 1) / 2;
  21. }
  22. public static float InQuad (float progressRatio)
  23. {
  24. return progressRatio * progressRatio;
  25. }
  26. public static float OutQuad (float progressRatio)
  27. {
  28. return 1 - (1 - progressRatio) * (1 - progressRatio);
  29. }
  30. public static float InOutQuad (float progressRatio)
  31. {
  32. if (progressRatio < 0.5)
  33. {
  34. return 2 * progressRatio * progressRatio;
  35. }
  36. else
  37. {
  38. return 1 - (float)Math.Pow (-2 * progressRatio + 2, 2) / 2;
  39. }
  40. }
  41. public static float InCubic (float progressRatio)
  42. {
  43. return progressRatio * progressRatio * progressRatio;
  44. }
  45. public static float OutCubic (float progressRatio)
  46. {
  47. return 1 - (float)Math.Pow (1 - progressRatio, 3);
  48. }
  49. public static float InOutCubic (float progressRatio)
  50. {
  51. if (progressRatio < 0.5)
  52. {
  53. return 4 * progressRatio * progressRatio * progressRatio;
  54. }
  55. else
  56. {
  57. return 1 - (float)Math.Pow (-2 * progressRatio + 2, 3) / 2;
  58. }
  59. }
  60. public static float InQuart (float progressRatio)
  61. {
  62. return progressRatio * progressRatio * progressRatio * progressRatio;
  63. }
  64. public static float OutQuart (float progressRatio)
  65. {
  66. return 1 - (float)Math.Pow (1 - progressRatio, 4);
  67. }
  68. public static float InOutQuart (float progressRatio)
  69. {
  70. if (progressRatio < 0.5)
  71. {
  72. return 8 * progressRatio * progressRatio * progressRatio * progressRatio;
  73. }
  74. else
  75. {
  76. return 1 - (float)Math.Pow (-2 * progressRatio + 2, 4) / 2;
  77. }
  78. }
  79. public static float InQuint (float progressRatio)
  80. {
  81. return progressRatio * progressRatio * progressRatio * progressRatio * progressRatio;
  82. }
  83. public static float OutQuint (float progressRatio)
  84. {
  85. return 1 - (float)Math.Pow (1 - progressRatio, 5);
  86. }
  87. public static float InOutQuint (float progressRatio)
  88. {
  89. if (progressRatio < 0.5)
  90. {
  91. return 16 * progressRatio * progressRatio * progressRatio * progressRatio * progressRatio;
  92. }
  93. else
  94. {
  95. return 1 - (float)Math.Pow (-2 * progressRatio + 2, 5) / 2;
  96. }
  97. }
  98. public static float InExpo (float progressRatio)
  99. {
  100. if (progressRatio == 0)
  101. {
  102. return 0;
  103. }
  104. else
  105. {
  106. return (float)Math.Pow (2, 10 * progressRatio - 10);
  107. }
  108. }
  109. public static float OutExpo (float progressRatio)
  110. {
  111. if (progressRatio == 1)
  112. {
  113. return 1;
  114. }
  115. else
  116. {
  117. return 1 - (float)Math.Pow (2, -10 * progressRatio);
  118. }
  119. }
  120. public static float InOutExpo (float progressRatio)
  121. {
  122. if (progressRatio == 0)
  123. {
  124. return 0;
  125. }
  126. else if (progressRatio == 1)
  127. {
  128. return 1;
  129. }
  130. else if (progressRatio < 0.5)
  131. {
  132. return (float)Math.Pow (2, 20 * progressRatio - 10) / 2;
  133. }
  134. else
  135. {
  136. return (2 - (float)Math.Pow (2, -20 * progressRatio + 10)) / 2;
  137. }
  138. }
  139. public static float InCirc (float progressRatio)
  140. {
  141. return 1 - (float)Math.Sqrt (1 - progressRatio * progressRatio);
  142. }
  143. public static float OutCirc (float progressRatio)
  144. {
  145. return (float)Math.Sqrt (1 - (progressRatio - 1) * (progressRatio - 1));
  146. }
  147. public static float InOutCirc (float progressRatio)
  148. {
  149. if (progressRatio < 0.5)
  150. {
  151. return (1 - (float)Math.Sqrt (1 - (2 * progressRatio) * (2 * progressRatio))) / 2;
  152. }
  153. else
  154. {
  155. return ((float)Math.Sqrt (1 - (-2 * progressRatio + 2) * (-2 * progressRatio + 2)) + 1) / 2;
  156. }
  157. }
  158. public static float InBack (float progressRatio)
  159. {
  160. const float c1 = 1.70158f;
  161. const float c3 = c1 + 1;
  162. return c3 * progressRatio * progressRatio * progressRatio - c1 * progressRatio * progressRatio;
  163. }
  164. public static float OutBack (float progressRatio)
  165. {
  166. const float c1 = 1.70158f;
  167. const float c3 = c1 + 1;
  168. return 1 + c3 * (progressRatio - 1) * (progressRatio - 1) * (progressRatio - 1) + c1 * (progressRatio - 1) * (progressRatio - 1);
  169. }
  170. public static float InOutBack (float progressRatio)
  171. {
  172. const float c1 = 1.70158f;
  173. const float c2 = c1 * 1.525f;
  174. if (progressRatio < 0.5)
  175. {
  176. return ((2 * progressRatio) * (2 * progressRatio) * ((c2 + 1) * 2 * progressRatio - c2)) / 2;
  177. }
  178. else
  179. {
  180. return ((2 * progressRatio - 2) * (2 * progressRatio - 2) * ((c2 + 1) * (progressRatio * 2 - 2) + c2) + 2) / 2;
  181. }
  182. }
  183. public static float InElastic (float progressRatio)
  184. {
  185. const float c4 = (2 * (float)Math.PI) / 3;
  186. if (progressRatio == 0)
  187. {
  188. return 0;
  189. }
  190. else if (progressRatio == 1)
  191. {
  192. return 1;
  193. }
  194. else
  195. {
  196. return -(float)Math.Pow (2, 10 * progressRatio - 10) * (float)Math.Sin ((progressRatio * 10 - 10.75) * c4);
  197. }
  198. }
  199. public static float OutElastic (float progressRatio)
  200. {
  201. const float c4 = (2 * (float)Math.PI) / 3;
  202. if (progressRatio == 0)
  203. {
  204. return 0;
  205. }
  206. else if (progressRatio == 1)
  207. {
  208. return 1;
  209. }
  210. else
  211. {
  212. return (float)Math.Pow (2, -10 * progressRatio) * (float)Math.Sin ((progressRatio * 10 - 0.75) * c4) + 1;
  213. }
  214. }
  215. public static float InOutElastic (float progressRatio)
  216. {
  217. const float c5 = (2 * (float)Math.PI) / 4.5f;
  218. if (progressRatio == 0)
  219. {
  220. return 0;
  221. }
  222. else if (progressRatio == 1)
  223. {
  224. return 1;
  225. }
  226. else if (progressRatio < 0.5)
  227. {
  228. return -(float)Math.Pow (2, 20 * progressRatio - 10) * (float)Math.Sin ((20 * progressRatio - 11.125) * c5) / 2;
  229. }
  230. else
  231. {
  232. return ((float)Math.Pow (2, -20 * progressRatio + 10) * (float)Math.Sin ((20 * progressRatio - 11.125) * c5)) / 2 + 1;
  233. }
  234. }
  235. public static float InBounce (float progressRatio)
  236. {
  237. return 1 - OutBounce (1 - progressRatio);
  238. }
  239. public static float OutBounce (float progressRatio)
  240. {
  241. const float n1 = 7.5625f;
  242. const float d1 = 2.75f;
  243. if (progressRatio < 1 / d1)
  244. {
  245. return n1 * progressRatio * progressRatio;
  246. }
  247. else if (progressRatio < 2 / d1)
  248. {
  249. return n1 * (progressRatio - 1.5f / d1) * (progressRatio - 1.5f / d1) + 0.75f;
  250. }
  251. else if (progressRatio < 2.5 / d1)
  252. {
  253. return n1 * (progressRatio - 2.25f / d1) * (progressRatio - 2.25f / d1) + 0.9375f;
  254. }
  255. else
  256. {
  257. return n1 * (progressRatio - 2.625f / d1) * (progressRatio - 2.625f / d1) + 0.984375f;
  258. }
  259. }
  260. public static float InOutBounce (float progressRatio)
  261. {
  262. if (progressRatio < 0.5)
  263. {
  264. return (1 - OutBounce (1 - 2 * progressRatio)) / 2;
  265. }
  266. else
  267. {
  268. return (1 + OutBounce (2 * progressRatio - 1)) / 2;
  269. }
  270. }
  271. }