diff --git a/src/components/HslAssemble.module.css b/src/components/HslAssemble.module.css new file mode 100644 index 0000000..e69de29 diff --git a/src/components/HslAsssemble.tsx b/src/components/HslAsssemble.tsx new file mode 100644 index 0000000..8e35f7b --- /dev/null +++ b/src/components/HslAsssemble.tsx @@ -0,0 +1,73 @@ +import { floor } from 'lodash-es'; +import { useEffect, useMemo, useState } from 'react'; +import { useColorFunction } from '../ColorFunctionContext'; +import { ColorRangePicker } from './ColorRangePicker'; + +type HslAssembleProps = { + color: string; + onChange: (color: string) => void; +}; + +export function HslAssemble({ color, onChange }: HslAssembleProps) { + const { colorFn } = useColorFunction(); + const colorComponent = useMemo(() => { + try { + const [h, s, l] = colorFn?.represent_hsl(color) ?? [0, 0, 0]; + return [h, s, l]; + } catch (error) { + console.error('[Convert HSL]', error); + } + return [0, 0, 0]; + }, [color]); + const [h, setH] = useState(0); + const [s, setS] = useState(0); + const [l, setL] = useState(0); + + useEffect(() => { + try { + const newColor = colorFn?.hsl_to_hex(h, s / 100, l / 100) ?? '000000'; + onChange(newColor); + } catch (error) { + console.error('[Convert RGB]', error); + } + }, [h, s, l]); + useEffect(() => { + setH(colorComponent[0]); + setS(colorComponent[1] * 100); + setL(colorComponent[2] * 100); + }, []); + + return ( +
+ floor(v, 2)} + /> + floor(v, 2)} + /> + floor(v, 2)} + /> +
+ ); +}