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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=8.0, user-scalable=1">
<title>Color, Harmony, and Math</title>
<style type="text/css">
.color-wheel {
width: 40vw;
height: 40vw;
}
</style>
<script>
// https://en.wikipedia.org/wiki/CIELAB_color_space
//
// CIELAB colors are stored as arrays [ L*, a*, b* ].
// L* is scaled from 0.0 to 1.0, while a* and b* are scaled from -1.0 to 1.0.
// https://en.wikipedia.org/wiki/CIE_1931_color_space
//
// CIEXYZ colors are stored as arrays [ X, Y, Z ].
// X, Y, and Z are scaled such that the Y of illuminant D65 is 1.0. This is convenient
// because the sRGB conversion is defined in terms of that scale.
// https://en.wikipedia.org/wiki/SRGB
// CIEXYZ color space
let ILLUMINANT_D65 = [ 0.950489, 1.0, 1.088840 ];
function main() {
let svgParameters = {
scale: 10000,
};
let svgState = svgInit(svgParameters);
let colorWheelParameters = {
scale: svgParameters.scale,
nStepsAround: 128,
nStepsOutward: 128,
};
generateColorWheel(svgState, colorWheelParameters);
let colorSelectionDotParameters = {
scale: svgParameters.scale,
};
let colorSelectionDotSvgCoordinates = [ 5000, -5000 ];
let colorSelectionDotState =
generateColorSelectionDot(svgState, colorSelectionDotParameters,
colorSelectionDotSvgCoordinates);
}
function generateColorWheel(svgState, colorWheelParameters) {
let radiusForStepOutward = function(stepOutward) {
return stepOutward / colorWheelParameters.nStepsOutward;
};
let thetaForStepAround = function(stepAround) {
return stepAround * 2 * Math.PI / colorWheelParameters.nStepsAround;
};
for (var iOutward = 0; colorWheelParameters.nStepsOutward > iOutward; iOutward++) {
for (var iAround = 0; colorWheelParameters.nStepsAround > iAround; iAround++) {
let chunkParameters = {
startTheta: thetaForStepAround(iAround),
endTheta: thetaForStepAround(iAround + 1),
outsideRadius: radiusForStepOutward(iOutward + 1),
insideRadius: radiusForStepOutward(iOutward),
isInnermostWedge: iOutward == 0,
};
generateOneColorWheelChunk(svgState, colorWheelParameters, chunkParameters);
}
}
}
function generateOneColorWheelChunk(svgState, colorWheelParameters, chunkParameters) {
let scale = colorWheelParameters.scale,
nStepsAround = colorWheelParameters.nStepsAround,
startTheta = chunkParameters.startTheta,
endTheta = chunkParameters.endTheta,
outsideRadius = chunkParameters.outsideRadius,
insideRadius = chunkParameters.insideRadius,
isInnermostWedge = chunkParameters.isInnermostWedge;
let outsideScale = scale * outsideRadius,
insideScale = scale * insideRadius,
coordinatesFromPolar = function(radius, theta) {
return [ Math.cos(theta) * radius, Math.sin(theta) * radius ];
},
startOutsideCoords = coordinatesFromPolar(outsideScale, startTheta),
endOutsideCoords = coordinatesFromPolar(outsideScale, endTheta),
moveParameters = [].concat(startOutsideCoords),
outerArcParameters = [outsideScale, outsideScale, 0, 0, 1]
.concat(endOutsideCoords);
let pathData = ['M'].concat(moveParameters).concat(['A'])
.concat(outerArcParameters);
if (isInnermostWedge) {
let centerCoords = [ 0, 0 ],
lineParameters = [].concat(centerCoords);
pathData = pathData.concat(['L']).concat(lineParameters);
} else {
let startInsideCoords = coordinatesFromPolar(insideScale, startTheta),
endInsideCoords = coordinatesFromPolar(insideScale, endTheta),
innerArcParameters = [insideScale, insideScale, 0, 0, 0]
.concat(startInsideCoords);
pathData = pathData.concat(['L']).concat(endInsideCoords).concat(['A'])
.concat(innerArcParameters);
}
pathData = pathData.concat(['z']).join(' ');
let gradientId = svgGenerateId(svgState, 'gradient');
let gradientElement = document.createElementNS(svgState.SVG_NS, 'linearGradient');
gradientElement.setAttribute('id', gradientId);
gradientElement.setAttribute('gradientUnits', 'userSpaceOnUse');
gradientElement.setAttribute('x1', startOutsideCoords[0]);
gradientElement.setAttribute('y1', startOutsideCoords[1]);
gradientElement.setAttribute('x2', endOutsideCoords[0]);
gradientElement.setAttribute('y2', endOutsideCoords[1]);
let stop1Element = document.createElementNS(svgState.SVG_NS, 'stop');
stop1Element.setAttribute('offset', '0%');
stop1Element.setAttribute('stop-color', colorFromPolar(outsideRadius, startTheta));
gradientElement.appendChild(stop1Element);
let stop2Element = document.createElementNS(svgState.SVG_NS, 'stop');
stop2Element.setAttribute('offset', '100%');
stop2Element.setAttribute('stop-color', colorFromPolar(outsideRadius, endTheta));
gradientElement.appendChild(stop2Element);
svgState.defsElement.appendChild(gradientElement);
let pathElement = document.createElementNS(svgState.SVG_NS, 'path');
pathElement.setAttribute('d', pathData);
pathElement.setAttribute('fill', 'url(#' + gradientId + ')');
pathElement.setAttribute('shape-rendering', 'crispEdges');
svgState.svgElement.appendChild(pathElement);
}
function generateColorSelectionDot(svgState, dotParameters, svgCoordinates) {
let dotId = svgGenerateId(svgState, 'colorSelectionDot');
let dotState = { };
let dragState = { };
let pointerStartHandler = function(event) {
colorSelectionDotPointerStart(event, svgState, dotState, dragState, dotParameters);
},
pointerMoveHandler = function(event) {
colorSelectionDotPointerMove(event, svgState, dotState, dragState, dotParameters);
};
let groupElement = document.createElementNS(svgState.SVG_NS, 'g');
groupElement.setAttribute('id', dotId);
groupElement.setAttribute('draggable', 'true');
groupElement.addEventListener('dragstart', pointerStartHandler);
let blackCircleElement = document.createElementNS(svgState.SVG_NS, 'circle');
blackCircleElement.setAttribute('r', '200');
blackCircleElement.setAttribute('fill', 'black');
groupElement.appendChild(blackCircleElement);
let whiteCircleElement = document.createElementNS(svgState.SVG_NS, 'circle');
whiteCircleElement.setAttribute('r', '150');
whiteCircleElement.setAttribute('stroke', 'white');
whiteCircleElement.setAttribute('stroke-width', '50');
groupElement.appendChild(whiteCircleElement);
svgState.svgElement.appendChild(groupElement);
dotState.element = groupElement;
colorSelectionDotSetLocation(dotState, svgCoordinates);
return dotState;
}
function colorSelectionDotSetLocation(dotState, svgCoordinates) {
let transform = 'translate(' + svgCoordinates.join(',') + ')';
dotState.element.setAttribute('transform', transform);
dotState.location = [].concat(svgCoordinates);
}
function colorSelectionDotPointerStart(event, svgState, dotState, dragState, dotParameters) {
let clientCoordinates = [ event.clientX, event.clientY ],
scale = dotParameters.scale,
svgCoordinates = clientCoordinatesToSvg(svgState, clientCoordinates, scale);
console.log('start', svgCoordinates);
dragState.isActive = true;
dragState.mouseDownLocation = svgCoordinates;
dragState.originalDotLocation = dotState.location;
}
function colorSelectionDotPointerMove(event, svgState, dotState, dragState, dotParameters) {
let eventClientCoordinates = [ event.clientX, event.clientY ],
scale = dotParameters.scale,
eventSvgCoordinates = clientCoordinatesToSvg(svgState, eventClientCoordinates, scale),
offset = [
eventSvgCoordinates[0] - dragState.mouseDownLocation[0],
eventSvgCoordinates[1] - dragState.mouseDownLocation[1],
],
newLocation = [
dragState.originalDotLocation[0] + offset[0],
dragState.originalDotLocation[1] + offset[1],
];
console.log('offset', offset);
colorSelectionDotSetLocation(dotState, newLocation);
}
function clientCoordinatesToSvg(svgState, clientCoordinates, scale) {
let svgRect = svgState.svgElement.getClientRects()[0],
clientWidth = svgState.svgElement.clientWidth,
clientHeight = svgState.svgElement.clientHeight,
horizontalScale = scale * 2.0 / clientWidth,
verticalScale = scale * 2.0 / clientHeight;
return [
(clientCoordinates[0] - svgRect.left) * horizontalScale - scale,
scale - (clientCoordinates[1] - svgRect.top) * verticalScale,
];
}
function svgInit(svgParameters) {
let svgState = { },
scale = svgParameters.scale;
viewBox = [ -1 * scale, -1 * scale, 2 * scale, 2 * scale ].join(' ');
svgState.SVG_NS = 'http://www.w3.org/2000/svg';
svgState.divElement = document.getElementById('color-wheel');
svgState.svgElement = document.createElementNS(svgState.SVG_NS, 'svg');
svgState.svgElement.setAttribute('class', 'color-wheel');
svgState.svgElement.setAttribute('viewBox', viewBox);
svgState.svgElement.setAttribute('transform', 'scale(1 -1)');
svgState.divElement.appendChild(svgState.svgElement);
svgState.defsElement = document.createElementNS(svgState.SVG_NS, 'defs');
svgState.svgElement.appendChild(svgState.defsElement);
svgState.idCounter = 0;
return svgState;
}
function svgGenerateId(svgState, prefix) {
let result = prefix + svgState.idCounter;
svgState.idCounter++;
return result
}
function colorFromPolar(radius, theta) {
let cielab = [ radius, Math.cos(theta), Math.sin(theta) ],
ciexyz = cielabToCiexyz(cielab, ILLUMINANT_D65),
sRGB = ciexyzToSRGB(ciexyz),
web = sRGBToWeb(sRGB);
return web;
}
function ciexyzToCielab(ciexyz, illuminant) {
let delta = 6.0 / 29.0,
f = function(t) {
if (t > Math.pow(delta, 3.0)) {
return Math.pow(delta, 1.0 / 3.0);
} else {
return (t / (3 * Math.pow(delta, 2.0))) + (4.0 / 29.0);
}
},
fX = f(ciexyz[0] / illuminant[0]),
fY = f(ciexyz[1] / illuminant[1]),
fZ = f(ciexyz[2] / illuminant[2]),
cielab = [ 1.16 * fX, 5.0 * (fX - fY), 2.0 * (fY - fZ) ];
return cielab;
}
function cielabToCiexyz(cielab, illuminant) {
let delta = 6.0 / 29.0,
fPrime = function(t) {
if (t > delta) {
return Math.pow(t, 3.0);
} else {
return 3 * Math.pow(t, 2.0) * (t - (4.0 / 29.0));
}
},
rescaledL = (cielab[0] + 0.16) / 1.16,
x = illuminant[0] * fPrime(rescaledL + (cielab[1] / 5.0));
y = illuminant[1] * fPrime(rescaledL);
z = illuminant[2] * fPrime(rescaledL - (cielab[2] / 2.0));
ciexyz = [ x, y, z ];
return ciexyz;
}
function ciexyzToSRGB(ciexyz) {
let rCoefficients = [ +3.24096994, -1.53738318, -0.49861076 ],
gCoefficients = [ -0.96924364, +1.87596750, +0.04155506 ],
bCoefficients = [ +0.05563008, -0.20397696, +1.05697151 ],
multiplyCoefficients = function(coefficients) {
return coefficients[0] * ciexyz[0]
+ coefficients[1] * ciexyz[1]
+ coefficients[2] * ciexyz[2];
},
rLinear = multiplyCoefficients(rCoefficients),
gLinear = multiplyCoefficients(gCoefficients),
bLinear = multiplyCoefficients(bCoefficients),
gamma = function(u) {
if (u > 0.0031308 ) {
return (211.0 * Math.pow(u, 5.0 / 12.0) - 11.0) / 200.0;
} else {
return u * (323.0 / 25.0);
}
},
sRGB = [ gamma(rLinear), gamma(gLinear), gamma(bLinear) ];
return sRGB;
}
function sRGBToCiexyz(sRGB) {
let gammaPrime = function(u) {
if (u > 0.04045) {
return Math.pow((200.0 * u + 11.0) / 211.0, 12.0 / 5.0);
} else {
return u * (25.0 / 323.0);
}
}
rgbLinear = gammaPrime(sRGB[0]),
gLinear = gammaPrime(sRGB[1]),
bLinear = gammaPrime(sRGB[2]),
xCoefficients = [ +0.41239080, +0.35758434, +0.18048079 ],
yCoefficients = [ +0.21263901, +0.71516868, +0.07219232 ],
zCoefficients = [ +0.01933082, +0.11919478, +0.95053215 ],
multiplyCoefficients = function(coefficients) {
return coefficients[0] * rLinear,
+ coefficients[1] * gLinear,
+ coefficients[2] * bLinear;
},
ciexyz = [ multiplyCoefficients(xCoefficients),
multiplyCoefficients(yCoefficients),
multiplyCoefficients(zCoefficients) ];
return ciexyz;
}
function sRGBToWeb(sRGB) {
let channelToHex = function(channel) {
let capped = Math.max(0.0, Math.min(channel, 1.0)),
scaled = Math.floor(capped * 255.0);
let result = Number(scaled).toString(16);
while (2 > result.length) {
result = '0' + result;
}
return result;
};
return '#' + channelToHex(sRGB[0]) + channelToHex(sRGB[1])
+ channelToHex(sRGB[2]);
}
window.addEventListener('load', main);
</script>
</head>
<body>
<div id="color-wheel"></div>
</body>
</html>
|