Skip to content

Commit 8fb359b

Browse files
authored
refactor: bump fast-color version (#259)
* chore: adjust new ver * chore: bump
1 parent 5d04f41 commit 8fb359b

File tree

6 files changed

+25
-26
lines changed

6 files changed

+25
-26
lines changed

docs/example/basic.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ export default () => {
77

88
return (
99
<>
10-
<ColorPicker color={value} onChange={setValue} />
10+
<ColorPicker
11+
color={value}
12+
onChange={nextValue => {
13+
console.log('onChange', nextValue.toHsbString(), nextValue);
14+
setValue(nextValue);
15+
}}
16+
/>
1117
<br />
1218
<div
1319
style={{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
]
4646
},
4747
"dependencies": {
48-
"@ant-design/fast-color": "^1.2.0",
48+
"@ant-design/fast-color": "^2.0.1",
4949
"@babel/runtime": "^7.23.6",
5050
"classnames": "^2.2.6",
5151
"rc-util": "^5.38.1"

src/ColorPicker.tsx

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CSSProperties } from 'react';
22
import React, { forwardRef, useMemo } from 'react';
3-
import { ColorPickerPrefixCls, defaultColor, generateColor } from './util';
3+
import { ColorPickerPrefixCls, defaultColor } from './util';
44

55
import classNames from 'classnames';
66
import { Color } from './color';
@@ -76,12 +76,10 @@ export default forwardRef<HTMLDivElement, ColorPickerProps>((props, ref) => {
7676
value,
7777
defaultValue,
7878
});
79-
const alphaColor = useMemo(() => {
80-
const rgb = generateColor(colorValue.toRgbString());
81-
// alpha color need equal 1 for base color
82-
rgb.setAlpha(1);
83-
return rgb.toRgbString();
84-
}, [colorValue]);
79+
const alphaColor = useMemo(
80+
() => colorValue.setA(1).toRgbString(),
81+
[colorValue],
82+
);
8583

8684
// ============================ Events ============================
8785
const handleChange: BaseColorPickerProps['onChange'] = (data, type) => {
@@ -92,17 +90,10 @@ export default forwardRef<HTMLDivElement, ColorPickerProps>((props, ref) => {
9290
};
9391

9492
// Convert
95-
const getHueColor = (hue: number) => {
96-
const hsb = colorValue.toHsb();
97-
hsb.h = hue;
98-
return new Color(hsb);
99-
};
93+
const getHueColor = (hue: number) => new Color(colorValue.setHue(hue));
10094

101-
const getAlphaColor = (alpha: number) => {
102-
const hsb = colorValue.toHsb();
103-
hsb.a = Math.round(alpha) / 100;
104-
return new Color(hsb);
105-
};
95+
const getAlphaColor = (alpha: number) =>
96+
new Color(colorValue.setA(alpha / 100));
10697

10798
// Slider change
10899
const onHueChange = (hue: number) => {
@@ -170,7 +161,7 @@ export default forwardRef<HTMLDivElement, ColorPickerProps>((props, ref) => {
170161
]}
171162
min={0}
172163
max={100}
173-
value={colorValue.getAlpha() * 100}
164+
value={colorValue.a * 100}
174165
onChange={onAlphaChange}
175166
onChangeComplete={onAlphaChangeComplete}
176167
/>

src/components/Gradient.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { FC } from 'react';
22
import React, { useMemo } from 'react';
3-
import type { Color } from '../color';
3+
import { Color } from '../color';
44
import type { HsbaColorType } from '../interface';
55
import { generateColor } from '../util';
66

@@ -15,9 +15,9 @@ const Gradient: FC<{
1515
() =>
1616
colors
1717
.map((color, idx) => {
18-
const result = generateColor(color);
18+
let result = generateColor(color);
1919
if (type === 'alpha' && idx === colors.length - 1) {
20-
result.setAlpha(1);
20+
result = new Color(result.setA(1));
2121
}
2222
return result.toRgbString();
2323
})

src/components/Slider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const Slider: FC<BaseSliderProps> = props => {
4141
const colorRef = useRef(color);
4242

4343
const getValue = (c: Color) => {
44-
return type === 'hue' ? c.getHue() : c.getAlpha() * 100;
44+
return type === 'hue' ? c.getHue() : c.a * 100;
4545
};
4646

4747
const onDragChange = useEvent((offsetValue: TransformOffset) => {

tests/index.test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,13 @@ describe('ColorPicker', () => {
421421
color={value}
422422
onChange={(color, type) => {
423423
onChange(color, type);
424+
425+
let passedColor = color;
424426
if (type !== 'alpha') {
425427
// bad case, color should be immutable
426-
color.setAlpha(1);
428+
passedColor = new Color(color.setA(1));
427429
}
428-
setValue(color);
430+
setValue(passedColor);
429431
}}
430432
/>
431433
</>

0 commit comments

Comments
 (0)