All files / lib/style Gradient.ts

73.86% Statements 65/88
60.86% Branches 14/23
63.63% Functions 7/11
73.86% Lines 65/88

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                              1x 1x 1x 1x                                                                                                         1x 1x 1x     1x     8x 8x 5x   5x 5x     8x 3x 3x 8x   6x 6x 8x   1x 1x 1x 1x 1x         1x     16x     16x           1x 7x 7x 7x         1x                     1x                           1x       1x 8x       8x 8x     8x   8x 8x 8x               1x       1x 8x 8x 8x               1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x   1x 1x 1x   1x  
/*
 * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
 * http://paperjs.org/
 *
 * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
 * http://juerglehni.com/ & https://puckey.studio/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * All rights reserved.
 */
 
// TODO: remove eslint-disable comment and deal with errors over time
/* eslint-disable */
 
import { ref } from '~/globals';
import { Base } from '~/straps';
import { UID } from '~/util/UID';
import { GradientStop } from './GradientStop';
 
/**
 * @name Gradient
 *
 * @class The Gradient object.
 *
 * @classexample {@paperscript height=300}
 * // Applying a linear gradient color containing evenly distributed
 * // color stops:
 *
 * // Define two points which we will be using to construct
 * // the path and to position the gradient color:
 * var topLeft = view.center - [80, 80];
 * var bottomRight = view.center + [80, 80];
 *
 * // Create a rectangle shaped path between
 * // the topLeft and bottomRight points:
 * var path = new Path.Rectangle({
 *     topLeft: topLeft,
 *     bottomRight: bottomRight,
 *     // Fill the path with a gradient of three color stops
 *     // that runs between the two points we defined earlier:
 *     fillColor: {
 *         gradient: {
 *             stops: ['yellow', 'red', 'blue']
 *         },
 *         origin: topLeft,
 *         destination: bottomRight
 *     }
 * });
 *
 * @classexample {@paperscript height=300}
 * // Create a circle shaped path at the center of the view,
 * // using 40% of the height of the view as its radius
 * // and fill it with a radial gradient color:
 * var path = new Path.Circle({
 *     center: view.center,
 *     radius: view.bounds.height * 0.4
 * });
 *
 * // Fill the path with a radial gradient color with three stops:
 * // yellow from 0% to 5%, mix between red from 5% to 20%,
 * // mix between red and black from 20% to 100%:
 * path.fillColor = {
 *     gradient: {
 *         stops: [['yellow', 0.05], ['red', 0.2], ['black', 1]],
 *         radial: true
 *     },
 *     origin: path.position,
 *     destination: path.bounds.rightCenter
 * };
 */
export const Gradient = Base.extend(
  /** @lends Gradient# */ {
    _class: 'Gradient',
 
    // DOCS: Document #initialize()
    initialize: function Gradient(stops, radial) {
      // Use UID here since Gradients are exported through dictionary.add().
      // @ts-expect-error = Expected 1 arguments, but got 0
      this._id = UID.get();
      if (stops && Base.isPlainObject(stops)) {
        this.set(stops);
        // Erase arguments since we used the passed object instead.
        stops = radial = null;
      }
      // As these values might already have been set in the _set() call above,
      // only initialize them if that hasn't happened yet.
      if (this._stops == null) {
        this.setStops(stops || ['white', 'black']);
      }
      if (this._radial == null) {
        // Support old string type argument and new radial boolean.
        this.setRadial((typeof radial === 'string' && radial === 'radial') || radial || false);
      }
    },
 
    _serialize: function (options, dictionary) {
      return dictionary.add(this, function () {
        return Base.serialize([this._stops, this._radial], options, true, dictionary);
      });
    },
 
    /**
     * Called by various setters whenever a gradient value changes
     */
    _changed: function () {
      // Loop through the gradient-colors that use this gradient and notify
      // them, so they can notify the items they belong to.
      for (var i = 0, l = this._owners && this._owners.length; i < l; i++) {
        this._owners[i]._changed();
      }
    },
 
    /**
     * Called by Color#setGradient()
     * This is required to pass on _changed() notifications to the _owners.
     */
    _addOwner: function (color) {
      if (!this._owners) this._owners = [];
      this._owners.push(color);
    },
 
    /**
     * Called by Color whenever this gradient stops being used.
     */
    _removeOwner: function (color) {
      var index = this._owners ? this._owners.indexOf(color) : -1;
      if (index != -1) {
        this._owners.splice(index, 1);
        if (!this._owners.length) this._owners = undefined;
      }
    },
 
    /**
     * @return {Gradient} a copy of the gradient
     */
    clone: function () {
      var stops = [];
      for (var i = 0, l = this._stops.length; i < l; i++) {
        stops[i] = this._stops[i].clone();
      }
      return new Gradient(stops, this._radial);
    },
 
    /**
     * The gradient stops on the gradient ramp.
     *
     * @bean
     * @type GradientStop[]
     */
    getStops: function () {
      return this._stops;
    },
 
    setStops: function (stops) {
      if (stops.length < 2) {
        throw new Error('Gradient stop list needs to contain at least two stops.');
      }
      // If this gradient already contains stops, first remove their owner.
      var _stops = this._stops;
      if (_stops) {
        for (var i = 0, l = _stops.length; i < l; i++) _stops[i]._owner = undefined;
      }
      _stops = this._stops = GradientStop.readList(stops, 0, { clone: true });
      // Now assign this gradient as the new gradients' owner.
      for (var i = 0, l = _stops.length; i < l; i++) _stops[i]._owner = this;
      this._changed();
    },
 
    /**
     * Specifies whether the gradient is radial or linear.
     *
     * @bean
     * @type Boolean
     */
    getRadial: function () {
      return this._radial;
    },
 
    setRadial: function (radial) {
      this._radial = radial;
      this._changed();
    },
 
    /**
     * Checks whether the gradient is equal to the supplied gradient.
     *
     * @param {Gradient} gradient
     * @return {Boolean} {@true if they are equal}
     */
    equals: function (gradient) {
      if (gradient === this) return true;
      if (gradient && this._class === gradient._class) {
        var stops1 = this._stops,
          stops2 = gradient._stops,
          length = stops1.length;
        if (length === stops2.length) {
          for (var i = 0; i < length; i++) {
            if (!stops1[i].equals(stops2[i])) return false;
          }
          return true;
        }
      }
      return false;
    },
  }
);
 
ref.Gradient = Gradient;