Ejemplos
Ejemplos prácticos. Demos en vivo.
Elige un ejemplo para abrirlo a pantalla completa, con la demo en vivo y su código fuente lado a lado.
Elige un ejemplo para abrirlo a pantalla completa, con la demo en vivo y su código fuente lado a lado.
Elige el tono por ángulo y la saturación por distancia. La rueda se arma con noventa cuñas, un gradiente radial y un disco oscuro.
import 'nuclo' type Swatch = { hue: number; sat: number; val: number; hex: string } const R = 90 // the radius of the wheel let hue = 18 // the angle, 0..360 let sat = 0.9 // the distance from the center, 0..1 let val = 1 // the brightness slider, 0..1 let saved: Swatch[] = [] // HSV to hex, one channel per n. const hex = (h: number, s: number, v: number) => '#' + [5, 3, 1] .map((n) => { const k = (n + h / 60) % 6 const c = v - v * s * Math.max(0, Math.min(k, 4 - k, 1)) return Math.round(c * 255).toString(16).padStart(2, '0') }) .join('') const current = () => hex(hue, sat, val) const rad = (deg: number) => (deg * Math.PI) / 180 const at = (deg: number) => `${100 + R * Math.cos(rad(deg))},${100 + R * Math.sin(rad(deg))}` // The pointer's angle is the hue, its distance the saturation. function pick(e: PointerEvent) { if (!e.buttons) return const box = (e.currentTarget as Element).getBoundingClientRect() const x = ((e.clientX - box.left) / box.width) * 200 - 100 const y = ((e.clientY - box.top) / box.height) * 200 - 100 hue = Math.round(((Math.atan2(y, x) * 180) / Math.PI + 360) % 360) sat = Math.min(1, Math.hypot(x, y) / R) update() } function save() { const c = current() saved = [{ hue, sat, val, hex: c }, ...saved.filter((s) => s.hex !== c)].slice(0, 8) update() } // 90 wedges of hue, a radial gradient from white on top for the // saturation, and a black disc whose opacity is the darkness. export const ColorWheel = () => div( svgSvg( { viewBox: '0 0 200 200', width: '200', height: '200', style: { touchAction: 'none' }, onPointerDown: (e) => { e.currentTarget.setPointerCapture(e.pointerId) pick(e) }, onPointerMove: pick, }, defsSvg( radialGradientSvg( { id: 'white' }, stopSvg({ offset: '0', 'stop-color': 'white' }), stopSvg({ offset: '1', 'stop-color': 'white', 'stop-opacity': 0 }), ), ), ...Array.from({ length: 90 }, (_, i) => pathSvg({ d: `M100,100 L${at(i * 4)} A${R},${R} 0 0 1 ${at(i * 4 + 4.5)} Z`, fill: `hsl(${i * 4 + 2}, 100%, 50%)`, }), ), circleSvg({ cx: 100, cy: 100, r: R, fill: 'url(#white)' }), circleSvg({ cx: 100, cy: 100, r: R, fill: 'black', 'fill-opacity': () => 1 - val, }), circleSvg({ r: 9, fill: current, stroke: 'white', 'stroke-width': 3, cx: () => 100 + sat * R * Math.cos(rad(hue)), cy: () => 100 + sat * R * Math.sin(rad(hue)), }), ), label( span('Bright'), input({ type: 'range', min: '0', max: '100', step: '1', value: () => String(Math.round(val * 100)), onInput: (e) => { val = Number(e.currentTarget.value) / 100; update() }, }), span(() => String(Math.round(val * 100))), ), div( { style: { display: 'flex', alignItems: 'center', gap: '8px' } }, div({ style: () => ({ width: '40px', height: '40px', borderRadius: '10px', border: '1px solid #ccc', background: current(), }), }), span(current), button('Save', { onClick: save }), ), div( { style: { display: 'flex', flexWrap: 'wrap', gap: '6px', minHeight: '26px' } }, list(() => saved, (s) => button({ title: s.hex, 'aria-label': 'Use ' + s.hex, style: { width: '26px', height: '26px', padding: '0', borderRadius: '7px', background: s.hex }, onClick: () => { hue = s.hue; sat = s.sat; val = s.val; update() }, }), ), when(() => !saved.length, span('saved colors land here')), ), )