From 07fe0e8dc88bc9f2ee8db4437f619d366dd0e83f Mon Sep 17 00:00:00 2001 From: blessedcoolant <54517381+blessedcoolant@users.noreply.github.com> Date: Thu, 21 Mar 2024 00:24:24 +0530 Subject: [PATCH] chore: Move color transformers to new file --- .../src/common/util/colorCodeTransformers.ts | 17 +++++++++++++++++ .../fields/inputs/ColorFieldInputComponent.tsx | 17 +---------------- 2 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 invokeai/frontend/web/src/common/util/colorCodeTransformers.ts diff --git a/invokeai/frontend/web/src/common/util/colorCodeTransformers.ts b/invokeai/frontend/web/src/common/util/colorCodeTransformers.ts new file mode 100644 index 0000000000..f6e47d59e2 --- /dev/null +++ b/invokeai/frontend/web/src/common/util/colorCodeTransformers.ts @@ -0,0 +1,17 @@ +import type { RgbaColor } from 'react-colorful'; + +export function rgbaToHex(color: RgbaColor): string { + const hex = ((1 << 24) + (color.r << 16) + (color.g << 8) + color.b).toString(16).slice(1); + const alphaHex = Math.round(color.a * 255) + .toString(16) + .padStart(2, '0'); + return `#${hex}${alphaHex}`; +} + +export function hexToRGBA(hex: string, alpha: number) { + hex = hex.replace(/^#/, ''); + const r = parseInt(hex.substring(0, 2), 16); + const g = parseInt(hex.substring(2, 4), 16); + const b = parseInt(hex.substring(4, 6), 16); + return { r, g, b, a: alpha }; +} diff --git a/invokeai/frontend/web/src/features/nodes/components/flow/nodes/Invocation/fields/inputs/ColorFieldInputComponent.tsx b/invokeai/frontend/web/src/features/nodes/components/flow/nodes/Invocation/fields/inputs/ColorFieldInputComponent.tsx index e95adad697..093e3459ab 100644 --- a/invokeai/frontend/web/src/features/nodes/components/flow/nodes/Invocation/fields/inputs/ColorFieldInputComponent.tsx +++ b/invokeai/frontend/web/src/features/nodes/components/flow/nodes/Invocation/fields/inputs/ColorFieldInputComponent.tsx @@ -1,5 +1,6 @@ import { Box } from '@invoke-ai/ui-library'; import { useAppDispatch } from 'app/store/storeHooks'; +import { hexToRGBA, rgbaToHex } from 'common/util/colorCodeTransformers'; import { colorTokenToCssVar } from 'common/util/colorTokenToCssVar'; import { fieldColorValueChanged } from 'features/nodes/store/nodesSlice'; import type { ColorFieldInputInstance, ColorFieldInputTemplate } from 'features/nodes/types/field'; @@ -9,22 +10,6 @@ import { HexColorInput, RgbaColorPicker } from 'react-colorful'; import type { FieldComponentProps } from './types'; -function rgbaToHex(color: RgbaColor): string { - const hex = ((1 << 24) + (color.r << 16) + (color.g << 8) + color.b).toString(16).slice(1); - const alphaHex = Math.round(color.a * 255) - .toString(16) - .padStart(2, '0'); - return `#${hex}${alphaHex}`; -} - -function hexToRGBA(hex: string, alpha: number) { - hex = hex.replace(/^#/, ''); - const r = parseInt(hex.substring(0, 2), 16); - const g = parseInt(hex.substring(2, 4), 16); - const b = parseInt(hex.substring(4, 6), 16); - return { r, g, b, a: alpha }; -} - const FALLBACK_COLOR: RgbaColor = { r: 0, g: 0, b: 0, a: 255 }; const ColorFieldInputComponent = (props: FieldComponentProps) => {