-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcolor.js
63 lines (56 loc) · 1.67 KB
/
rcolor.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
// Free to use & distribute under the MIT license
// Wes Johnson (@SterlingWes)
//
// inspired by http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory;
} else if (typeof define === 'function' && define.amd) {
define(factory);
} else {
root.RColor = factory();
}
}(this, function () {
var RColor = function() {
this.hue = Math.random(),
this.goldenRatio = 0.618033988749895;
this.hexwidth = 2;
};
RColor.prototype.hsvToRgb = function (h,s,v) {
var h_i = Math.floor(h*6),
f = h*6 - h_i,
p = v * (1-s),
q = v * (1-f*s),
t = v * (1-(1-f)*s),
r = 255,
g = 255,
b = 255;
switch(h_i) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return [Math.floor(r*256),Math.floor(g*256),Math.floor(b*256)];
};
RColor.prototype.padHex = function(str) {
if(str.length > this.hexwidth) return str;
return new Array(this.hexwidth - str.length + 1).join('0') + str;
};
RColor.prototype.get = function(hex,saturation,value) {
this.hue += this.goldenRatio;
this.hue %= 1;
if(typeof saturation !== "number") saturation = 0.5;
if(typeof value !== "number") value = 0.95;
var rgb = this.hsvToRgb(this.hue,saturation,value);
if(hex)
return "#" + this.padHex(rgb[0].toString(16))
+ this.padHex(rgb[1].toString(16))
+ this.padHex(rgb[2].toString(16));
else
return rgb;
};
return RColor;
}));