This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
golden-colors.js
84 lines (68 loc) · 1.97 KB
/
golden-colors.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
var goldenColors = {};
(function () {
var goldenRatioConjugate = 0.618033988749895;
function getRandom255 () {
return Math.round(Math.random() * 255);
}
function convertDecToHex (number) {
var result = number.toString(16);
if (result.length === 1) {
result = '0' + result;
}
return result;
}
function convertHsvToRgb (h, s, v) {
var r, g, b;
var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch(i % 6){
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.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
function WebColor (r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}
WebColor.prototype.toRgb = function () {
return [this.r, this.g, this.b];
};
WebColor.prototype.toRgbString = function () {
return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')';
};
WebColor.prototype.toHexString = function () {
return '#' +
convertDecToHex(this.r) +
convertDecToHex(this.g) +
convertDecToHex(this.b);
};
WebColor.prototype.toString = function () {
return this.toHexString();
};
WebColor.prototype.toJSON = function () {
return this.toHexString();
};
goldenColors.getNaive = function () {
return new WebColor(getRandom255(), getRandom255(), getRandom255());
};
goldenColors.getHsvSimple = function (s, v) {
var color = convertHsvToRgb(Math.random(), s, v);
return new WebColor(color[0], color[1], color[2]);
};
var h = Math.random();
goldenColors.getHsvGolden = function (s, v) {
h += goldenRatioConjugate;
h %= 1;
var color = convertHsvToRgb(h, s, v);
return new WebColor(color[0], color[1], color[2]);
};
})();