paper.js/src/color/Gradient.js

69 lines
1.7 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({
beans: true,
// TODO: should type here be called 'radial' and have it
// receive a boolean value?
initialize: function(stops, type) {
2011-05-03 03:55:01 -04:00
if (!stops) {
stops = [new GradientStop('white', 0),
new GradientStop('black', 1)];
}
this.setStops(stops);
this.type = type ? type : 'linear';
},
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.');
2011-05-03 03:55:01 -04:00
if (!(stops[0] instanceof GradientStop)) {
2011-05-03 03:54:13 -04:00
for (var i = 0, l = stops.length; i < l; i++) {
var rampPoint;
var stop = stops[i];
// If it is an array, the second argument is the rampPoint:
if (Array.isArray(stop)) {
rampPoint = stop[1];
stop = stop[0];
} else {
// Otherwise stops is an array of colors, and we need to
// calculate the midPoint:
rampPoint = i / (l - 1);
}
stops[i] = new GradientStop(stop, rampPoint);
}
}
this._stops = stops;
},
equals: function(gradient) {
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
});