From fa9ee9ce31e91c6ed03bf2364ed9135066e2e568 Mon Sep 17 00:00:00 2001 From: Thomas Kennedy Date: Wed, 15 May 2024 13:34:57 -0500 Subject: [PATCH] Tweaks --- public/editor/ColorPicker.tsx | 7 +++---- public/editor/FeatureGallery.tsx | 5 +---- public/editor/Sketch.tsx | 21 +++++++++++++++------ public/editor/stateStore.ts | 17 ++++++++--------- public/editor/types.ts | 2 -- public/editor/utils.ts | 3 +-- 6 files changed, 28 insertions(+), 27 deletions(-) diff --git a/public/editor/ColorPicker.tsx b/public/editor/ColorPicker.tsx index edfb942..3050380 100644 --- a/public/editor/ColorPicker.tsx +++ b/public/editor/ColorPicker.tsx @@ -6,6 +6,7 @@ import { PopoverContent, Popover, } from "@nextui-org/react"; +import { ColorFormat } from "./types"; const rgbaObjToRgbaStr = (rgbaObj: { r: number; @@ -22,15 +23,13 @@ export const ColorPicker = ({ style, value, colorFormat = "hex", - allowAlpha = false, presetColors, }: { onClick?: () => void; onChange: (hex: string) => void; style?: CSSProperties; value: string; - colorFormat: "hex" | "rgba"; - allowAlpha: boolean; + colorFormat: ColorFormat; presetColors: string[]; }) => { return ( @@ -48,8 +47,8 @@ export const ColorPicker = ({ { if (colorFormat === "rgba") { onChange(rgbaObjToRgbaStr(color.rgba)); diff --git a/public/editor/FeatureGallery.tsx b/public/editor/FeatureGallery.tsx index 0f512cd..cca4397 100644 --- a/public/editor/FeatureGallery.tsx +++ b/public/editor/FeatureGallery.tsx @@ -244,9 +244,7 @@ const FeatureSelector = ({ }); }; - let colorFormat = gallerySectionConfig.colorFormat - ? gallerySectionConfig.colorFormat - : "hex"; + const colorFormat = gallerySectionConfig.colorFormat || "hex"; return (
{ const eyeDropper = new window.EyeDropper!(); @@ -85,7 +87,7 @@ export interface SketchProps width?: number; color?: string | HsvaColor; presetColors: string[]; - allowAlpha: boolean; + colorFormat: ColorFormat; onChange?: (newShade: ColorResult) => void; } @@ -113,11 +115,18 @@ export const Sketch = React.forwardRef( width = 240, color, style, - allowAlpha, + colorFormat, presetColors, ...other } = props; + let formattedPresetColors = presetColors; + if (colorFormat === "rgba") { + formattedPresetColors = presetColors.map((rgbaColor) => + hsvaToHexa(rgbaStringToHsva(rgbaColor)), + ); + } + const [hsva, setHsva] = useState({ h: 209, s: 36, v: 90, a: 1 }); useEffect(() => { if (typeof color === "string" && validHex(color)) { @@ -200,7 +209,7 @@ export const Sketch = React.forwardRef( />
- {allowAlpha && ( + {colorFormat === "rgba" && (
( handleChange(result.hsva)} />
handleChange(hsvColor)} rectProps={{ diff --git a/public/editor/stateStore.ts b/public/editor/stateStore.ts index 8b9eea7..d9849b6 100644 --- a/public/editor/stateStore.ts +++ b/public/editor/stateStore.ts @@ -1,5 +1,10 @@ import { create, StateCreator } from "zustand"; -import { CombinedState, GallerySectionConfig, GallerySize } from "./types"; +import { + ColorFormat, + CombinedState, + GallerySectionConfig, + GallerySize, +} from "./types"; import { generate } from "../../src/generate"; import { generateRangeFromStep, getFromDict, roundTwoDecimals } from "./utils"; import { @@ -16,16 +21,14 @@ const gallerySectionInfos: (Pick< ( | { selectionType: "color"; - colorFormat: "rgba" | "hex"; - allowAlpha: boolean; + colorFormat: ColorFormat; renderOptions: { valuesToRender: string[]; }; } | { selectionType: "colors"; - colorFormat: "rgba" | "hex"; - allowAlpha: boolean; + colorFormat: ColorFormat; renderOptions: { colorCount: number; valuesToRender: string[][]; @@ -53,7 +56,6 @@ const gallerySectionInfos: (Pick< isSelected: true, selectionType: "color", colorFormat: "hex", - allowAlpha: false, renderOptions: { valuesToRender: distinctSkinColors, }, @@ -139,7 +141,6 @@ const gallerySectionInfos: (Pick< text: "Hair Color", selectionType: "color", colorFormat: "hex", - allowAlpha: false, renderOptions: { valuesToRender: distinctHairColors, }, @@ -159,7 +160,6 @@ const gallerySectionInfos: (Pick< text: "Shave Style", selectionType: "color", colorFormat: "rgba", - allowAlpha: true, renderOptions: { valuesToRender: [ "rgba(0,0,0,0)", @@ -251,7 +251,6 @@ const gallerySectionInfos: (Pick< text: "Team Colors", selectionType: "colors", colorFormat: "hex", - allowAlpha: false, renderOptions: { colorCount: 3, valuesToRender: jerseyColorOptions, diff --git a/public/editor/types.ts b/public/editor/types.ts index 938aa0f..9bb25be 100644 --- a/public/editor/types.ts +++ b/public/editor/types.ts @@ -52,7 +52,6 @@ type GallerySectionConfigColor = GallerySectionConfigBase & { selectionType: "color"; selectedValue: string; colorFormat: ColorFormat; - allowAlpha: boolean; renderOptions: { valuesToRender: string[]; }; @@ -62,7 +61,6 @@ type GallerySectionConfigColors = GallerySectionConfigBase & { selectionType: "colors"; selectedValue: string[]; colorFormat: ColorFormat; - allowAlpha: boolean; renderOptions: { colorCount: number; valuesToRender: string[][]; diff --git a/public/editor/utils.ts b/public/editor/utils.ts index 54ccbc2..73a41ba 100644 --- a/public/editor/utils.ts +++ b/public/editor/utils.ts @@ -162,8 +162,7 @@ export const luma = (colorHex: string): number => { export const doesStrLookLikeColor = (str: string): boolean => { const regex = - /^(#([0-9A-F]{3,4}){1,2}$|rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})(,\s*((0?\.\d+)|(1\.0?)|1))?\))$/i; - + /^(#([0-9A-F]{3,4}){1,2}$|rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})(,\s*((0(?:\.\d+)?|1(?:\.0)?)))?\))$/i; return regex.test(str); };