-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
513 lines (453 loc) · 17.2 KB
/
Copy pathindex.html
File metadata and controls
513 lines (453 loc) · 17.2 KB
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hcg-color-picker Demo - Chrome-Style JavaScript Color Picker</title>
<meta name="description" content="Live demo of hcg-color-picker, a lightweight vanilla JavaScript color picker with Chrome-style UI, alpha channel, HEX/RGB/HSL inputs, EyeDropper API, copy to clipboard, and programmatic API.">
<link rel="canonical" href="https://www.html-code-generator.com/javascript/chrome-style-color-picker">
<meta property="og:type" content="website">
<meta property="og:title" content="hcg-color-picker Demo - Chrome-Style JavaScript Color Picker">
<meta property="og:description" content="Interactive demos: alpha channel, debounced callbacks, scroll positioning, programmatic setColor/open/close, and multiple pickers sharing one popup.">
<meta property="og:url" content="https://www.html-code-generator.com/javascript/chrome-style-color-picker">
<link rel="stylesheet" href="hcg-color.css">
<style>
*, *::before, *::after { box-sizing: border-box; }
body {
font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
background: #fff;
color: #1f2430;
margin: 0;
padding: 48px 24px 64px;
min-height: 100vh;
}
.wrap { max-width: 720px; margin: 0 auto; }
h1 {
font-size: 1.65rem;
font-weight: 700;
color: #111827;
margin-bottom: 10px;
line-height: 1.3;
}
.subtitle {
font-size: .95rem;
color: #4b5563;
line-height: 1.7;
margin-bottom: 36px;
}
.subtitle code {
background: #f3f4f6;
border-radius: 4px;
padding: 1px 6px;
font-size: .82rem;
color: #4338ca;
}
.subtitle a {
color: #4338ca;
font-weight: 600;
text-decoration: none;
}
.subtitle a:hover { text-decoration: underline; }
.demo-section { margin-bottom: 32px; }
.demo-section > h2 {
font-size: 1.05rem;
font-weight: 700;
color: #111827;
margin-bottom: 6px;
}
.demo-desc {
font-size: .86rem;
color: #6b7280;
line-height: 1.6;
margin-bottom: 14px;
}
.panel {
background: #fff;
border: 1px solid #e5e7eb;
border-radius: 14px;
padding: 20px;
margin-bottom: 12px;
}
.panel-label {
display: block;
font-size: .72rem;
letter-spacing: .08em;
text-transform: uppercase;
color: #9ca3af;
font-weight: 600;
margin-bottom: 12px;
}
.field-label {
display: block;
font-size: .88rem;
font-weight: 600;
color: #374151;
margin-bottom: 8px;
}
.picker-row {
display: flex;
align-items: center;
gap: 14px;
flex-wrap: wrap;
}
.color-output {
font-family: "Fira Code", "Cascadia Code", ui-monospace, monospace;
font-size: .82rem;
color: #374151;
background: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 8px;
padding: 8px 12px;
min-width: 180px;
}
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.demo-btn {
background: #fff;
border: 1px solid #d1d5db;
border-radius: 8px;
padding: 8px 16px;
color: #4b5563;
font-size: .82rem;
font-weight: 600;
cursor: pointer;
}
.demo-btn:hover { border-color: #9ca3af; color: #1f2937; background: #f9fafb; }
.output-box {
margin-top: 12px;
background: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 10px;
padding: 12px 14px;
font-family: "Fira Code", "Cascadia Code", ui-monospace, monospace;
font-size: .78rem;
color: #374151;
line-height: 1.6;
}
.scroll-panel {
height: 160px;
overflow-y: auto;
border: 1px dashed #d1d5db;
border-radius: 10px;
padding: 16px;
background: #fafafa;
}
.scroll-panel .picker-row { margin: 48px 0 80px; }
.event-log {
background: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 10px;
padding: 12px 14px;
font-family: "Fira Code", "Cascadia Code", ui-monospace, monospace;
font-size: .74rem;
color: #374151;
height: 140px;
overflow-y: auto;
line-height: 1.55;
}
.event-log div + div { margin-top: 2px; }
.demo-code {
margin-top: 4px;
border: 1px solid #e5e7eb;
border-radius: 10px;
background: #f9fafb;
overflow: hidden;
}
.demo-code summary {
padding: 10px 14px;
font-size: .82rem;
font-weight: 600;
color: #4338ca;
cursor: pointer;
user-select: none;
list-style: none;
}
.demo-code summary::-webkit-details-marker { display: none; }
.demo-code summary::before {
content: '▸ ';
display: inline-block;
transition: transform .15s;
}
.demo-code[open] summary::before { transform: rotate(90deg); }
.demo-code pre {
margin: 0;
padding: 0 14px 14px;
overflow-x: auto;
}
.demo-code code {
display: block;
font-family: "Fira Code", "Cascadia Code", ui-monospace, monospace;
font-size: .76rem;
line-height: 1.65;
color: #374151;
white-space: pre;
}
</style>
</head>
<body>
<div class="wrap">
<h1>hcg-color-picker - Live Demo</h1>
<p class="subtitle">
Interactive demos of <code>hcg-color-picker</code>, a lightweight zero-dependency
Chrome-style color picker for vanilla JavaScript. Try alpha channel, debounced
callbacks, scroll-aware positioning, programmatic control, and multiple triggers
sharing one popup.
<a href="https://www.html-code-generator.com/javascript/chrome-style-color-picker">hcg-color-picker - Full documentation</a>
</p>
<section class="demo-section" id="sec-basic">
<h2>1. Basic picker with alpha</h2>
<p class="demo-desc">Click the swatch to open the picker. Switch between HEX, RGB, and HSL inputs inside the panel. Use the eye dropper (Chrome/Edge) or hover the preview to copy.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<label class="field-label" for="picker-basic">Brand color</label>
<div class="picker-row">
<button id="picker-basic" type="button" data-color="#4338ca"></button>
<span class="color-output" id="out-basic">#4338caff</span>
</div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<pre><code><button id="picker" type="button" data-color="#4338ca"></button>
const picker = new hcgColor(document.getElementById("picker"), {
color: "#4338ca",
onChange: (colors) => console.log(colors.hexa)
});</code></pre>
</details>
</section>
<section class="demo-section" id="sec-no-alpha">
<h2>2. Without alpha channel</h2>
<p class="demo-desc">Pass <code>alpha: false</code> to hide the alpha slider and force opaque 6-digit hex output.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<label class="field-label" for="picker-no-alpha">Solid fill</label>
<div class="picker-row">
<button id="picker-no-alpha" type="button" data-color="#09b94f"></button>
<span class="color-output" id="out-no-alpha">#09b94f</span>
</div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<pre><code>new hcgColor(document.getElementById("picker-no-alpha"), {
color: "#09b94f",
alpha: false,
onChange: (colors) => console.log(colors.hex)
});</code></pre>
</details>
</section>
<section class="demo-section" id="sec-debounce">
<h2>3. Debounced change callback</h2>
<p class="demo-desc">Set <code>debounce</code> (milliseconds) to limit how often <code>onChange</code> fires while dragging sliders.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<label class="field-label" for="picker-debounce">Accent (150 ms debounce)</label>
<div class="picker-row">
<button id="picker-debounce" type="button" data-color="#e11d48"></button>
<span class="color-output" id="out-debounce">#e11d48ff</span>
</div>
<div class="output-box" id="debounce-count">Change events fired: 0</div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<pre><code>new hcgColor(document.getElementById("picker-debounce"), {
color: "#e11d48",
debounce: 150,
onChange: (colors, source) => {
console.log("debounced:", colors.hexa, source);
}
});</code></pre>
</details>
</section>
<section class="demo-section" id="sec-scroll">
<h2>4. Inside a scroll container</h2>
<p class="demo-desc">The popup repositions on scroll and flips above the trigger when there is not enough room below.</p>
<div class="panel">
<span class="panel-label">Try it — scroll this box first</span>
<div class="scroll-panel">
<p style="font-size:.84rem;color:#6b7280;margin:0 0 8px;">Scroll down, then open the picker to test positioning.</p>
<div class="picker-row">
<button id="picker-scroll" type="button" data-color="#2563eb"></button>
<span class="color-output" id="out-scroll">#2563ebff</span>
</div>
</div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<pre><code><!-- Works inside overflow containers; repositions on scroll -->
new hcgColor(document.getElementById("picker-scroll"), { color: "#2563eb" });</code></pre>
</details>
</section>
<section class="demo-section" id="sec-api">
<h2>5. Programmatic API</h2>
<p class="demo-desc">Control the picker with <code>open()</code>, <code>close()</code>, <code>setColor()</code>, <code>getColor()</code>, and <code>disable()</code> / <code>enable()</code>.</p>
<div class="panel">
<span class="panel-label">Controls</span>
<label class="field-label" for="picker-api">Theme color</label>
<div class="picker-row">
<button id="picker-api" type="button" data-color="#7c3aed"></button>
<span class="color-output" id="out-api">#7c3aedff</span>
</div>
<div class="btn-row">
<button class="demo-btn" type="button" id="btn-open">Open</button>
<button class="demo-btn" type="button" id="btn-close">Close</button>
<button class="demo-btn" type="button" id="btn-red">Set red</button>
<button class="demo-btn" type="button" id="btn-teal">Set teal</button>
<button class="demo-btn" type="button" id="btn-silent">Set silent</button>
<button class="demo-btn" type="button" id="btn-toggle">Disable / Enable</button>
</div>
<div class="output-box" id="api-output">isOpen: false</div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<pre><code>const api = new hcgColor(document.getElementById("picker-api"), { color: "#7c3aed" });
api.open();
api.close();
api.setColor("#ff0000");
api.setColor("#ff0000", true); // silent: no change event
const colors = api.getColor();
api.disable();
api.enable();</code></pre>
</details>
</section>
<section class="demo-section" id="sec-events">
<h2>6. Open, close, and change events</h2>
<p class="demo-desc">Subscribe with <code>.on("change")</code>, <code>.on("open")</code>, and <code>.on("close")</code>. The change callback receives a color object and a <code>source</code> string.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<label class="field-label" for="picker-events">Event source demo</label>
<div class="picker-row">
<button id="picker-events" type="button" data-color="#d97706"></button>
<span class="color-output" id="out-events">#d97706ff</span>
</div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<pre><code>const picker = new hcgColor(document.getElementById("picker-events"), { color: "#d97706" });
picker.on("open", () => console.log("opened"));
picker.on("close", () => console.log("closed"));
picker.on("change", (colors, source) => {
console.log(colors.hexa, source); // drag | input | api | eyedropper
});</code></pre>
</details>
</section>
<section class="demo-section" id="sec-multiple">
<h2>7. Multiple pickers, one popup</h2>
<p class="demo-desc">Every instance shares a single floating panel for a smaller DOM footprint. Only one picker can be open at a time.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<div class="picker-row">
<button id="picker-m1" type="button" data-color="#ef4444" title="Red"></button>
<button id="picker-m2" type="button" data-color="#22c55e" title="Green"></button>
<button id="picker-m3" type="button" data-color="#3b82f6" title="Blue"></button>
<span class="color-output" id="out-multiple">Last changed: —</span>
</div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<pre><code>new hcgColor(document.getElementById("picker-m1"), { color: "#ef4444", onChange: onAnyChange });
new hcgColor(document.getElementById("picker-m2"), { color: "#22c55e", onChange: onAnyChange });
new hcgColor(document.getElementById("picker-m3"), { color: "#3b82f6", onChange: onAnyChange });</code></pre>
</details>
</section>
<section class="demo-section" id="sec-log">
<h2>8. Event log</h2>
<p class="demo-desc">All demos above write to this log so you can see debouncing, open/close, and color values in real time.</p>
<div class="panel">
<span class="panel-label">Live output</span>
<div id="log" class="event-log" aria-live="polite"></div>
<div class="btn-row">
<button class="demo-btn" type="button" id="btn-clear-log">Clear log</button>
</div>
</div>
</section>
</div>
<script src="hcg-color.js"></script>
<script>
const logEl = document.getElementById("log");
let debounceEvents = 0;
function log(msg) {
const div = document.createElement("div");
div.textContent = "[" + new Date().toLocaleTimeString() + "] " + msg;
logEl.insertBefore(div, logEl.firstChild);
if (logEl.children.length > 80) logEl.removeChild(logEl.lastChild);
}
function formatColors(colors) {
return colors.hexa + " " + colors.rgba + " " + colors.hsla;
}
function bindOutput(picker, outId, label) {
picker.on("change", (colors, source) => {
document.getElementById(outId).textContent = picker._alphaEnabled ? colors.hexa : colors.hex;
log(label + " changed (" + (source || "change") + "): " + formatColors(colors));
});
picker.on("open", () => log(label + " opened"));
picker.on("close", () => log(label + " closed"));
}
const basic = new hcgColor(document.getElementById("picker-basic"), { color: "#4338ca" });
bindOutput(basic, "out-basic", "Basic");
const noAlpha = new hcgColor(document.getElementById("picker-no-alpha"), {
color: "#09b94f",
alpha: false
});
bindOutput(noAlpha, "out-no-alpha", "No-alpha");
const debounced = new hcgColor(document.getElementById("picker-debounce"), {
color: "#e11d48",
debounce: 150,
onChange: () => {
debounceEvents += 1;
document.getElementById("debounce-count").textContent = "Change events fired: " + debounceEvents;
}
});
bindOutput(debounced, "out-debounce", "Debounced");
const scrollPicker = new hcgColor(document.getElementById("picker-scroll"), { color: "#2563eb" });
bindOutput(scrollPicker, "out-scroll", "Scroll");
const apiPicker = new hcgColor(document.getElementById("picker-api"), { color: "#7c3aed" });
bindOutput(apiPicker, "out-api", "API");
let apiDisabled = false;
function refreshApiOutput() {
document.getElementById("api-output").textContent = "isOpen: " + apiPicker.isOpen;
}
document.getElementById("btn-open").addEventListener("click", () => {
apiPicker.open();
refreshApiOutput();
});
document.getElementById("btn-close").addEventListener("click", () => {
apiPicker.close();
refreshApiOutput();
});
document.getElementById("btn-red").addEventListener("click", () => apiPicker.setColor("#dc2626"));
document.getElementById("btn-teal").addEventListener("click", () => apiPicker.setColor("#0d9488"));
document.getElementById("btn-silent").addEventListener("click", () => {
apiPicker.setColor("#f59e0b", true);
log("API picker set silent (no change event)");
});
document.getElementById("btn-toggle").addEventListener("click", () => {
apiDisabled = !apiDisabled;
if (apiDisabled) apiPicker.disable();
else apiPicker.enable();
log("API picker " + (apiDisabled ? "disabled" : "enabled"));
});
apiPicker.on("open", refreshApiOutput);
apiPicker.on("close", refreshApiOutput);
const eventsPicker = new hcgColor(document.getElementById("picker-events"), { color: "#d97706" });
bindOutput(eventsPicker, "out-events", "Events");
function onMultipleChange(colors) {
document.getElementById("out-multiple").textContent = "Last changed: " + colors.hexa;
}
new hcgColor(document.getElementById("picker-m1"), {
color: "#ef4444",
onChange: (colors) => { onMultipleChange(colors); log("Multi red: " + colors.hexa); }
});
new hcgColor(document.getElementById("picker-m2"), {
color: "#22c55e",
onChange: (colors) => { onMultipleChange(colors); log("Multi green: " + colors.hexa); }
});
new hcgColor(document.getElementById("picker-m3"), {
color: "#3b82f6",
onChange: (colors) => { onMultipleChange(colors); log("Multi blue: " + colors.hexa); }
});
document.getElementById("btn-clear-log").addEventListener("click", () => {
logEl.innerHTML = "";
log("Log cleared");
});
log("Demo ready — open any picker above.");
</script>
</body>
</html>