Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(design): add RGBA support to ColorPicker (#6412)

白熱d46cd6dd9d15
diff --git a/packages/design/src/components/color-picker/AlphaSlider.tsx b/packages/design/src/components/color-picker/AlphaSlider.tsx
index 6a3ae84563..41f496fe19 100644
--- a/packages/design/src/components/color-picker/AlphaSlider.tsx
+++ b/packages/design/src/components/color-picker/AlphaSlider.tsx
@@ -92,7 +92,7 @@ export function AlphaSlider({ hsv, alpha, onChange, onChanged }: IAlphaSliderPro
univer-relative univer-h-2 univer-w-full univer-cursor-pointer univer-rounded-full univer-shadow-inner
`}
style={{
- background: `linear-gradient(to right, transparent, rgb(${color}))`,
+ background: `linear-gradient(to right, transparent, rgb(${color.join(',')}))`,
}}
onPointerDown={(e) => {
setIsDragging(true);
diff --git a/packages/design/src/components/color-picker/ColorInput.tsx b/packages/design/src/components/color-picker/ColorInput.tsx
index c26917d56e..63cccb30f8 100644
--- a/packages/design/src/components/color-picker/ColorInput.tsx
+++ b/packages/design/src/components/color-picker/ColorInput.tsx
@@ -22,12 +22,16 @@ import { hexToHsv, hsvToHex, hsvToRgb, rgbToHsv } from './color-conversion';
interface IColorInputProps {
hsv: [number, number, number];
- onChange: (h: number, s: number, v: number) => void;
+ alpha: number;
+ format: 'hex' | 'rgba';
+ onChange: (h: number, s: number, v: number, a?: number) => void;
}
interface IInputProps {
hsv: [number, number, number];
- onChange?: (h: number, s: number, v: number) => void;
+ alpha?: number;
+ format?: 'hex' | 'rgba';
+ onChange?: (h: number, s: number, v: number, a?: number) => void;
}
function HexInput({ hsv, onChange }: IInputProps) {
@@ -91,8 +95,8 @@ function HexInput({ hsv, onChange }: IInputProps) {
);
}
-function RgbInput({ hsv, onChange }: IInputProps) {
- const [localValues, setLocalValues] = useState({ r: 0, g: 0, b: 0 });
+function RgbInput({ hsv, alpha, format, onChange }: IInputProps) {
+ const [localValues, setLocalValues] = useState({ r: 0, g: 0, b: 0, a: 1 });
useEffect(() => {
const [r, g, b] = hsvToRgb(hsv[0], hsv[1], hsv[2]);
@@ -100,10 +104,23 @@ function RgbInput({ hsv, onChange }: IInputProps) {
r: Math.round(r),
g: Math.round(g),
b: Math.round(b),
+ a: alpha ?? 1,
});
- }, [hsv]);
+ }, [hsv, alpha]);
+
+ const handleChange = (color: 'r' | 'g' | 'b' | 'a', value: string) => {
+ if (color === 'a') {
+ if (value !== '' && !/^\d*\.?\d*$/.test(value)) return;
+ const numValue = value === '' ? 0 : Number.parseFloat(value);
+ if (numValue > 1) return;
+ const newValues = { ...localValues, a: numValue };
+ setLocalValues(newValues);
+ if (onChange) {
+ onChange(hsv[0], hsv[1], hsv[2], numValue);
+ }
+ return;
+ }
- const handleChange = (color: 'r' | 'g' | 'b', value: string) => {
if (value !== '' && !/^\d*$/.test(value)) return;
const numValue = value === '' ? 0 : Number.parseInt(value, 10);
@@ -115,7 +132,7 @@ function RgbInput({ hsv, onChange }: IInputProps) {
if (onChange) {
const hsv = rgbToHsv(newValues.r, newValues.g, newValues.b);
- onChange(...hsv);
+ onChange(...hsv, localValues.a);
}
};
@@ -125,6 +142,7 @@ function RgbInput({ hsv, onChange }: IInputProps) {
r: Math.round(r),
g: Math.round(g),
b: Math.round(b),
+ a: alpha ?? 1,
});
};
@@ -154,11 +172,19 @@ function RgbInput({ hsv, onChange }: IInputProps) {
onBlur={handleBlur}
maxLength={3}
/>
+ {format === 'rgba' && (
+ <input
+ value={localValues.a}
+ onChange={(e) => handleChange('a', e.target.value)}
+ onBlur={handleBlur}
+ maxLength={4}
+ />
+ )}
</div>
);
}
-export function ColorInput({ hsv, onChange }: IColorInputProps) {
+export function ColorInput({ hsv, alpha, format, onChange }: IColorInputProps) {
return (
<div
className={`
@@ -172,7 +198,7 @@ export function ColorInput({ hsv, onChange }: IColorInputProps) {
>
<div className="univer-relative univer-flex univer-flex-1 univer-gap-2">
<HexInput hsv={hsv} onChange={onChange} />
- <RgbInput hsv={hsv} onChange={onChange} />
+ <RgbInput hsv={hsv} alpha={alpha} format={format} onChange={onChange} />
</div>
</div>
);
diff --git a/packages/design/src/components/color-picker/ColorPicker.stories.tsx b/packages/design/src/components/color-picker/ColorPicker.stories.tsx
index 43e5bc0c21..4c5d341e0f 100644
--- a/packages/design/src/components/color-picker/ColorPicker.stories.tsx
+++ b/packages/design/src/components/color-picker/ColorPicker.stories.tsx
@@ -36,10 +36,33 @@ export const Playground = {
const [value, setValue] = useState('#FF0000');
return (
- <>
- {value}
- <ColorPicker value={value} onChange={setValue} />
- </>
+ <div className="univer-flex univer-flex-col univer-gap-4">
+ <div>
+ <div>
+ HEX Format:
+ {value}
+ </div>
+ <ColorPicker format="hex" value={value} onChange={setValue} />
+ </div>
+ </div>
+ );
+ },
+};
+
+export const RgbaFormat = {
+ render() {
+ const [value, setValue] = useState('rgba(255, 0, 0, 0.5)');
+
+ return (
+ <div className="univer-flex univer-flex-col univer-gap-4">
+ <div>
+ <div>
+ RGBA Format:
+ {value}
+ </div>
+ <ColorPicker format="rgba" value={value} onChange={setValue} />
+ </div>
+ </div>
);
},
};
diff --git a/packages/design/src/components/color-picker/ColorPicker.tsx b/packages/design/src/components/color-picker/ColorPicker.tsx
index db0367c511..4a5ddccaa0 100644
--- a/packages/design/src/components/color-picker/ColorPicker.tsx
+++ b/packages/design/src/components/color-picker/ColorPicker.tsx
@@ -19,7 +19,8 @@ import { isBrowser } from '../../helper/is-browser';
import { Button } from '../button/Button';
import { ConfigContext } from '../config-provider/ConfigProvider';
import { Dialog } from '../dialog/Dialog';
-import { hexToHsv, hsvToHex, hsvToRgb, rgbToHex } from './color-conversion';
+import { AlphaSlider } from './AlphaSlider';
+import { hexToHsv, hsvToHex, hsvToRgb, hsvToRgba, parseRgba, rgbToHex, rgbToHsv } from './color-conversion';
import { ColorInput } from './ColorInput';
import { ColorPresets } from './ColorPresets';
import { ColorSpectrum } from './ColorSpectrum';
@@ -27,21 +28,23 @@ import { HueSlider } from './HueSlider';
const MemoizedColorSpectrum = memo(ColorSpectrum);
const MemoizedHueSlider = memo(HueSlider);
+const MemoizedAlphaSlider = memo(AlphaSlider);
const MemoizedColorInput = memo(ColorInput);
const MemoizedColorPresets = memo(ColorPresets);
export interface IColorPickerProps {
- format?: 'hex';
+ format?: 'hex' | 'rgba';
value?: string;
onChange?: (value: string) => void;
}
-export function ColorPicker({ format = 'hex', value = '#000000', onChange }: IColorPickerProps) {
+export function ColorPicker({ format = 'hex', value, onChange }: IColorPickerProps) {
if (!isBrowser) return null;
const { locale } = useContext(ConfigContext);
const [hsv, setHsv] = useState<[number, number, number]>([0, 100, 100]);
+ const [alpha, setAlpha] = useState(1);
const [visible, setVisible] = useState(false);
const getRgb = useCallback((h: number, s: number, v: number) => {
@@ -50,31 +53,50 @@ export function ColorPicker({ format = 'hex', value = '#000000', onChange }: ICo
useEffect(() => {
try {
+ const actualValue = value || (format === 'hex' ? '#000000' : 'rgba(0, 0, 0, 1)');
if (format === 'hex') {
- const [h, s, v] = value ? hexToHsv(value) : hsv;
+ const [h, s, v] = hexToHsv(actualValue);
setHsv([h, s, v]);
+ setAlpha(1);
+ } else if (format === 'rgba') {
+ const [r, g, b, a] = parseRgba(actualValue);
+ const [h, s, v] = rgbToHsv(r, g, b);
+ setHsv([h, s, v]);
+ setAlpha(a);
}
} catch (error) {
console.error('Invalid value:', error);
}
- }, [value]);
+ }, [value, format]);
function handleColorChange(h: number, s: number, v: number) {
setHsv([h, s, v]);
}
- function handleColorChanged(h: number, s: number, v: number) {
- const [r, g, b] = getRgb(h, s, v);
+ function handleAlphaChange(a: number) {
+ setAlpha(a);
+ }
+
+ function handleColorChanged(h: number, s: number, v: number, a: number = alpha) {
if (format === 'hex') {
+ const [r, g, b] = getRgb(h, s, v);
const hex = rgbToHex(r, g, b);
onChange?.(hex);
+ } else if (format === 'rgba') {
+ const [r, g, b] = getRgb(h, s, v);
+ onChange?.(`rgba(${r}, ${g}, ${b}, ${a})`);
}
}
function handleConfirmCustomColor() {
const [h, s, v] = hsv;
- const hex = hsvToHex(h, s, v);
- onChange?.(hex);
+ if (format === 'hex') {
+ const hex = hsvToHex(h, s, v);
+ onChange?.(hex);
+ } else if (format === 'rgba') {
+ const [r, g, b] = getRgb(h, s, v);
+ onChange?.(`rgba(${r}, ${g}, ${b}, ${alpha})`);
+ }
setVisible(false);
}
@@ -91,7 +113,8 @@ export function ColorPicker({ format = 'hex', value = '#000000', onChange }: ICo
hsv={hsv}
onChange={(h, s, v) => {
handleColorChange(h, s, v);
- handleColorChanged(h, s, v);
+ handleAlphaChange(1);
+ handleColorChanged(h, s, v, 1);
}}
/>
@@ -125,18 +148,32 @@ export function ColorPicker({ format = 'hex', value = '#000000', onChange }: ICo
<div
className="univer-size-6 univer-flex-shrink-0 univer-rounded-sm"
style={{
- backgroundColor: hsvToHex(...hsv),
+ backgroundColor: format === 'hex' ? hsvToHex(...hsv) : hsvToRgba(...hsv, alpha),
}}
/>
- <MemoizedHueSlider
- hsv={hsv}
- onChange={handleColorChange}
- />
+ <div className="univer-flex-1 univer-space-y-2">
+ <MemoizedHueSlider
+ hsv={hsv}
+ onChange={handleColorChange}
+ />
+ {format === 'rgba' && (
+ <MemoizedAlphaSlider
+ hsv={hsv}
+ alpha={alpha}
+ onChange={handleAlphaChange}
+ />
+ )}
+ </div>
</div>
<MemoizedColorInput
hsv={hsv}
- onChange={handleColorChange}
+ alpha={alpha}
+ format={format}
+ onChange={(h, s, v, a) => {
+ handleColorChange(h, s, v);
+ if (a !== undefined) handleAlphaChange(a);
+ }}
/>
<footer className="univer-flex univer-items-center univer-justify-end univer-gap-2">
diff --git a/packages/design/src/components/color-picker/__tests__/index.spec.tsx b/packages/design/src/components/color-picker/__tests__/index.spec.tsx
index 95177873e4..a762e87aee 100644
--- a/packages/design/src/components/color-picker/__tests__/index.spec.tsx
+++ b/packages/design/src/components/color-picker/__tests__/index.spec.tsx
@@ -153,7 +153,7 @@ describe('ColorInput', () => {
}
it('should call onChange when color input changes', () => {
- const { container } = render(<TestComponent onChange={onChange} />);
+ const { container } = render(<TestComponent onChange={onChange} alpha={1} format="hex" />);
const inputs = container.querySelectorAll('input[type="text"]') as unknown as HTMLInputElement[];
const [rInput, gInput, bInput] = inputs;
diff --git a/packages/design/src/components/color-picker/color-conversion.ts b/packages/design/src/components/color-picker/color-conversion.ts
index 6f24b90aec..f9a9e9fcd0 100644
--- a/packages/design/src/components/color-picker/color-conversion.ts
+++ b/packages/design/src/components/color-picker/color-conversion.ts
@@ -115,6 +115,11 @@ export const hsvToHex = (h: number, s: number, v: number): string => {
return rgbToHex(r, g, b);
};
+export const hsvToRgba = (h: number, s: number, v: number, a: number): string => {
+ const [r, g, b] = hsvToRgb(h, s, v);
+ return `rgba(${r}, ${g}, ${b}, ${a})`;
+};
+
export const parseRgba = (rgba: string): [number, number, number, number] => {
const match = rgba.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d*\.?\d+))?\)/);
if (!match) throw new Error('Invalid RGBA string');