-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolors.js
37 lines (35 loc) · 1.13 KB
/
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
/*jslint sloppy: true, white: true */
var toDegree = 180 / Math.PI;
function fromArgb(a, r, g, b) {
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + a/255 + ')';
}
function convertHsvToRgb(h, s, v) {
var a, b, c, d, hueFloor;
h = h / 360;
if (s > 0) {
if (h >= 1) {
h = 0;
}
h = 6 * h;
hueFloor = Math.floor(h);
a = Math.round(255 * v * (1.0 - s));
b = Math.round(255 * v * (1.0 - (s * (h - hueFloor))));
c = Math.round(255 * v * (1.0 - (s * (1.0 - (h - hueFloor)))));
d = Math.round(255 * v);
switch (hueFloor) {
case 0: return fromArgb(255, d, c, a);
case 1: return fromArgb(255, b, d, a);
case 2: return fromArgb(255, a, d, c);
case 3: return fromArgb(255, a, b, d);
case 4: return fromArgb(255, c, a, d);
case 5: return fromArgb(255, d, a, b);
default: return fromArgb(0, 0, 0, 0);
}
}
d = v * 255;
return fromArgb(255, d, d, d);
}
function getColor(x, y) {
var hue = Math.atan2(y, x) * toDegree + 360;
return convertHsvToRgb(hue, 1, 1);
}