-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5cabc3d
commit 41ced09
Showing
6 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { Button, Card, ColorPicker, Form, Input } from "antd"; | ||
import { useState, useEffect, useMemo } from "react"; | ||
import { colord,extend } from "colord"; | ||
import type { Colord } from 'colord'; | ||
import namesPlugin from "colord/plugins/names"; | ||
import hwbPlugin from "colord/plugins/hwb"; | ||
import lchPlugin from "colord/plugins/lch"; | ||
import cmykPlugin from "colord/plugins/cmyk"; | ||
import type { ColorPickerProps, GetProp } from 'antd'; | ||
|
||
type Color = GetProp<ColorPickerProps, 'value'>; | ||
|
||
extend([namesPlugin,hwbPlugin,lchPlugin,cmykPlugin]); | ||
|
||
|
||
export default function ColorConversion() { | ||
const [form] = Form.useForm(); | ||
const [color, setColor] = useState<Color>("#1677ff"); | ||
|
||
const parseColor = (color: any) => { | ||
const colorVal: Colord = colord(color); | ||
const colorValues = { | ||
colorName: colorVal.toName({ closest: true }), | ||
colorHex: colorVal.toHex(), | ||
colorRgb: colorVal.toRgbString(), | ||
colorHsl: colorVal.toHslString(), | ||
colorHwb: colorVal.toHwbString(), | ||
colorLch: colorVal.toLchString(), | ||
colorCmyk: colorVal.toCmykString() | ||
}; | ||
return colorValues; | ||
} | ||
|
||
const handleColorChange = (newColor: any) => { | ||
setColor(newColor) | ||
const colorVal:Colord = colord(newColor.toHexString()); | ||
form.setFieldsValue({ | ||
...parseColor(colorVal) | ||
}) | ||
}; | ||
|
||
const handleInputChange = (e: any) => { | ||
const { value } = e.target; | ||
try { | ||
const colorVal:Colord = colord(value) | ||
if (colorVal.isValid()){ | ||
setColor(colorVal.toHex()); | ||
form.setFieldsValue({ | ||
...parseColor(colorVal) | ||
}) | ||
} | ||
} catch { | ||
|
||
} | ||
|
||
}; | ||
|
||
const bgColor = useMemo<string>( | ||
() => (typeof color === 'string' ? color : color!.toHexString()), | ||
[color], | ||
); | ||
|
||
const btnStyle: React.CSSProperties = { | ||
backgroundColor: bgColor, | ||
width: '100%' | ||
}; | ||
|
||
useEffect(() => { | ||
form.setFieldsValue({ | ||
...parseColor(color) | ||
}) | ||
},[]) | ||
|
||
return ( | ||
<div> | ||
<Card> | ||
<Form | ||
labelCol={{ span: 8 }} | ||
wrapperCol={{ span: 16 }} | ||
style={{ maxWidth: 600 }} | ||
form={form} | ||
initialValues={parseColor(color)} | ||
> | ||
<Form.Item label="颜色拾取器"> | ||
<ColorPicker style={{width:'100%'}} value={color} onChange={handleColorChange} > | ||
<Button type="primary" style={btnStyle}> | ||
</Button> | ||
</ColorPicker> | ||
</Form.Item> | ||
<Form.Item label="Hex" name="colorHex"> | ||
<Input onChange={handleInputChange} /> | ||
</Form.Item> | ||
<Form.Item label="Rgb" name="colorRgb"> | ||
<Input onChange={handleInputChange} /> | ||
</Form.Item> | ||
<Form.Item label="Hsl" name="colorHsl"> | ||
<Input onChange={handleInputChange} /> | ||
</Form.Item> | ||
<Form.Item label="Hwb" name="colorHwb"> | ||
<Input onChange={handleInputChange} /> | ||
</Form.Item> | ||
<Form.Item label="Lch" name="colorLch"> | ||
<Input onChange={handleInputChange} /> | ||
</Form.Item> | ||
<Form.Item label="Cmyk" name="colorCmyk"> | ||
<Input onChange={handleInputChange} /> | ||
</Form.Item> | ||
<Form.Item label="颜色名称" name="colorName"> | ||
<Input onChange={handleInputChange} /> | ||
</Form.Item> | ||
</Form> | ||
</Card> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters