gradients.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Color4 } from "../Maths/math.color.js";
  2. /** Class used to store color4 gradient */
  3. export class ColorGradient {
  4. /**
  5. * Creates a new color4 gradient
  6. * @param gradient gets or sets the gradient value (between 0 and 1)
  7. * @param color1 gets or sets first associated color
  8. * @param color2 gets or sets first second color
  9. */
  10. constructor(
  11. /**
  12. * Gets or sets the gradient value (between 0 and 1)
  13. */
  14. gradient,
  15. /**
  16. * Gets or sets first associated color
  17. */
  18. color1,
  19. /**
  20. * Gets or sets second associated color
  21. */
  22. color2) {
  23. this.gradient = gradient;
  24. this.color1 = color1;
  25. this.color2 = color2;
  26. }
  27. /**
  28. * Will get a color picked randomly between color1 and color2.
  29. * If color2 is undefined then color1 will be used
  30. * @param result defines the target Color4 to store the result in
  31. */
  32. getColorToRef(result) {
  33. if (!this.color2) {
  34. result.copyFrom(this.color1);
  35. return;
  36. }
  37. Color4.LerpToRef(this.color1, this.color2, Math.random(), result);
  38. }
  39. }
  40. /** Class used to store color 3 gradient */
  41. export class Color3Gradient {
  42. /**
  43. * Creates a new color3 gradient
  44. * @param gradient gets or sets the gradient value (between 0 and 1)
  45. * @param color gets or sets associated color
  46. */
  47. constructor(
  48. /**
  49. * Gets or sets the gradient value (between 0 and 1)
  50. */
  51. gradient,
  52. /**
  53. * Gets or sets the associated color
  54. */
  55. color) {
  56. this.gradient = gradient;
  57. this.color = color;
  58. }
  59. }
  60. /** Class used to store factor gradient */
  61. export class FactorGradient {
  62. /**
  63. * Creates a new factor gradient
  64. * @param gradient gets or sets the gradient value (between 0 and 1)
  65. * @param factor1 gets or sets first associated factor
  66. * @param factor2 gets or sets second associated factor
  67. */
  68. constructor(
  69. /**
  70. * Gets or sets the gradient value (between 0 and 1)
  71. */
  72. gradient,
  73. /**
  74. * Gets or sets first associated factor
  75. */
  76. factor1,
  77. /**
  78. * Gets or sets second associated factor
  79. */
  80. factor2) {
  81. this.gradient = gradient;
  82. this.factor1 = factor1;
  83. this.factor2 = factor2;
  84. }
  85. /**
  86. * Will get a number picked randomly between factor1 and factor2.
  87. * If factor2 is undefined then factor1 will be used
  88. * @returns the picked number
  89. */
  90. getFactor() {
  91. if (this.factor2 === undefined || this.factor2 === this.factor1) {
  92. return this.factor1;
  93. }
  94. return this.factor1 + (this.factor2 - this.factor1) * Math.random();
  95. }
  96. }
  97. /**
  98. * Helper used to simplify some generic gradient tasks
  99. */
  100. export class GradientHelper {
  101. /**
  102. * Gets the current gradient from an array of IValueGradient
  103. * @param ratio defines the current ratio to get
  104. * @param gradients defines the array of IValueGradient
  105. * @param updateFunc defines the callback function used to get the final value from the selected gradients
  106. */
  107. static GetCurrentGradient(ratio, gradients, updateFunc) {
  108. // Use last index if over
  109. if (gradients[0].gradient > ratio) {
  110. updateFunc(gradients[0], gradients[0], 1.0);
  111. return;
  112. }
  113. for (let gradientIndex = 0; gradientIndex < gradients.length - 1; gradientIndex++) {
  114. const currentGradient = gradients[gradientIndex];
  115. const nextGradient = gradients[gradientIndex + 1];
  116. if (ratio >= currentGradient.gradient && ratio <= nextGradient.gradient) {
  117. const scale = (ratio - currentGradient.gradient) / (nextGradient.gradient - currentGradient.gradient);
  118. updateFunc(currentGradient, nextGradient, scale);
  119. return;
  120. }
  121. }
  122. // Use last index if over
  123. const lastIndex = gradients.length - 1;
  124. updateFunc(gradients[lastIndex], gradients[lastIndex], 1.0);
  125. }
  126. }
  127. //# sourceMappingURL=gradients.js.map