2011-03-06 19:50:44 -05:00
|
|
|
/*
|
|
|
|
* Paper.js
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
2011-03-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-03-06 19:50:44 -05:00
|
|
|
* http://scriptographer.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-06-22 18:56:05 -04:00
|
|
|
/**
|
|
|
|
* @name Gradient
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @class The Gradient object.
|
|
|
|
*/
|
|
|
|
var Gradient = this.Gradient = Base.extend(/** @lends Gradient# */{
|
2011-06-01 05:49:43 -04:00
|
|
|
// TODO: Should type here be called 'radial' and have it receive a
|
|
|
|
// boolean value?
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
|
|
|
* Creates a gradient object
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-25 18:55:44 -04:00
|
|
|
* @param {GradientStop[]} stops
|
2011-05-27 15:21:49 -04:00
|
|
|
* @param {String} [type='linear'] 'linear' or 'radial'
|
2011-05-25 18:55:44 -04:00
|
|
|
*/
|
2011-04-07 11:01:49 -04:00
|
|
|
initialize: function(stops, type) {
|
2011-05-19 15:54:52 -04:00
|
|
|
this.setStops(stops || ['white', 'black']);
|
|
|
|
this.type = type || 'linear';
|
2011-02-19 16:50:37 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
|
|
|
* @return {Gradient} a copy of the gradient
|
|
|
|
*/
|
2011-05-19 17:02:26 -04:00
|
|
|
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.type);
|
|
|
|
},
|
|
|
|
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
|
|
|
* The gradient stops on the gradient ramp.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-26 14:09:25 -04:00
|
|
|
* @type GradientStop[]
|
2011-05-25 18:55:44 -04:00
|
|
|
* @bean
|
|
|
|
*/
|
2011-02-19 16:50:37 -05:00
|
|
|
getStops: function() {
|
|
|
|
return this._stops;
|
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-19 16:50:37 -05:00
|
|
|
setStops: function(stops) {
|
2011-02-20 21:32:39 -05:00
|
|
|
if (stops.length < 2)
|
2011-03-03 17:45:17 -05:00
|
|
|
throw new Error(
|
|
|
|
'Gradient stop list needs to contain at least two stops.');
|
2011-05-19 15:51:09 -04:00
|
|
|
this._stops = GradientStop.readAll(stops);
|
|
|
|
// Now reassign ramp points if they were not specified.
|
|
|
|
for (var i = 0, l = this._stops.length; i < l; i++) {
|
|
|
|
var stop = this._stops[i];
|
|
|
|
if (stop._defaultRamp)
|
|
|
|
stop.setRampPoint(i / (l - 1));
|
2011-04-07 11:01:49 -04:00
|
|
|
}
|
2011-05-05 15:28:28 -04:00
|
|
|
},
|
2011-05-19 15:51:09 -04:00
|
|
|
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
|
|
|
* Checks whether the gradient is equal to the supplied gradient.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-25 18:55:44 -04:00
|
|
|
* @param {Gradient} gradient
|
2011-05-30 13:42:17 -04:00
|
|
|
* @return {Boolean} {@true they are equal}
|
2011-05-25 18:55:44 -04:00
|
|
|
*/
|
2011-05-05 15:28:28 -04:00
|
|
|
equals: function(gradient) {
|
2011-05-21 06:06:37 -04:00
|
|
|
if (gradient.type != this.type)
|
|
|
|
return false;
|
2011-05-19 17:00:03 -04:00
|
|
|
if (this._stops.length == gradient._stops.length) {
|
|
|
|
for (var i = 0, l = this._stops.length; i < l; i++) {
|
|
|
|
if (!this._stops[i].equals(gradient._stops[i]))
|
2011-05-05 15:28:28 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-02-19 16:50:37 -05:00
|
|
|
}
|
2011-03-03 11:32:55 -05:00
|
|
|
});
|