mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-02 12:09:14 +08:00
3e5a392f73
* docs: form add color-picker demo * test: update snapshot * fix: color-picker support form * perf: optimize code logic * refactor: optimize code * test: update snapshot * test: update snap * test: update snap * chore: fix prettier * chore: fix prettier
53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
/* eslint-disable class-methods-use-this */
|
|
import type { ColorGenInput } from '@rc-component/color-picker';
|
|
import { Color as RcColor } from '@rc-component/color-picker';
|
|
|
|
export const toHexFormat = (value?: string, alpha?: boolean) =>
|
|
value?.replace(/[^\w/]/gi, '').slice(0, alpha ? 8 : 6) || '';
|
|
|
|
export const getHex = (value?: string, alpha?: boolean) => (value ? toHexFormat(value, alpha) : '');
|
|
|
|
export interface Color
|
|
extends Pick<
|
|
RcColor,
|
|
'toHsb' | 'toHsbString' | 'toHex' | 'toHexString' | 'toRgb' | 'toRgbString'
|
|
> {}
|
|
|
|
export class ColorFactory {
|
|
/** Original Color object */
|
|
private metaColor: RcColor;
|
|
|
|
constructor(color: ColorGenInput<Color>) {
|
|
this.metaColor = new RcColor(color as ColorGenInput);
|
|
if (!color) {
|
|
this.metaColor.setAlpha(0);
|
|
}
|
|
}
|
|
|
|
toHsb() {
|
|
return this.metaColor.toHsb();
|
|
}
|
|
|
|
toHsbString() {
|
|
return this.metaColor.toHsbString();
|
|
}
|
|
|
|
toHex() {
|
|
return getHex(this.toHexString(), this.metaColor.getAlpha() < 1);
|
|
}
|
|
|
|
toHexString() {
|
|
return this.metaColor.getAlpha() === 1
|
|
? this.metaColor.toHexString()
|
|
: this.metaColor.toHex8String();
|
|
}
|
|
|
|
toRgb() {
|
|
return this.metaColor.toRgb();
|
|
}
|
|
|
|
toRgbString() {
|
|
return this.metaColor.toRgbString();
|
|
}
|
|
}
|