All files / lib/style GradientStop.ts

98.24% Statements 56/57
80.95% Branches 17/21
100% Functions 9/9
98.24% Lines 56/57

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                              1x 1x 1x               1x 1x 1x                   1x   28x 28x 28x       5x   1x 1x 5x   2x 2x 2x 5x 28x 28x 28x           1x 3x 3x   1x 3x 3x 3x 3x         1x       56x 56x                                                                                 1x 4x 4x   1x 28x 28x 28x             1x 1x                                                                               1x 4x 4x   1x   28x 28x 28x 28x   1x 3x 3x 3x     3x 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 { Base } from '~/straps';
import { Change } from '~/item/ChangeFlag';
import { Color } from './Color';
 
// TODO: Support midPoint? (initial tests didn't look nice)
/**
 * @name GradientStop
 *
 * @class The GradientStop object.
 */
export const GradientStop = Base.extend(
  /** @lends GradientStop# */ {
    _class: 'GradientStop',
 
    /**
     * Creates a GradientStop object.
     *
     * @param {Color} [color=new Color(0, 0, 0)] the color of the stop
     * @param {Number} [offset=null] the position of the stop on the gradient
     * ramp as a value between `0` and `1`; `null` or `undefined` for automatic
     * assignment.
     */
    initialize: function GradientStop(arg0, arg1) {
      // (color, offset)
      var color = arg0,
        offset = arg1;
      if (typeof arg0 === 'object' && arg1 === undefined) {
        // Make sure the first entry in the array is not a number, in which
        // case the whole array would be a color, and the assignments would
        // already have occurred correctly above.
        if (Array.isArray(arg0) && typeof arg0[0] !== 'number') {
          // ([color, offset])
          color = arg0[0];
          offset = arg0[1];
        } else if ('color' in arg0 || 'offset' in arg0 || 'rampPoint' in arg0) {
          // (stop)
          color = arg0.color;
          offset = arg0.offset || arg0.rampPoint || 0;
        }
      }
      this.setColor(color);
      this.setOffset(offset);
    },
 
    // TODO: Do we really need to also clone the color here?
    /**
     * @return {GradientStop} a copy of the gradient-stop
     */
    clone: function () {
      return new GradientStop(this._color.clone(), this._offset);
    },
 
    _serialize: function (options, dictionary) {
      var color = this._color,
        offset = this._offset;
      return Base.serialize(offset == null ? [color] : [color, offset], options, true, dictionary);
    },
 
    /**
     * Called by various setters whenever a value changes
     */
    _changed: function () {
      // Notify the graident that uses this stop about the change, so it can
      // notify its gradient colors, which in turn will notify the items they
      // are used in:
      if (this._owner) this._owner._changed(/*#=*/ Change.STYLE);
    },
 
    /**
     * The ramp-point of the gradient stop as a value between `0` and `1`.
     *
     * @bean
     * @type Number
     *
     * @example {@paperscript height=300}
     * // Animating a gradient's ramp points:
     *
     * // 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
     * });
     *
     * path.fillColor = {
     *     gradient: {
     *         stops: [['yellow', 0.05], ['red', 0.2], ['black', 1]],
     *         radial: true
     *     },
     *     origin: path.position,
     *     destination: path.bounds.rightCenter
     * };
     *
     * var gradient = path.fillColor.gradient;
     *
     * // This function is called each frame of the animation:
     * function onFrame(event) {
     *     var blackStop = gradient.stops[2];
     *     // Animate the offset between 0.7 and 0.9:
     *     blackStop.offset = Math.sin(event.time * 5) * 0.1 + 0.8;
     *
     *     // Animate the offset between 0.2 and 0.4
     *     var redStop = gradient.stops[1];
     *     redStop.offset = Math.sin(event.time * 3) * 0.1 + 0.3;
     * }
     */
    getOffset: function () {
      return this._offset;
    },
 
    setOffset: function (offset) {
      this._offset = offset;
      this._changed();
    },
 
    /**
     * @private
     * @bean
     * @deprecated use {@link #offset} instead.
     */
    getRampPoint: '#getOffset',
    setRampPoint: '#setOffset',
 
    /**
     * The color of the gradient stop.
     *
     * @bean
     * @type Color
     *
     * @example {@paperscript height=300}
     * // Animating a gradient's ramp points:
     *
     * // 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
     * });
     *
     * path.fillColor = {
     *     gradient: {
     *         stops: [['yellow', 0.05], ['red', 0.2], ['black', 1]],
     *         radial: true
     *     },
     *     origin: path.position,
     *     destination: path.bounds.rightCenter
     * };
     *
     * var redStop = path.fillColor.gradient.stops[1];
     * var blackStop = path.fillColor.gradient.stops[2];
     *
     * // This function is called each frame of the animation:
     * function onFrame(event) {
     *     // Animate the offset between 0.7 and 0.9:
     *     blackStop.offset = Math.sin(event.time * 5) * 0.1 + 0.8;
     *
     *     // Animate the offset between 0.2 and 0.4
     *     redStop.offset = Math.sin(event.time * 3) * 0.1 + 0.3;
     * }
     */
    getColor: function () {
      return this._color;
    },
 
    setColor: function (/* color */) {
      // Clear old color owner before setting new one:
      Color._setOwner(this._color, null);
      this._color = Color._setOwner(Color.read(arguments, 0), this, 'setColor');
      this._changed();
    },
 
    equals: function (stop) {
      return (
        stop === this ||
        (stop && this._class === stop._class && this._color.equals(stop._color) && this._offset == stop._offset) ||
        false
      );
    },
  }
);