From 8a7fa80f9c5661a0627f0f319a77b2cee22d2d1b Mon Sep 17 00:00:00 2001 From: Ainur Timerbaev Date: Sun, 2 Oct 2022 17:48:08 +0100 Subject: [PATCH] fix(ColorEditor): fix rgb to other colors conversion --- src/components/color-editor/color-editor.tsx | 35 ++++++-------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/src/components/color-editor/color-editor.tsx b/src/components/color-editor/color-editor.tsx index c22601a16..6633c661b 100644 --- a/src/components/color-editor/color-editor.tsx +++ b/src/components/color-editor/color-editor.tsx @@ -9,7 +9,7 @@ type Payload = AnyColor; export type ColorFormat = "color_xy" | "color_hs"; type ColorProps = { value: Payload; - steps?: string[]; + steps?: string[][]; format: ColorFormat; onChange(value: Record): void; minimal?: boolean; @@ -37,44 +37,31 @@ export const toRGB = (source: AnyColor, sourceFormat: ColorFormat): string => { } } -const rgbToTargetFormat = (source: string, targetFormat: ColorFormat): Record => { - switch (targetFormat) { - case "color_hs": - const [hue, saturation] = convertColors.hex.hsv(source); - return { hue, saturation }; - - - case "color_xy": - const [X, Y, Z] = convertColors.hex.xyz(source); - const x = X / (X + Y + Z); - const y = Y / (X + Y + Z); - return { x, y }; +const whitePallet = ['#FFFFFF', '#FDF4DC', '#F4FDFF']; +const pridePallet = ['#FF0018', '#FFA52C', '#FFFF41', '#008018', '#0000F9', '#86007D']; - default: - return { hex: source }; - } -} -const pridePallet = ['#FF0018', '#FFA52C', '#FFFF41', '#008018', '#0000F9', '#86007D']; const ColorEditor: FunctionComponent, 'onChange' | 'value'>> = (props) => { - const { onChange, value = {} as AnyColor, format, steps = pridePallet, minimal, ...rest } = props; + const { onChange, value = {} as AnyColor, format, steps = [pridePallet, whitePallet], minimal, ...rest } = props; const [currentColor, setCurrentColor] = useState(toRGB(value, format)); useEffect(() => { setCurrentColor(toRGB(value, format)) }, [value, format]); return <> - {!minimal &&
+ {!minimal && steps.map((pallet, idx) =>
{ - steps.map(step => + pallet.map(step => className="btn" style={{ backgroundColor: step }} key={step} item={step} title={step} - onClick={(item) => onChange(rgbToTargetFormat(item, format))}>   ) + onClick={(item) => onChange({ hex: item })}>    + ) } -
} +
)} + { if (e.target.value.toLowerCase() !== currentColor.toLowerCase()) { - onChange(rgbToTargetFormat(e.target.value, format)) + onChange({ hex: e.target.value }) } }}