Skip to content

Fix color picker on manual hex code entry #703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/graph-editor/src/components/controls/color.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export const ColorField = observer(({ port, readOnly }: IField) => {
}
}, [port.value]);

const isValidHexColor = (hex: string): boolean => {
return /^#([0-9A-Fa-f]{3}){1,2}$/.test(hex);
};

const onChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement> | string) => {
let col;
Expand All @@ -38,7 +42,15 @@ export const ColorField = observer(({ port, readOnly }: IField) => {
}

//We need to convert from hex
(port as Input).setValue(hexToColor(col));
try {
// Only convert if the hex color is valid
if (isValidHexColor(col)) {
(port as Input).setValue(hexToColor(col));
}
} catch (error) {
console.error('Error converting hex color:', error);
// Don't set invalid value to port, just update the display value
}
},
[port, useDelayed],
);
Expand All @@ -65,7 +77,15 @@ export const ColorField = observer(({ port, readOnly }: IField) => {
{useDelayed && (
<IconButton
icon={<FloppyDisk />}
onClick={() => (port as Input).setValue(val)}
onClick={() => {
try {
if (isValidHexColor(val)) {
(port as Input).setValue(hexToColor(val));
}
} catch (error) {
console.error('Error saving hex color:', error);
}
}}
/>
)}
</Stack>
Expand Down
8 changes: 7 additions & 1 deletion packages/graph-engine/src/nodes/color/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ export const toHex = (color: Color): string =>
color.to('srgb').toString({ format: 'hex' });

export const hexToColor = (hex: string): ColorType => {
return toColorObject(new Color(hex));
try {
return toColorObject(new Color(hex));
} catch (error) {
console.error('Invalid hex color:', error);
// Return black as a fallback
return Black;
}
};

export const Black = {
Expand Down