chore: Move color transformers to new file

This commit is contained in:
blessedcoolant 2024-03-21 00:24:24 +05:30
parent 45fc7d8054
commit 07fe0e8dc8
2 changed files with 18 additions and 16 deletions

View File

@ -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 };
}

View File

@ -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<ColorFieldInputInstance, ColorFieldInputTemplate>) => {