paper.js/src/color/Gradient.js

90 lines
2.1 KiB
JavaScript
Raw Normal View History

2011-03-06 19:50:44 -05:00
/*
* Paper.js
*
* 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-03-07 20:41:50 -05:00
* Distributed under the MIT license. See LICENSE file for details.
*
2011-03-06 19:50:44 -05:00
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
2011-03-07 20:41:50 -05:00
* All rights reserved.
2011-03-06 19:50:44 -05:00
*/
var Gradient = this.Gradient = Base.extend({
/** @lends Gradient# */
beans: true,
2011-05-26 07:04:47 -04:00
// TODO: Should type here be called 'radial' and have it
// receive a boolean value?
/**
* Creates a gradient object
*
* @param {GradientStop[]} stops
* @param {String} [type='linear'] 'linear' or 'radial'
* @constructs Gradient
*
* @class The Gradient object.
*/
initialize: function(stops, type) {
2011-05-19 15:54:52 -04:00
this.setStops(stops || ['white', 'black']);
this.type = type || 'linear';
},
/**
* @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.type);
},
/**
* The gradient stops on the gradient ramp.
*
* @type GradientStop[]
* @bean
*/
getStops: function() {
return this._stops;
},
setStops: function(stops) {
2011-02-20 21:32:39 -05:00
if (stops.length < 2)
throw new Error(
'Gradient stop list needs to contain at least two stops.');
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));
}
},
/**
* Checks whether the gradient is equal to the supplied gradient.
*
* @param {Gradient} gradient
* @return {Boolean} {@true they are equal}
*/
equals: function(gradient) {
if (gradient.type != this.type)
return false;
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]))
return false;
}
return true;
}
return false;
}
2011-03-03 11:32:55 -05:00
});