-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
465 lines (422 loc) · 17.3 KB
/
index.html
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue Color Chooser</title>
<!--<script src="https://unpkg.com/vue"></script>-->
<!--<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>-->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
</head>
<body>
<div id="colorChooser">
<h1>Vue Color Chooser {{vueRelease}}</h1>
<!--BASE COLOR CHOOSER:-->
<b>Click to lock Base Color:</b>
<span :style="'color: white; background-color: rgb(' + baseColor.red + ','
+ baseColor.grn + ','
+ baseColor.blu + ');'">
{{baseColor.red}} {{baseColor.grn}} {{baseColor.blu}}</span>
<div id="baseColors">
<div class="base-el"
v-for="props in baseProps"
:class="{selected:props === baseColor}"
v-on:mouseover="baseHover(props)"
v-on:click="baseClick(props)"
:style="'color: white; background-color: rgb(' + props.red + ','
+ props.grn + ','
+ props.blu + ');'">
R:{{props.red}}<br>G:{{props.grn}}<br>B:{{props.blu}}
</div>
</div>
<br>
<!--FINAL COLOR CHOOSER:-->
<b>Click to lock Final Color:</b>
<span :style="'color: ' + fgColor(readoutColor.red, readoutColor.grn, readoutColor.blu) +
'; background-color: rgb(' + readoutColor.red + ','
+ readoutColor.grn + ','
+ readoutColor.blu + ');'">
{{readoutColor.red}} {{readoutColor.grn}} {{readoutColor.blu}}</span>
<br>
<div id="finalColors">
<div class="final-el"
v-for="props in finalProps"
:class="{selected:props === finalColor}"
v-on:mouseover="finalHover(props)"
v-on:click="finalClick(props)"
:style="'color: ' + fgColor(props.red, props.grn, props.blu)
+ '; background-color: rgb(' + props.red + ',' + props.grn + ',' + props.blu + ');'"
v-html="'R:' + props.red + '<br>G:' + props.grn + '<br>B:' + props.blu">
</div>
</div>
<!--FINAL READOUT AND CLIPBOARD-COPY BUTTONS:-->
<span id="readout-grid">
<span class="info"><b>Final<br>Color:</b></span>
<span :style="'color: ' + fgColor(readoutColor.red, readoutColor.grn, readoutColor.blu)
+ '; padding: 12px; border: 3px solid gray; background-color: rgb('
+ readoutColor.red + ',' + readoutColor.grn + ',' + readoutColor.blu + ');'"
v-html="'R:' + readoutColor.red + '<br>G:' + readoutColor.grn + '<br>B:' + readoutColor.blu">
</span>
<span></span>
<span></span>
<span></span>
<button v-on:click="copyToClip(readoutColor.red,',',readoutColor.grn,',',readoutColor.blu)">
Copy <b>{{readoutColor.red}},{{readoutColor.grn}},{{readoutColor.blu}}</b> to clipboard</button>
<span class="info" v-if="clipCopied"><b>{{clipCopied}}</b> copied to clipboard.</span>
<span class="info" v-else><br><br><br></span>
<button v-on:click="copyToClip('#',readoutHex.red,readoutHex.grn,readoutHex.blu)">
Copy <b>#{{readoutHex.red}}{{readoutHex.grn}}{{readoutHex.blu}}</b> to clipboard</button>
<span class=info v-if="framesPerSec">Average frames/sec:
<b>{{framesPerSec}}@{{finalCols}}x{{finalCols}}</b></span>
<span class=info v-else><br><br><br></span>
<button v-on:click="toggleDemo()">
Demo {{demoButton}}</button>
</span>
</div>
<script>
"use strict";
const release = "3.0"; // "Semantic" program version for end users
document.title = "Vue Color Chooser " + release;
// Toggles:
let baseLock = false;
let finalLock = false;
// Demo mode:
let demoSecs = 0; // For frames/sec readout
let frameCount = 0;
let doDemo = false;
let demoRefreshing = false;
let demoBaseArray = []; // Copy of baseArray For "demo" mode operation
let demoSpeed = 10; // ms delay during demo cycles
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('demo')) {
if (urlParams.get('demo') === 'true')
doDemo = true;
}
///// Set up responsive sizing of all elements by executing our CSS via JavaScript:
// todo: unstyled page flashes at startup.
const gridCellSize = 45;
const bodyMargin = 40;
let baseColSpec;
let finalColSpec;
setMySize();
window.addEventListener('resize', setMySize);
function setMySize() {
const myWidth = document.documentElement.clientWidth - bodyMargin * 2; // account for margins
const myHeight = document.documentElement.clientHeight - bodyMargin * 2; // margins + other elements
// "final" grid dimensions:
const finalWidth = myWidth - 145; // Allow for readout grid
const finalHeight = myHeight - 185; // Allow for base grid
const finalMinDim = finalWidth < finalHeight ? finalWidth : finalHeight;
baseColSpec = Math.round(myWidth / gridCellSize);
finalColSpec = Math.round(finalMinDim / gridCellSize);
if (finalColSpec < 2) finalColSpec = 2;
demoSecs = 0;
frameCount = 0;
/// Write style sheet here so we can share its variables with JavaScript:
let styleEl = document.getElementById("ccStyles"); // Avoid appending multiple <style>s
if (styleEl)
styleEl.parentNode.removeChild(styleEl);
const ccStyleSheet = document.createElement('style');
ccStyleSheet.id = "ccStyles";
ccStyleSheet.innerHTML = `
:root {
font-family: sans-serif;
--bgcolor: lightgray;
background-color: var(--bgcolor);
}
body {
margin: ${bodyMargin}px;
}
h1 {
text-align: center;
}
#baseColors {
display: grid;
width: fit-content;
grid-template-columns: repeat(${baseColSpec}, ${gridCellSize}px);
}
#finalColors {
display: inline-grid;
width: fit-content;
grid-template-columns: repeat(${finalColSpec}, ${gridCellSize}px); /* best w/odd number */
}
#readout-grid {
display: inline-grid;
grid-template-columns: ${gridCellSize * 2}px ${gridCellSize * 2}px;
max-width: ${gridCellSize * 2}px; /* scroll screen instead of wrapping beneath elements to my left */
}
.base-el, .final-el {
border-color: var(--bgcolor);
border-style: solid;
border-width: 2px;
}
.base-el, .final-el, .selected {
font-size: ${gridCellSize / 4}px;
line-height: ${gridCellSize / 4}px;
/*border-radius: ${gridCellSize / 8}px; causes lockups (performance) */
height: ${gridCellSize / 1.1}px;
display: block;
cursor: pointer;
padding-left: ${gridCellSize / 20}px;
padding-top: ${gridCellSize / 20}px;
}
.info {
font-size: ${gridCellSize / 3.5}px;
text-align: right;
padding: 6px;
}
@keyframes blink {
0% {
border: 2px inset white;
}
100% {
border: 2px outset white;
}
}
/* Highlight a selected cell */
.selected {
animation: blink .6s step-start infinite alternate;
// animation-timing-function: ease-in-out;
animation-timing-function: ease;
/*todo: causes lockup w/Chrome ("mousehandler timeout?") */
/*transform: skewY(-20deg);*/
}
button {
height: ${gridCellSize * 2}px;
width: ${gridCellSize * 2}px;
font-size: ${gridCellSize / 3.5}px;
line-height: ${gridCellSize / 3.5}px;
align-self: center;
cursor: pointer;
border: 3px solid gray;
}
button:hover,
button:focus {
background: white;
}
button:active {
transform: translate(4px, 4px);
}
`;
document.head.appendChild(ccStyleSheet);
}
new Vue({
el: '#colorChooser',
mounted() {
window.addEventListener('resize', this.onResize);
if (doDemo) {
this.demoButton = "OFF";
this.clockDemo();
this.runDemo(); // Kick off the demo
} else {
this.demoButton = "ON";
}
},
data: {
vueRelease: release,
// Initial user choice:
baseColor: {red: 127, grn: 127, blu: 127},
// Final user choice:
finalColor: {red: 127, grn: 127, blu: 127},
// Readouts:
readoutColor: {red: 127, grn: 127, blu: 127},
readoutHex: {red: "7f", grn: "7f", blu: "7f"},
// Selections (control CSS highlighting):
baseSelection: undefined,
finalSelection: undefined,
// Used in "copy to clipboard" logic:
clipCopied: undefined,
demoButton: undefined,
// Stateful in case of resize event:
baseCols: baseColSpec,
finalCols: finalColSpec,
finalArray: [], // stateful so we can watch it during demo mode
framesPerSec: undefined,
},
watch: {
// Change to finalArray triggers change to final selection during demo:
finalArray() {
if (doDemo) {
// console.log(`finalArray watched.`);
let nextFinalEl;
const finalCellCt = this.finalCols * this.finalCols;
do
nextFinalEl = this.finalArray[Math.trunc(Math.random() * finalCellCt)];
while (nextFinalEl === this.finalColor); // Force new random location
// todo: D.R.Y.:
this.readoutColor = nextFinalEl;
this.finalColor = nextFinalEl;
this.readoutHex.red = nextFinalEl.red.toString(16).padStart(2, "0");
this.readoutHex.grn = nextFinalEl.grn.toString(16).padStart(2, "0");
this.readoutHex.blu = nextFinalEl.blu.toString(16).padStart(2, "0");
frameCount++;
}
},
// Change to finalColor triggers change to finalArray during demo:
finalColor() {
doDemo && this.runDemo();
}
},
// The "base" grid contains a fixed set of initial colors to choose from.
// The "final" grid changes dynamically to colors adjacent to that of the
// currently selected base grid cell. So initialize the base grid with a
// spectrum of colors, and initialize the final grid with the coordinates
// needed to calculate its colors at finalCell(), below.
//
computed: {
///////////// CALCULATE BASE GRID COLORS and IDs:
baseProps: function () {
let baseArray = [];
let red = 255;
let grn = 255;
let blu = 255;
// Base rainbow grid. Math derived from https://krazydad.com/tutorials/makecolors.php:
const sinFreq = 6 / this.baseCols;
const greenPhase = 2 * Math.PI / 3;
const bluePhase = 4 * Math.PI / 3;
const sineWidth = 127;
const sineCtr = 128;
for (let j = 0; j < this.baseCols - 1; ++j) {
red = Math.round(Math.sin(sinFreq * j + 0) * sineWidth + sineCtr);
grn = Math.round(Math.sin(sinFreq * j + greenPhase) * sineWidth + sineCtr);
blu = Math.round(Math.sin(sinFreq * j + bluePhase) * sineWidth + sineCtr);
baseArray.push({red, grn, blu}); // NB: pushing an OBJECT
}
// Add gray square:
red = grn = blu = 127;
baseArray.push({red, grn, blu});
demoBaseArray = baseArray;
return baseArray;
},
///////////// CALCULATE FINAL COLOR GRID CELL COLORS & IDs:
finalProps: function () {
// Derive grid size from CSS:
this.finalArray = [];
let red;
let grn;
let blu;
let xOffset;
let yOffset;
const baseRed = +this.baseColor.red;
const baseGrn = +this.baseColor.grn;
const baseBlu = +this.baseColor.blu;
let xSpread = Math.round(136 / this.finalCols);
let ySpread = Math.round(154 / this.finalCols);
let periphCols = Math.trunc(this.finalCols / 2);
// red/grn/blu code adapted from Justin Meyer:
for (let c = 0, id = 0; c < this.finalCols; c++) {
for (let r = 0; r < this.finalCols; id++, r++) {
xOffset = periphCols - c; // How far am I from grid center?
yOffset = periphCols - r;
// Purely empirical way of getting a useful range:
red = baseRed + xOffset * xSpread + yOffset * ySpread;
grn = baseGrn + xOffset * xSpread + yOffset * ySpread;
blu = baseBlu + xOffset * xSpread + yOffset * ySpread;
red = red > 255 ? 255 : red < 1 ? 0 : red;
grn = grn > 255 ? 255 : grn < 1 ? 0 : grn;
blu = blu > 255 ? 255 : blu < 1 ? 0 : blu;
this.finalArray.push({red, grn, blu});
}
}
return this.finalArray;
}
},
methods: {
runDemo() {
// console.log(`runDemo called.`);
if (demoRefreshing) return;
demoRefreshing = true;
let nextBaseEl;
do
nextBaseEl = demoBaseArray[Math.trunc(Math.random() * baseColSpec)];
while (nextBaseEl === this.baseColor); // Force new random location
setTimeout(() => {
if (doDemo) { // Test flag here since it may have been set during timeout
this.baseColor = nextBaseEl;
}
demoRefreshing = false;
}, demoSpeed);
},
clockDemo() {
setTimeout(() => {
if (doDemo) {
this.framesPerSec = (frameCount / ++demoSecs).toFixed(2); // Seconds we have been running this demo
this.clockDemo();
}
}, 1000);
},
// Our window was resized, so trigger a render:
onResize(event) {
this.baseCols = baseColSpec;
this.finalCols = finalColSpec;
},
// Set selection to id and base color from background-color of a hovered/clicked cell:
setBaseRGB(color) {
if (doDemo) return;
this.baseColor = color;
this.finalSelection = undefined;
finalLock = false;
},
baseHover(color) {
if (!baseLock) this.setBaseRGB(color);
},
baseClick(color) {
baseLock = !baseLock;
this.setBaseRGB(color);
},
// Set selection to id and final color from background-color of a hovered/clicked cell:
setFinalRGB(color) {
if (doDemo) return;
this.readoutColor = color;
this.finalColor = color;
this.readoutHex.red = color.red.toString(16).padStart(2, "0");
this.readoutHex.grn = color.grn.toString(16).padStart(2, "0");
this.readoutHex.blu = color.blu.toString(16).padStart(2, "0");
},
finalHover(color) {
if (!finalLock) this.setFinalRGB(color);
},
finalClick(color) {
finalLock = !finalLock;
this.setFinalRGB(color);
},
fgColor(red, grn, blu) {
// if (+red + +grn + +blu > 400) { // '+' coerces strings to numbers
if (+red + +grn * 1.5 + +blu * .5 > 400) { // '+' coerces strings to numbers
return "black";
}
else
return "white";
},
// Copy argument(s) to end user system's clipboard:
copyToClip() {
let toCopy = "";
for (let i = 0; i < arguments.length; i++) {
toCopy += arguments[i];
}
// Note: this has the confusing side-effect of de-focusing any active button:
let textArea = document.createElement("textarea");
textArea.value = toCopy;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("Copy");
textArea.remove();
this.clipCopied = `${toCopy}`;
},
toggleDemo() {
doDemo = !doDemo;
if (doDemo) {
demoRefreshing = false;
this.demoButton = "OFF";
this.clockDemo();
this.runDemo(); // Kick off demo
} else {
this.demoButton = "ON";
this.frameCount = 0;
this.demoSecs = 0;
}
},
}
});
</script>
</body>
</html>