GradientStyle.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import {Style} from "./Style";
  2. import {GradientColorStop} from "./GradientColorStop";
  3. /**
  4. * Gradient style is used to represent any type of gradient based style.
  5. *
  6. * It handles any gradient based operations and should be used as base for other gradient styles.
  7. *
  8. * @class
  9. * @extends {Style}
  10. */
  11. function GradientStyle()
  12. {
  13. Style.call(this);
  14. /**
  15. * List of colors that compose this gradient ordered.
  16. *
  17. * You need to add at least one color stop to have a visible gradient.
  18. *
  19. * @type {GradientColorStop[]}
  20. */
  21. this.colors = [];
  22. }
  23. GradientStyle.prototype = Object.create(Style.prototype);
  24. /**
  25. * Add a new color stop defined by an offset and a color to the gradient.
  26. *
  27. * If the offset is not between 0 and 1 inclusive, or if color can't be parsed as a CSS color, an error is raised.
  28. *
  29. * @param {number} offset Offset of the color stop between 0 and 1 inclusive.
  30. * @param {string} color CSS color value.
  31. */
  32. GradientStyle.prototype.addColorStop = function(offset, color)
  33. {
  34. this.colors.push(new GradientColorStop(offset, color));
  35. };
  36. GradientStyle.prototype.serialize = function()
  37. {
  38. return {
  39. colors: this.colors
  40. };
  41. };
  42. GradientStyle.prototype.parse = function(data)
  43. {
  44. var colors = [];
  45. for(var i = 0; i < data.colors.length; i++)
  46. {
  47. colors.push(new GradientColorStop(data.colors[i].offset, data.colors[i].color));
  48. }
  49. this.colors = colors;
  50. };
  51. export {GradientStyle};