/
ColorGradient.js
166 lines (154 loc) · 4.1 KB
/
ColorGradient.js
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
define(function(require, exports, module) {
var filter = require('../internals').filter,
numberComparator = require('../internals').numberComparator,
mathUtils = require('../math/mathUtils'),
LinearInterpolation = require('../math/LinearInterpolation'),
ColorList = require('./ColorList');
//a protected object for every point on the gradient
var _GradPoint = function(p, c){
this.pos = p;
this.color = c;
};
_GradPoint.prototype = {
compareTo: function(p){
if(numberComparator(p.pos,this.pos) === 0){
return 0;
}
return this.pos < p.pos ? -1 : 1;
},
getColor: function(){
return this.color;
},
getPosition: function(){
return this.pos;
}
};
var ColorGradient = function(){
this.gradient = [];
this.interpolator = new LinearInterpolation();
this.maxDither = 0;
};
ColorGradient.prototype = {
/**
* Adds a new color at specified position.
* @param {Number} p position in the gradient
* @param {toxi.color.TColor} c color to add
*/
addColorAt: function(p, c){
this.gradient.push(new _GradPoint(p,c));
},
/**
* Calculates the gradient from specified position.
* @param {Number} pos position to start at (float)
* @param {Number} width (integer)
* @return list of interpolated gradient colors
*/
calcGradient: function(pos, width){
if( arguments.length === 0 ){
pos = this.gradient[0].getPosition();
var last = this.gradient[this.gradient.length-1].getPosition();
width = Math.floor(last - pos);
}
var result = new ColorList();
if( this.gradient.length === 0 ){
return result;
}
var frac = 0,
currPoint, nextPoint,
endPos = pos + width,
i = 0,
l = this.gradient.length;
for( i=0; i<l; i++ ){
if( this.gradient[i].pos < pos ){
currPoint = this.gradient[i];
}
}
var isPremature = (currPoint===undefined),
activeGradient;
if( !isPremature ){
activeGradient = filter(this.gradient, function( g ){ return g.pos >= currPoint.pos; });
} else {
//start position is before 1st gradient color, so use whole
activeGradient = this.gradient;
currPoint = this.gradient[0];
}
var currWidth = 0;
//start over with i, and use it to iterate
i = 0;
l = activeGradient.length;
if( currPoint !== activeGradient[l-1] ){
nextPoint = activeGradient[i];
if( isPremature ){
var d = currPoint.pos - pos;
currWidth = mathUtils.abs( d ) > 0 ? 1 / d : 1;
} else {
if( nextPoint.pos - currPoint.pos > 0 ) {
currWidth = 1 / (nextPoint.pos - currPoint.pos);
}
}
}
while( pos < endPos ){
if( isPremature ){
frac = 1 - (currPoint.pos - pos) * currWidth;
} else {
frac = (pos - currPoint.pos) * currWidth;
}
//switch to next color?
if( frac > 1.0 ){
currPoint = nextPoint;
isPremature = false;
i++;
if( i < activeGradient.length ){
nextPoint = activeGradient[i];
if( currPoint !== activeGradient[l-1] ){
currWidth = 1 / (nextPoint.pos - currPoint.pos);
} else {
currWidth = 0;
}
frac = (pos - currPoint.pos) * currWidth;
}
}
if( currPoint !== activeGradient[l-1] ){
var ditheredFrac = mathUtils.clip( frac+mathUtils.normalizedRandom() * this.maxDither, 0, 1 );
ditheredFrac = this.interpolator.interpolate( 0, 1, ditheredFrac );
result.add( currPoint.color.getBlended(nextPoint.color, ditheredFrac) );
} else {
result.add( currPoint.color.copy() );
}
pos++;
}
return result;
},
getGradientPoints: function(){
return this.gradient;
},
/**
* @return the interpolator
*/
getInterpolator: function(){
return this.interpolator;
},
/**
* @return the maximum dither amount.
*/
getMaxDither: function(){
return this.maxDither;
},
/**
* @param interpolator the interpolator to set
*/
setInterpolator: function(interpolator){
this.interpolator = interpolator;
},
/**
* Sets the maximum dither amount. Setting this to values >0 will jitter the
* interpolated colors in the calculated gradient. The value range for this
* parameter is 0.0 (off) to 1.0 (100%).
* @param {Number} maxDither
*/
setMaxDither: function(maxDither){
this.maxDither = mathUtils.clip(maxDither, 0, 1);
}
};
module.exports = ColorGradient;
});