-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrainbow.js
111 lines (83 loc) · 3.25 KB
/
rainbow.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
( function() {
var numRange = 100;
//RAINBOW CLASS
function Rainbow( spectrum ) {
this._gradients = null;
this._colors = null;
this.setColors( spectrum );
}
Rainbow.prototype = {
setColors: function( spectrum ) {
var increment = this._segmentLength = numRange / ( spectrum.length - 1 );
this._gradients = [];
for ( var i = 0; i < spectrum.length - 1; i++ ) {
var colorGradient = new ColorGradient(
spectrum[ i ],
spectrum[ i + 1 ],
increment * i,
increment * ( i + 1 )
);
this._gradients[ i ] = colorGradient;
}
this._colors = spectrum;
},
colorAt: function( ratio, inRgb ) {
var number = ratio * numRange;
var index = Math.min( Math.floor( number / this._segmentLength ), this._gradients.length - 1 );
return this._gradients[ index ].colorAt( number, inRgb );
}
}
//COLOR GRADIENT CLASS
function ColorGradient( colorStart, colorEnd, minNumber, maxNumber ) {
this._startColor = getHexColor( colorStart );
this._endColor = getHexColor( colorEnd );
this._minNumber = minNumber;
this._maxNumber = maxNumber;
this._range = this._maxNumber - this._minNumber;
}
ColorGradient.prototype = {
colorAt: function( number, inRgb ) {
var r = this.calcInt( number, this._startColor.substring( 0, 2 ), this._endColor.substring( 0, 2 ) );
var g = this.calcInt( number, this._startColor.substring( 2, 4 ), this._endColor.substring( 2, 4 ) );
var b = this.calcInt( number, this._startColor.substring( 4, 6 ), this._endColor.substring( 4, 6 ) );
if ( inRgb ) {
return [ r, g, b ];
} else {
return calcHex( r ) + calcHex( g ) + calcHex( b );
}
},
calcInt: function( number, channelStart_Base16, channelEnd_Base16 ) {
var num = number;
if ( number < this._minNumber ) {
num = this._minNumber;
} else if ( number > this._maxNumber ) {
num = this._maxNumber;
}
var cStart_Base10 = parseInt( channelStart_Base16, 16 );
var cEnd_Base10 = parseInt( channelEnd_Base16, 16 );
var cPerUnit = ( cEnd_Base10 - cStart_Base10 ) / this._range;
var c_Base10 = Math.round( cPerUnit * ( num - this._minNumber ) + cStart_Base10 );
return c_Base10;
}
}
//UTILS
function calcHex( integer ) {
return formatHex( integer.toString( 16 ) );
}
function formatHex( hex ) {
return ( hex.length === 1 ) ? '0' + hex : hex;
}
function getHexColor( string ) {
return string.substring( string.length - 6, string.length );
}
//EXPORTING as global var in browser of as export in node
var root = this;
if ( typeof exports !== 'undefined' ) {
if ( typeof module !== 'undefined' && module.exports ) {
exports = module.exports = Rainbow;
}
exports.Rainbow = Rainbow;
} else {
root.Rainbow = Rainbow;
}
} )();