-
Notifications
You must be signed in to change notification settings - Fork 25
/
hcg.js
168 lines (156 loc) · 3.06 KB
/
hcg.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
167
168
/**
* @module color-space/hcg
*/
import rgb from './rgb.js';
import hsl from './hsl.js';
import hsv from './hsv.js';
import hwb from './hwb.js';
export default {
name: 'hcg',
min: [0,0,0],
max: [360,100,100],
channel: ['hue', 'chroma', 'gray'],
alias: ['HCG', 'HSG'],
rgb: function(hcg) {
var h = hcg[0] / 360;
var c = hcg[1] / 100;
var g = hcg[2] / 100;
if (c === 0.0) {
return [g * 255, g * 255, g * 255];
}
var hi = (h % 1) * 6;
var v = (hi % 1);
var pure = [0, 0, 0];
var w = 1 - v;
switch (Math.floor(hi)) {
case 0:
pure[0] = 1; pure[1] = v; pure[2] = 0; break;
case 1:
pure[0] = w; pure[1] = 1; pure[2] = 0; break;
case 2:
pure[0] = 0; pure[1] = 1; pure[2] = v; break;
case 3:
pure[0] = 0; pure[1] = w; pure[2] = 1; break;
case 4:
pure[0] = v; pure[1] = 0; pure[2] = 1; break;
default:
pure[0] = 1; pure[1] = 0; pure[2] = w;
}
var mg = (1.0 - c) * g;
var rgb = [
(c * pure[0] + mg) * 255,
(c * pure[1] + mg) * 255,
(c * pure[2] + mg) * 255
];
return rgb;
},
hsl: function(hcg) {
var c = hcg[1] / 100;
var g = hcg[2] / 100;
var l = g * (1.0 - c) + 0.5 * c;
var s = 0;
if (l < 1.0 && l > 0.0) {
if (l < 0.5) {
s = c / (2 * l);
} else {
s = c / (2 * (1 - l));
}
}
return [hcg[0], s * 100, l * 100];
},
hsv: function(hcg){
var c = hcg[1] / 100;
var g = hcg[2] / 100;
var v = c + g * (1.0 - c);
var res;
if (v > 0.0) {
var f = c / v;
res = [hcg[0], f * 100, v * 100];
} else {
res = [hcg[0], 0, v * 100];
}
return res;
},
hwb: function(hcg){
var c = hcg[1] / 100;
var g = hcg[2] / 100;
var v = c + g * (1.0 - c);
return [hcg[0], (v - c) * 100, (1 - v) * 100];
}
};
//append rgb
rgb.hcg = function(rgb) {
var r = rgb[0] / 255;
var g = rgb[1] / 255;
var b = rgb[2] / 255;
var max = Math.max(Math.max(r, g), b);
var min = Math.min(Math.min(r, g), b);
var chroma = (max - min);
var grayscale;
var hue;
if (chroma < 1) {
grayscale = min / (1 - chroma);
} else {
grayscale = 0;
}
if (chroma > 0) {
if (max === r) {
hue = ((g - b) / chroma % 6);
} else
if (max === g) {
hue = 2 + (b - r) / chroma;
} else {
hue = 4 + (r - g) / chroma;
}
hue /= 6;
hue = (hue % 1);
} else {
hue = 0;
}
return [hue * 360, chroma * 100, grayscale * 100];
};
//extend hsl
hsl.hcg = function(hsl) {
var s = hsl[1] / 100;
var l = hsl[2] / 100;
var c = 0;
if (l < 0.5) {
c = 2.0 * s * l;
} else {
c = 2.0 * s * (1.0 - l);
}
var res;
if (c < 1.0) {
var f = (l - 0.5 * c) / (1.0 - c);
res = [hsl[0], c * 100, f * 100];
} else {
res = [hsl[0], c * 100, 0];
}
return res;
};
//extend hsv
hsv.hcg = function(hsv){
var s = hsv[1] / 100;
var v = hsv[2] / 100;
var c = s * v;
var res;
if (c < 1.0) {
var f = (v - c) / (1 - c);
res = [hsv[0], c * 100, f * 100];
} else {
res = [hsv[0], c * 100, 0];
}
return res;
}
//extend hwb
hwb.hcg = function(hwb){
var w = hwb[1] / 100;
var b = hwb[2] / 100;
var v = 1 - b;
var c = v - w;
var g = 0;
if (c < 1) {
g = (v - c) / (1 - c);
}
return [hwb[0], c * 100, g * 100];
}