Color.js

  1. /*
  2. * Copyright 2020 WICKLETS LLC
  3. *
  4. * This file is part of Wick Engine.
  5. *
  6. * Wick Engine is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Wick Engine is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Wick Engine. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /* Small utility class for colors. */
  20. Wick.Color = class {
  21. /**
  22. * Creates a Transformation.
  23. * @param {string} color - (Optional) Hex or Rgba color to create a Wick.Color from.
  24. */
  25. constructor (color) {
  26. if(color) {
  27. this._color = new paper.Color(color);
  28. } else {
  29. this._color = new paper.Color();
  30. }
  31. }
  32. /**
  33. * The red value of the color. Ranges from 0.0 to 1.0.
  34. * @type {Number}
  35. */
  36. get r () {
  37. return this._color.red;
  38. }
  39. set r (r) {
  40. this._color.red = r;
  41. }
  42. /**
  43. * The green value of the color. Ranges from 0.0 to 1.0.
  44. * @type {Number}
  45. */
  46. get g () {
  47. return this._color.green;
  48. }
  49. set g (g) {
  50. this._color.green = g;
  51. }
  52. /**
  53. * The blue value of the color. Ranges from 0.0 to 1.0.
  54. * @type {Number}
  55. */
  56. get b () {
  57. return this._color.blue;
  58. }
  59. set b (b) {
  60. this._color.blue = b;
  61. }
  62. /**
  63. * The alpha value of the color. Ranges from 0.0 to 1.0.
  64. * @type {Number}
  65. */
  66. get a () {
  67. return this._color.alpha;
  68. }
  69. set a (a) {
  70. this._color.alpha = a;
  71. }
  72. /**
  73. * The color as a hex string. Example: "#AABBCC"
  74. * @type {String}
  75. */
  76. get hex () {
  77. return this._color.toCSS(true);
  78. }
  79. /**
  80. * The color as an rgba string. Example: "rgba(r,g,b,a)"
  81. */
  82. get rgba () {
  83. return this._color.toCSS();
  84. }
  85. /**
  86. * Adds together the r, g, and b values of both colors and produces a new color.
  87. * @param {Wick.Color} color - the color to add to this color
  88. * @returns {Wick.Color} the resulting color
  89. */
  90. add (color) {
  91. var newColor = new Wick.Color();
  92. newColor.r = this.r + color.r;
  93. newColor.g = this.g + color.g;
  94. newColor.b = this.b + color.b;
  95. return newColor;
  96. }
  97. /**
  98. * Multiplies the r, g, and b values of both colors to produce a new color.
  99. * @param {Wick.Color} color - the color to multiply with this color
  100. * @returns {Wick.Color} the resulting color
  101. */
  102. multiply (n) {
  103. var newColor = new Wick.Color();
  104. newColor.r = this.r * n;
  105. newColor.g = this.g * n;
  106. newColor.b = this.b * n;
  107. return newColor;
  108. }
  109. /**
  110. * Averages the r, g, and b values of two colors.
  111. * @param {Wick.Color} colorA - a color to average with another color (order does not matter)
  112. * @param {Wick.Color} colorB - a color to average with another color (order does not matter)
  113. * @returns {Wick.Color} The resulting averaged color.
  114. */
  115. static average (colorA, colorB) {
  116. return colorA.multiply(0.5).add(colorB.multiply(0.5));
  117. }
  118. }