a4019b704e02e92093b6a7ac8ab21eea493526eb81d1bacfd44996e6958cffbeb0fccc8cc9ffbcb5330bda2d6c4df46a17b222ae2140da3564044be52b0402 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var tinycolor = require('@ctrl/tinycolor');
  4. var shared = require('@vue/shared');
  5. class Color {
  6. constructor(options = {}) {
  7. this._hue = 0;
  8. this._saturation = 100;
  9. this._value = 100;
  10. this._alpha = 100;
  11. this._tiny = new tinycolor.TinyColor();
  12. this._isValid = false;
  13. this.enableAlpha = false;
  14. this.format = "";
  15. this.value = "";
  16. for (const option in options) {
  17. if (shared.hasOwn(options, option)) {
  18. this[option] = options[option];
  19. }
  20. }
  21. if (options.value) {
  22. this.fromString(options.value);
  23. } else {
  24. this.doOnChange();
  25. }
  26. }
  27. set(prop, value) {
  28. if (arguments.length === 1 && typeof prop === "object") {
  29. for (const p in prop) {
  30. if (shared.hasOwn(prop, p)) {
  31. this.set(p, prop[p]);
  32. }
  33. }
  34. return;
  35. }
  36. this[`_${prop}`] = value;
  37. this._isValid = true;
  38. this.doOnChange();
  39. }
  40. get(prop) {
  41. if (["hue", "saturation", "value", "alpha"].includes(prop)) {
  42. return Math.round(this[`_${prop}`]);
  43. }
  44. return this[`_${prop}`];
  45. }
  46. toRgb() {
  47. return this._isValid ? this._tiny.toRgb() : { r: 255, g: 255, b: 255, a: 0 };
  48. }
  49. fromString(value) {
  50. const color = new tinycolor.TinyColor(value);
  51. this._isValid = color.isValid;
  52. if (color.isValid) {
  53. const { h, s, v, a } = color.toHsv();
  54. this._hue = h;
  55. this._saturation = s * 100;
  56. this._value = v * 100;
  57. this._alpha = a * 100;
  58. } else {
  59. this._hue = 0;
  60. this._saturation = 100;
  61. this._value = 100;
  62. this._alpha = 100;
  63. }
  64. this.doOnChange();
  65. }
  66. compare(color) {
  67. const compareColor = new tinycolor.TinyColor({
  68. h: color._hue,
  69. s: color._saturation / 100,
  70. v: color._value / 100,
  71. a: color._alpha / 100
  72. });
  73. return this._tiny.equals(compareColor);
  74. }
  75. doOnChange() {
  76. const { _hue, _saturation, _value, _alpha, format, enableAlpha } = this;
  77. let _format = format || (enableAlpha ? "rgb" : "hex");
  78. if (format === "hex" && enableAlpha) {
  79. _format = "hex8";
  80. }
  81. this._tiny = new tinycolor.TinyColor({
  82. h: _hue,
  83. s: _saturation / 100,
  84. v: _value / 100,
  85. a: _alpha / 100
  86. });
  87. this.value = this._isValid ? this._tiny.toString(_format) : "";
  88. }
  89. }
  90. exports["default"] = Color;
  91. //# sourceMappingURL=color.js.map