11"use client" ;
22
33import { PipetteIcon , XIcon } from "lucide-react" ;
4- import { useCallback , useEffect , useMemo , useRef , useState , type PointerEvent } from "react" ;
4+ import { useCallback , useEffect , useRef , useState } from "react" ;
55
6+ import { hexToHsv , hsvToHex , type HsvColor } from "../../lib/color" ;
7+ import { ColorHueSlider , ColorSaturationValuePlane } from "../ui/color-picker" ;
8+ import { Input } from "../ui/input" ;
69import { Button } from "../ui/button" ;
710import { Popover , PopoverClose , PopoverPopup , PopoverTrigger } from "../ui/popover" ;
811import { normalizeProviderAccentColor } from "../../providerInstances" ;
912import { cn } from "../../lib/utils" ;
1013
1114const FALLBACK_ACCENT_COLOR = "#2563eb" ;
1215
13- function clamp ( value : number , min = 0 , max = 1 ) {
14- return Math . min ( max , Math . max ( min , value ) ) ;
15- }
16-
17- function hexToHsv ( hex : string ) {
18- const normalized = normalizeProviderAccentColor ( hex ) ?? FALLBACK_ACCENT_COLOR ;
19- const numeric = Number . parseInt ( normalized . slice ( 1 ) , 16 ) ;
20- const red = ( ( numeric >> 16 ) & 255 ) / 255 ;
21- const green = ( ( numeric >> 8 ) & 255 ) / 255 ;
22- const blue = ( numeric & 255 ) / 255 ;
23- const max = Math . max ( red , green , blue ) ;
24- const min = Math . min ( red , green , blue ) ;
25- const delta = max - min ;
26-
27- let hue = 0 ;
28- if ( delta !== 0 ) {
29- if ( max === red ) {
30- hue = ( ( green - blue ) / delta ) % 6 ;
31- } else if ( max === green ) {
32- hue = ( blue - red ) / delta + 2 ;
33- } else {
34- hue = ( red - green ) / delta + 4 ;
35- }
36- hue *= 60 ;
37- if ( hue < 0 ) hue += 360 ;
38- }
39-
40- return {
41- h : hue ,
42- s : max === 0 ? 0 : delta / max ,
43- v : max ,
44- } ;
45- }
46-
47- function hsvToHex ( hue : number , saturation : number , value : number ) {
48- const chroma = value * saturation ;
49- const x = chroma * ( 1 - Math . abs ( ( ( hue / 60 ) % 2 ) - 1 ) ) ;
50- const match = value - chroma ;
51- const [ red , green , blue ] =
52- hue < 60
53- ? [ chroma , x , 0 ]
54- : hue < 120
55- ? [ x , chroma , 0 ]
56- : hue < 180
57- ? [ 0 , chroma , x ]
58- : hue < 240
59- ? [ 0 , x , chroma ]
60- : hue < 300
61- ? [ x , 0 , chroma ]
62- : [ chroma , 0 , x ] ;
63-
64- return `#${ [ red , green , blue ]
65- . map ( ( channel ) =>
66- Math . round ( ( channel + match ) * 255 )
67- . toString ( 16 )
68- . padStart ( 2 , "0" ) ,
69- )
70- . join ( "" ) } `;
71- }
72-
7316function ProviderCustomColorPanel ( props : {
7417 readonly value : string ;
7518 readonly onCommit : ( value : string ) => void ;
7619} ) {
7720 const { onCommit } = props ;
78- const initialHsv = useMemo ( ( ) => hexToHsv ( props . value ) , [ props . value ] ) ;
79- const [ hsv , setHsv ] = useState ( initialHsv ) ;
21+ const [ hsv , setHsv ] = useState ( ( ) => hexToHsv ( props . value ) ) ;
8022 const currentColor = hsvToHex ( hsv . h , hsv . s , hsv . v ) ;
8123 const [ hexDraft , setHexDraft ] = useState < string | null > ( null ) ;
8224
8325 const commitHsv = useCallback (
84- ( nextHsv : typeof hsv ) => {
26+ ( nextHsv : HsvColor ) => {
8527 setHsv ( nextHsv ) ;
8628 onCommit ( hsvToHex ( nextHsv . h , nextHsv . s , nextHsv . v ) ) ;
8729 } ,
8830 [ onCommit ] ,
8931 ) ;
9032
91- const updateFromPlane = useCallback (
92- ( event : PointerEvent < HTMLDivElement > ) => {
93- const bounds = event . currentTarget . getBoundingClientRect ( ) ;
94- const saturation = clamp ( ( event . clientX - bounds . left ) / bounds . width ) ;
95- const value = 1 - clamp ( ( event . clientY - bounds . top ) / bounds . height ) ;
96- commitHsv ( { ...hsv , s : saturation , v : value } ) ;
97- } ,
98- [ commitHsv , hsv ] ,
99- ) ;
100-
101- const updateFromHue = useCallback (
102- ( event : PointerEvent < HTMLDivElement > ) => {
103- const bounds = event . currentTarget . getBoundingClientRect ( ) ;
104- commitHsv ( { ...hsv , h : clamp ( ( event . clientX - bounds . left ) / bounds . width ) * 360 } ) ;
105- } ,
106- [ commitHsv , hsv ] ,
107- ) ;
108-
109- const handlePointerDown = ( handler : ( event : PointerEvent < HTMLDivElement > ) => void ) => {
110- return ( event : PointerEvent < HTMLDivElement > ) => {
111- event . currentTarget . setPointerCapture ( event . pointerId ) ;
112- handler ( event ) ;
113- } ;
114- } ;
115-
11633 return (
11734 < div className = "w-56 bg-popover" >
118- < div
119- className = "relative h-36 cursor-crosshair touch-none"
120- style = { {
121- backgroundColor : `hsl(${ hsv . h } 100% 50%)` ,
122- backgroundImage :
123- "linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, transparent)" ,
124- } }
125- onPointerDown = { handlePointerDown ( updateFromPlane ) }
126- onPointerMove = { ( event ) => {
127- if ( event . currentTarget . hasPointerCapture ( event . pointerId ) ) {
128- updateFromPlane ( event ) ;
129- }
130- } }
131- >
132- < span
133- className = "pointer-events-none absolute size-3 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-white shadow-[0_0_0_1px_rgb(0_0_0/0.35)]"
134- style = { { left : `${ hsv . s * 100 } %` , top : `${ ( 1 - hsv . v ) * 100 } %` } }
135- />
136- </ div >
35+ < ColorSaturationValuePlane
36+ label = "Accent color saturation and brightness"
37+ value = { hsv }
38+ onChange = { commitHsv }
39+ className = "h-36 rounded-none"
40+ />
13741 < div className = "grid gap-3 p-3" >
138- < div
139- className = "relative h-3 cursor-pointer touch-none rounded-full"
140- style = { {
141- background : "linear-gradient(to right, #f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00)" ,
142- } }
143- onPointerDown = { handlePointerDown ( updateFromHue ) }
144- onPointerMove = { ( event ) => {
145- if ( event . currentTarget . hasPointerCapture ( event . pointerId ) ) {
146- updateFromHue ( event ) ;
147- }
148- } }
149- >
150- < span
151- className = "pointer-events-none absolute top-1/2 size-4 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-white shadow-[0_0_0_1px_rgb(0_0_0/0.35)]"
152- style = { { left : `${ ( hsv . h / 360 ) * 100 } %` , backgroundColor : currentColor } }
153- />
154- </ div >
155- < input
42+ < ColorHueSlider
43+ label = "Accent color hue"
44+ value = { hsv . h }
45+ onChange = { ( h ) => commitHsv ( { ...hsv , h } ) }
46+ />
47+ < Input
48+ nativeInput
49+ size = "sm"
15650 value = { hexDraft ?? currentColor }
15751 onChange = { ( event ) => {
15852 const nextColor = event . currentTarget . value ;
@@ -162,7 +56,7 @@ function ProviderCustomColorPanel(props: {
16256 props . onCommit ( nextColor ) ;
16357 } }
16458 onBlur = { ( ) => setHexDraft ( null ) }
165- className = "h-8 rounded-md border border-input bg-background px-2 font-mono text-xs text-foreground outline-none transition-colors focus:border-ring "
59+ className = "rounded-md font-mono text-xs"
16660 aria-label = "Custom hex accent color"
16761 spellCheck = { false }
16862 />
0 commit comments