-
Notifications
You must be signed in to change notification settings - Fork 0
/
chooser3.js
435 lines (401 loc) · 15.1 KB
/
chooser3.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
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
"use strict";
// import {Component} from "//unpkg.com/can@5/core.min.mjs";
import {Component} from "//unpkg.com/can@5/core.mjs";
const release = "2.6"; // "Semantic" program version for end users
document.title = "CanJS Color Chooser " + release;
///// Set up responsive sizing of all elements by executing our CSS via JavaScript:
const gridCellSize = 45;
const bodyMargin = 40;
let baseColSpec;
let finalColSpec;
// 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;
}
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 from here so we can use its variables in our 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;
/*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);
}
Component.extend({
tag: "color-chooser",
view: `
<h1>CanJS Color Chooser ${release}</h1>
<div><b>Click to lock base color</b>
<span style="{{colorStyle(baseOrSuggestedColor)}}">
{{printShortColor(baseOrSuggestedColor)}}
</span>
</div>
<div id="baseColors">
{{#for(color of baseColors)}}
<span style="{{colorStyle(color)}}"
on:mouseenter="hoverBaseColor(color)"
on:click="clickBaseColor(color)"
{{#eq(color, suggestedBaseColor)}}class="selected"{{else}}class="base-el"{{/eq}}>
{{{printLongColor(color)}}}
</span>
{{/for}}
<br>
</div>
<div class='clear'>
<b>Click to lock final color</b>
<span style="{{colorStyle(finalOrBaseOrSuggestedColor)}}">
{{printShortColor(finalOrBaseOrSuggestedColor)}}
</span>
</div>
<div id="finalColors">
{{#for(color of finalColors)}}
<span style="{{colorStyle(color)}}"
on:mouseenter="hoverFinalColor(color)"
on:click="clickFinalColor(color)"
{{#eq(color, suggestedFinalColor)}}class="selected"{{else}}class="final-el"{{/eq}}>
{{{printLongColor(color)}}}
</span>
{{/for}}
</div>
<div id="readout-grid">
<span class="info"><b>Final<br>Color:</b></span>
<span style="{{colorStyle(finalOrBaseOrSuggestedColor)}} padding: 12px; border: 3px solid gray;">
{{{printLongColor(finalOrBaseOrSuggestedColor)}}}
</span>
<span></span>
<span></span>
<span></span>
<button on:click=copyToClip({{printShortColor(finalOrBaseOrSuggestedColor)}})>
Copy <br><b>{{printShortColor(finalOrBaseOrSuggestedColor)}}</b><br> to clipboard</button>
<span class="info">{{#if(clipCopied)}}<b>{{clipCopied}}</b> copied to clipboard.{{else}}<br><br><br>{{/if}}</span>
<!-- Quotes coerce non-numeric to string for copyToClip(): -->
<button on:click=copyToClip(\"{{printHexColor(finalOrBaseOrSuggestedColor)}}\")>
Copy <br><b>{{printHexColor(finalOrBaseOrSuggestedColor)}}</b><br> to clipboard</button>
<span></span>
<span></span>
<span class="info">{{#if(framesPerSec)}}Average frames/sec:
<b>{{framesPerSec}}@{{finalCols}}x{{finalCols}}</b>{{/if}}</span>
<button on:click="toggleDemo()">
Demo {{demoButton}}</button>
</div>
`,
ViewModel: {
// STATEFUL PROPS
suggestedBaseColor: "any",
baseColor: "any",
suggestedFinalColor: "any",
finalColor: "any",
clipCopied: {default: 0},
framesPerSec: {default: 0},
baseCols: {default: baseColSpec},
finalCols: {default: finalColSpec},
// finalArray: undefined, // not "watched" if declared as "undefined" (why?)
finalArray: "any", // stateful so we can watch it during demo mode
demoButton: {
default:
function () {
if (doDemo)
return "OFF";
else
return "ON";
}
},
connectedCallback() {
if (doDemo) {
demoRefreshing = false;
this.demoButton = "OFF";
this.clockDemo();
this.runDemo(); // Kick off demo
}
},
// DERIVED VALUES
get baseColors() {
// const sinFreq = .3;
const sinFreq = 6 / this.baseCols;
const greenPhase = 2 * Math.PI / 3;
const bluePhase = 4 * Math.PI / 3;
const sineWidth = 127;
const sineCtr = 128;
const colors = [];
for (let j = 0; j < this.baseCols - 1; ++j) {
var red = Math.round(Math.sin(sinFreq * j + 0) * sineWidth + sineCtr);
var grn = Math.round(Math.sin(sinFreq * j + greenPhase) * sineWidth + sineCtr);
var blu = Math.round(Math.sin(sinFreq * j + bluePhase) * sineWidth + sineCtr);
colors.push({red, grn, blu})
}
// Add gray square:
red = grn = blu = 127;
colors.push({red, grn, blu});
demoBaseArray = colors;
return colors;
},
get baseOrSuggestedColor() {
return this.baseColor || this.suggestedBaseColor || this.baseColors[this.baseCols - 1];
},
get finalOrBaseOrSuggestedColor() {
return this.finalColor || this.suggestedFinalColor || this.baseOrSuggestedColor;
},
get finalColors() {
let colorArray = [];
// this.finalArray = [];
let red;
let grn;
let blu;
let xOffset;
let yOffset;
let xSpread = Math.round(136 / this.finalCols);
let ySpread = Math.round(154 / this.finalCols);
let periphCols = Math.trunc(this.finalCols / 2);
for (let c = 0; c < this.finalCols; c++) {
for (let r = 0; r < this.finalCols; r++) {
xOffset = periphCols - c; // How far am I from grid center?
yOffset = periphCols - r;
// Purely empirical way of getting a useful range:
red = this.baseOrSuggestedColor.red + xOffset * xSpread + yOffset * ySpread;
grn = this.baseOrSuggestedColor.grn + xOffset * xSpread + yOffset * ySpread;
blu = this.baseOrSuggestedColor.blu + 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}) // causes performance issues (fix CanJS?)
colorArray.push({red, grn, blu})
}
}
// return this.finalArray;
this.finalArray = colorArray;
return colorArray;
},
// HELPER METHODS
colorStyle(color) {
if (color.red + color.grn * 1.5 + color.blu * .5 > 400) // '+' coerces strings to numbers
return `color: black; background-color: rgb(${color.red},${color.grn},${color.blu});`
else
return `color: white; background-color: rgb(${color.red},${color.grn},${color.blu});`
},
printShortColor(color) {
return `${color.red},${color.grn},${color.blu}`;
},
printHexColor(color) {
let redHex = color.red.toString(16).padStart(2, "0");
let grnHex = color.grn.toString(16).padStart(2, "0");
let bluHex = color.blu.toString(16).padStart(2, "0");
return `#${redHex}${grnHex}${bluHex}`;
},
printLongColor(color) {
return `R:${color.red}<br>G:${color.grn}<br>B:${color.blu}`
},
// METHODS THAT CHANGE STATE
hoverBaseColor(color) {
if (doDemo) return;
if (!this.baseColor)
this.suggestedBaseColor = color;
},
clickBaseColor(color) {
if (doDemo) return;
if (this.baseColor) {
this.baseColor = null;
this.suggestedBaseColor = color;
}
else {
this.baseColor = color;
// There are less imperative ways of doing this
this.suggestedFinalColor = null;
this.finalColor = null;
}
},
hoverFinalColor(color) {
if (doDemo) return;
if (!this.finalColor)
this.suggestedFinalColor = color;
},
clickFinalColor(color) {
if (doDemo) return;
if (this.finalColor) {
this.finalColor = null;
this.suggestedFinalColor = color;
}
else {
this.finalColor = color;
this.suggestedFinalColor = color;
}
},
// Copy argument(s) to end user system's clipboard:
copyToClip(...clipStrings) {
let textArea = document.createElement("textarea");
textArea.value = clipStrings;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("Copy");
textArea.remove();
this.clipCopied = `${clipStrings}`;
},
// Methods related to "demo" mode. Strategy is:
// - runDemo() changes suggestedBaseColor to a random selection
// - change to suggestedBaseColor triggers random change to suggestedFinalColor
// - change to suggestedFinalColor triggers runDemo() and the cycle repeats.
runDemo() {
let nextBaseEl;
do
nextBaseEl = demoBaseArray[Math.trunc(Math.random() * baseColSpec)];
while (nextBaseEl === this.suggestedBaseColor); // Force new random location
setTimeout(() => {
if (doDemo) { // Test flag here since it may have been cleared during timeout
this.suggestedBaseColor = nextBaseEl; // Trigger update of final grid
}
demoRefreshing = false;
}, demoSpeed);
},
clockDemo() {
setTimeout(() => {
if (doDemo) {
this.framesPerSec = (frameCount / ++demoSecs).toFixed(2); // Seconds we have been running this demo}
this.clockDemo();
}
}, 1000);
},
toggleDemo() {
doDemo = !doDemo;
if (doDemo) {
demoRefreshing = false;
this.demoButton = "OFF";
this.clockDemo();
this.runDemo(); // Kick off demo
} else {
this.demoButton = "ON";
frameCount = 0;
demoSecs = 0;
}
}
},
events: {
"{window} resize":
function () {
// console.log('resize event.');
this.viewModel.suggestedBaseColor = null;
this.viewModel.baseColor = null;
this.viewModel.suggestedFinalColor = null;
this.viewModel.finalColor = null;
this.viewModel.baseCols = baseColSpec;
this.viewModel.finalCols = finalColSpec;
},
"{viewModel} finalArray":
// finalArray update triggered by runDemo(); now select a color from it
function (viewModel, event, newValue) {
if (doDemo) {
// console.log("finalArray triggered demo update.");
let nextFinalEl;
const finalCellCt = this.viewModel.finalCols * this.viewModel.finalCols;
do
nextFinalEl = this.viewModel.finalArray[Math.trunc(Math.random() * finalCellCt)];
while (nextFinalEl === this.viewModel.finalColor); // Force new random location
this.viewModel.suggestedFinalColor = nextFinalEl;
frameCount++;
}
},
"{viewModel} suggestedFinalColor":
// suggestedFinalColor changed due to finalArray update (above); now restart runDemo()
function (viewModel, event, newValue) {
doDemo && this.viewModel.runDemo();
}
}
});