'use client' import { type JSX, useState, useEffect, useRef } from 'react' export type ColorPickerPopupProps = { value: string onChange: (color: string) => void onClose: () => void anchorName?: string } function hexToHsv(hex: string): { h: number; s: number; v: number } { hex = hex.replace('#', '') let r = 0, g = 0, b = 0 if (hex.length === 3) { r = parseInt(hex[0] + hex[0], 16) g = parseInt(hex[1] + hex[1], 16) b = parseInt(hex[2] + hex[2], 16) } else if (hex.length === 6) { r = parseInt(hex.substring(0, 2), 16) g = parseInt(hex.substring(2, 4), 16) b = parseInt(hex.substring(4, 6), 16) } r /= 255 g /= 255 b /= 255 const max = Math.max(r, g, b) const min = Math.min(r, g, b) const d = max - min let h = 0 const s = max === 0 ? 0 : d / max const v = max if (max !== min) { switch (max) { case r: h = (g - b) / d + (g < b ? 6 : 0); break case g: h = (b - r) / d + 2; break case b: h = (r - g) / d + 4; break } h /= 6 } return { h: h * 360, s: s * 100, v: v * 100 } } function hsvToRgb(h: number, s: number, v: number): { r: number; g: number; b: number } { let r = 0, g = 0, b = 0 const i = Math.floor(h * 6) const f = h * 6 - i const p = v * (1 - s) const q = v * (1 - f * s) const t = v * (1 - (1 - f) * s) switch (i % 6) { case 0: r = v; g = t; b = p; break case 1: r = q; g = v; b = p; break case 2: r = p; g = v; b = t; break case 3: r = p; g = q; b = v; break case 4: r = t; g = p; b = v; break case 5: r = v; g = p; b = q; break } return { r: Math.round(r * 255), g: Math.round(g * 255), b: Math.round(b * 255) } } function hsvToHex(h: number, s: number, v: number): string { const { r, g, b } = hsvToRgb(h / 360, s / 100, v / 100) function toHex(x: number) { const hex = x.toString(16) return hex.length === 1 ? '0' + hex : hex } return `#${toHex(r)}${toHex(g)}${toHex(b)}` } const PRESET_COLORS: string[] = [ '#f87171', '#fd8738', '#fbbf24', '#facc15', '#a3e635', '#4ade80', '#34d399', '#2dd4bf', '#38bdf8', '#60a5fa', '#818cf8', '#a78bfa', '#c084fc', '#e879f9', '#f472b6', '#fb7185' ] function SaturationPicker({ hsv, onChange }: { hsv: { h: number, s: number, v: number }, onChange: (s: number, v: number) => void }) { const containerRef = useRef(null) function handleMove(e: MouseEvent | React.MouseEvent) { if (!containerRef.current) return const { left, top, width, height } = containerRef.current.getBoundingClientRect() const x = Math.min(Math.max((e.clientX - left) / width, 0), 1) const y = Math.min(Math.max((e.clientY - top) / height, 0), 1) onChange(x * 100, (1 - y) * 100) } function handleMouseDown(e: React.MouseEvent) { handleMove(e) function moveHandler(e: MouseEvent) { handleMove(e) } function upHandler() { window.removeEventListener('mousemove', moveHandler) window.removeEventListener('mouseup', upHandler) } window.addEventListener('mousemove', moveHandler) window.addEventListener('mouseup', upHandler) } const bgColor = hsvToHex(hsv.h, 100, 100) return (
) } function HuePicker({ hue, onChange }: { hue: number, onChange: (h: number) => void }) { const containerRef = useRef(null) function handleMove(e: MouseEvent | React.MouseEvent) { if (!containerRef.current) return const { left, width } = containerRef.current.getBoundingClientRect() const x = Math.min(Math.max((e.clientX - left) / width, 0), 1) onChange(x * 360) } function handleMouseDown(e: React.MouseEvent) { handleMove(e) function moveHandler(e: MouseEvent) { handleMove(e) } function upHandler() { window.removeEventListener('mousemove', moveHandler) window.removeEventListener('mouseup', upHandler) } window.addEventListener('mousemove', moveHandler) window.addEventListener('mouseup', upHandler) } return (
) } export default function ColorPickerPopup({ value, onChange, onClose, anchorName }: ColorPickerPopupProps): JSX.Element { const [hsv, setHsv] = useState(() => hexToHsv(value || '#000000')) const [hexInput, setHexInput] = useState(value || '#000000') useEffect(() => { if (value && value !== hexInput) { setHsv(hexToHsv(value)) setHexInput(value) } }, [value]) function handleColorChange(newHsv: { h: number, s: number, v: number }) { setHsv(newHsv) const hex = hsvToHex(newHsv.h, newHsv.s, newHsv.v) setHexInput(hex) onChange(hex) } function handleSaturationChange(s: number, v: number) { handleColorChange({ ...hsv, s, v }) } function handleHueChange(h: number) { handleColorChange({ ...hsv, h }) } function manualHexChange(e: React.ChangeEvent) { const val = e.target.value setHexInput(val) if (/^#[0-9A-F]{6}$/i.test(val)) { const newHsv = hexToHsv(val) setHsv(newHsv) onChange(val) } } return (
HEX
{PRESET_COLORS.map(color => (
) }