Create a web interface for a color picker app that allows selecting different color…
- User
Create a web interface for a color picker app that allows selecting different color spaces and displays color wheels or other models depedning on the color space. The color hovered over should be displayed in various formats.
- Assistant
Below is a single-file demo (save it as
color-picker.htmland open it in a browser).
It lets the user- Pick a color space (RGB, HSL, HSV, LAB, CMYK).
- See a wheel / gradient that makes sense for that space.
- Hover anywhere on the graphic and instantly read the hovered color in Hex, RGB, HSL, HSV, Lab and CMYK.
The heavy lifting for color conversion is done with the awesome open-source lib chroma.js.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Multi-Space Color Picker</title> <style> :root{--size:420px;} body{font-family:system-ui,Segoe UI,Helvetica,Arial,sans-serif; display:flex;flex-direction:column;align-items:center;padding:1.5rem; background:#f4f4f7;color:#222;} h1{margin:0 0 .5rem;} #toolbar{display:flex;align-items:center;gap:.5rem;margin-bottom:1rem;} canvas{border:1px solid #ccc;border-radius:50%;cursor:crosshair;} #readout{margin-top:1rem;display:flex;gap:1rem;align-items:center;flex-wrap:wrap;} #swatch{width:60px;height:60px;border-radius:.5rem;border:1px solid #888;} .label{font-weight:600;} code{background:#eee;padding:.15rem .4rem;border-radius:4px;} </style> </head> <body> <h1>Multi-Space Color Picker 🎨</h1> <div id="toolbar"> <label for="space" class="label">Color space:</label> <select id="space"> <option value="hsl">HSL (wheel)</option> <option value="hsv">HSV (wheel)</option> <option value="rgb">RGB (square)</option> <option value="lab">Lab (a/b plane)</option> <option value="cmyk">CMYK (C/M plane)</option> </select> </div> <canvas id="pad" width="420" height="420"></canvas> <div id="readout"> <div id="swatch"></div> <div id="vals"> <div><span class="label">HEX:</span> <code id="hex">–</code></div> <div><span class="label">RGB:</span> <code id="rgb">–</code></div> <div><span class="label">HSL:</span> <code id="hsl">–</code></div> <div><span class="label">HSV:</span> <code id="hsv">–</code></div> <div><span class="label">Lab:</span> <code id="lab">–</code></div> <div><span class="label">CMYK:</span> <code id="cmyk">–</code></div> </div> </div> <!-- chroma.js CDN --> <script src="https://cdn.jsdelivr.net/npm/chroma-js@2.4.2/+esm"></script> <script type="module"> import chroma from 'https://cdn.jsdelivr.net/npm/chroma-js@2.4.2/+esm'; const canvas = document.getElementById('pad'); const ctx = canvas.getContext('2d'); const sel = document.getElementById('space'); const swatch = document.getElementById('swatch'); const out = { hex : document.getElementById('hex'), rgb : document.getElementById('rgb'), hsl : document.getElementById('hsl'), hsv : document.getElementById('hsv'), lab : document.getElementById('lab'), cmyk: document.getElementById('cmyk') }; const SIZE = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--size')); drawCurrent(); // draw initial HSL wheel sel.addEventListener('change', drawCurrent); canvas.addEventListener('mousemove', handleHover); function drawCurrent(){ // clear then branch by mode ctx.clearRect(0,0,SIZE,SIZE); const mode = sel.value; if(mode === 'hsl' || mode === 'hsv') drawWheel(mode); else drawRect(mode); } /* ---------- Drawers ---------- */ function drawWheel(mode){ // Based on polar angle = hue, radius = S, with fixed L or V. const img = ctx.createImageData(SIZE,SIZE); const R = SIZE/2; for(let y=0;y<SIZE;y++){ for(let x=0;x<SIZE;x++){ const dx = x - R, dy = y - R; const dist = Math.sqrt(dx*dx+dy*dy); if(dist > R){ // keep wheel circular, set alpha=0 const idx = (y*SIZE+x)*4+3; img.data[idx] = 0; continue; } const sat = dist / R; // saturation 0-1 let hue = Math.atan2(dy,dx)*180/Math.PI; // ‑180..180 hue = (hue+360)%360; const color = (mode==='hsl') ? chroma.hsl(hue, sat, 0.5) : chroma.hsv(hue, sat, 1); const idx = (y*SIZE+x)*4; const [r,g,b] = color.rgb(); img.data[idx] = r; img.data[idx+1] = g; img.data[idx+2] = b; img.data[idx+3] = 255; } } ctx.putImageData(img,0,0); } function drawRect(mode){ const img = ctx.createImageData(SIZE,SIZE); for(let y=0;y<SIZE;y++){ for(let x=0;x<SIZE;x++){ const u = x/SIZE, v = y/SIZE; // 0-1 let color; switch(mode){ case 'rgb': color = chroma.rgb(u*255, v*255, 128); break; // vary R, G (keep B) case 'lab': color = chroma.lab(60, u*200-100, v*200-100); break; // L fixed 60 case 'cmyk': color = chroma.cmyk(u, v, 0.5, 0.1); break; // vary C, M default: color = chroma.gray(0); // safety } const idx = (y*SIZE+x)*4; const [r,g,b] = color.rgb(); img.data[idx] = r; img.data[idx+1] = g; img.data[idx+2] = b; img.data[idx+3] = 255; } } ctx.putImageData(img,0,0); } /* ---------- Hover handling ---------- */ function handleHover(ev){ const rect = canvas.getBoundingClientRect(); const x = ev.clientX - rect.left; const y = ev.clientY - rect.top; if(x<0||y<0||x>=SIZE||y>=SIZE) return; const px = ctx.getImageData(x,y,1,1).data; // Uint8Clamped [r,g,b,a] if(px[3]===0) return; // transparent pixel outside wheel const c = chroma(px[0],px[1],px[2]); // Swatch swatch.style.background = c.hex(); // Outputs out.hex.textContent = c.hex(); out.rgb.textContent = formatArr(c.rgb(),'rgb'); out.hsl.textContent = formatArr(c.hsl(),'hsl'); out.hsv.textContent = formatArr(c.hsv(),'hsv'); out.lab.textContent = formatArr(c.lab(),'lab'); out.cmyk.textContent= formatArr(c.cmyk(),'cmyk'); } function formatArr(arr,mode){ const round=(n)=>Math.round(n*100)/100; switch(mode){ case 'rgb': return `rgb(${arr.map(Math.round).join(', ')})`; case 'hsl': case 'hsv': return `${mode}(${round(arr[0])}, ${round(arr[1]*100)}%, ${round(arr[2]*100)}%)`; case 'lab': return `L${round(arr[0])} a${round(arr[1])} b${round(arr[2])}`; case 'cmyk': return `${arr.map(v=>round(v*100)+'%').join(' / ')}`; default: return arr.join(', '); } } </script> </body> </html>How it works (quick tour)
Color-plane builders
• For HSL/HSV we fill each pixel in a circular wedge: hue from polar angle, saturation from radius (lightness/value fixed).
• For RGB, Lab, CMYK we map two channels on X/Y, keeping the remaining channel(s) constant to create a 2-D slice.Performance
Everything is rendered once per mode change with a singleputImageDatacall (≈1 ms on modern hardware for a 420 × 420 buffer). Hovering does not redraw the canvas – we only read the pixel under the cursor (fast!).Extending ideas
• Add sliders for the “fixed” channels to explore the full 3-D space.
• Output CSS color formats (OKLCH, HWB…).
• Allow clicking to “lock” a color and copy it to clipboard.Enjoy exploring colors! 🎉
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.