Skip to content

Commit

Permalink
add colorConversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Xutaotaotao committed May 21, 2024
1 parent 5cabc3d commit 41ced09
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@types/react": "^18.0.33",
"@types/react-dom": "^18.0.11",
"antd": "^5.16.4",
"colord": "^2.9.3",
"compressorjs": "^1.2.1",
"cross-env": "^7.0.3",
"gh-pages": "^6.1.1",
Expand Down
3 changes: 2 additions & 1 deletion src/.umi/core/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ if (process.env.NODE_ENV === 'development') {
import React from 'react';

export async function getRoutes() {
const routes = {"imageCompress/singleImageCompress":{"path":"imageCompress/singleImageCompress","id":"imageCompress/singleImageCompress","parentId":"@@/global-layout"},"imageCompress/batchImageCompress":{"path":"imageCompress/batchImageCompress","id":"imageCompress/batchImageCompress","parentId":"@@/global-layout"},"imageCompress/index":{"path":"imageCompress","id":"imageCompress/index","parentId":"@@/global-layout"},"imgFormatConversion":{"path":"imgFormatConversion","id":"imgFormatConversion","parentId":"@@/global-layout"},"jsonFormatting":{"path":"jsonFormatting","id":"jsonFormatting","parentId":"@@/global-layout"},"imageSlicing":{"path":"imageSlicing","id":"imageSlicing","parentId":"@@/global-layout"},"jsonDiff":{"path":"jsonDiff","id":"jsonDiff","parentId":"@@/global-layout"},"imgScan":{"path":"imgScan","id":"imgScan","parentId":"@@/global-layout"},"chrono":{"path":"chrono","id":"chrono","parentId":"@@/global-layout"},"index":{"path":"/","id":"index","parentId":"@@/global-layout"},"home":{"path":"home","id":"home","parentId":"@@/global-layout"},"@@/global-layout":{"id":"@@/global-layout","path":"/","isLayout":true}} as const;
const routes = {"imageCompress/singleImageCompress":{"path":"imageCompress/singleImageCompress","id":"imageCompress/singleImageCompress","parentId":"@@/global-layout"},"imageCompress/batchImageCompress":{"path":"imageCompress/batchImageCompress","id":"imageCompress/batchImageCompress","parentId":"@@/global-layout"},"imageCompress/index":{"path":"imageCompress","id":"imageCompress/index","parentId":"@@/global-layout"},"imgFormatConversion":{"path":"imgFormatConversion","id":"imgFormatConversion","parentId":"@@/global-layout"},"colorConversion":{"path":"colorConversion","id":"colorConversion","parentId":"@@/global-layout"},"jsonFormatting":{"path":"jsonFormatting","id":"jsonFormatting","parentId":"@@/global-layout"},"imageSlicing":{"path":"imageSlicing","id":"imageSlicing","parentId":"@@/global-layout"},"jsonDiff":{"path":"jsonDiff","id":"jsonDiff","parentId":"@@/global-layout"},"imgScan":{"path":"imgScan","id":"imgScan","parentId":"@@/global-layout"},"chrono":{"path":"chrono","id":"chrono","parentId":"@@/global-layout"},"index":{"path":"/","id":"index","parentId":"@@/global-layout"},"home":{"path":"home","id":"home","parentId":"@@/global-layout"},"@@/global-layout":{"id":"@@/global-layout","path":"/","isLayout":true}} as const;
return {
routes,
routeComponents: {
'imageCompress/singleImageCompress': React.lazy(() => import(/* webpackChunkName: "src__pages__imageCompress__singleImageCompress" */'../../../src/pages/imageCompress/singleImageCompress.tsx')),
'imageCompress/batchImageCompress': React.lazy(() => import(/* webpackChunkName: "src__pages__imageCompress__batchImageCompress" */'../../../src/pages/imageCompress/batchImageCompress.tsx')),
'imageCompress/index': React.lazy(() => import(/* webpackChunkName: "src__pages__imageCompress__index" */'../../../src/pages/imageCompress/index.tsx')),
'imgFormatConversion': React.lazy(() => import(/* webpackChunkName: "src__pages__imgFormatConversion" */'../../../src/pages/imgFormatConversion.tsx')),
'colorConversion': React.lazy(() => import(/* webpackChunkName: "src__pages__colorConversion" */'../../../src/pages/colorConversion.tsx')),
'jsonFormatting': React.lazy(() => import(/* webpackChunkName: "src__pages__jsonFormatting" */'../../../src/pages/jsonFormatting.tsx')),
'imageSlicing': React.lazy(() => import(/* webpackChunkName: "src__pages__imageSlicing" */'../../../src/pages/imageSlicing.tsx')),
'jsonDiff': React.lazy(() => import(/* webpackChunkName: "src__pages__jsonDiff" */'../../../src/pages/jsonDiff.tsx')),
Expand Down
1 change: 1 addition & 0 deletions src/assets/img/color.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/layouts/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import JsonDiffSvg from "@/assets/img/jsonDiff.svg";
import imgScanSvg from "@/assets/img/imgScan.svg";
import imgFormatConversion from "@/assets/img/imgFormatConversion.svg";
import timeSvg from "@/assets/img/time.svg";
import colorSvg from "@/assets/img/color.svg";

export const NAV_MAP = [
{
Expand Down Expand Up @@ -55,4 +56,10 @@ export const NAV_MAP = [
icon: <img src={timeSvg} />,
des:'用于监控事物的持续时间'
},
{
itemKey: "/colorConversion",
text: "颜色转换器",
icon: <img src={colorSvg} />,
des:'在不同格式之间转换颜色'
},
];
115 changes: 115 additions & 0 deletions src/pages/colorConversion.tsx
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>
);
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2626,6 +2626,11 @@ color-name@~1.1.4:
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

colord@^2.9.3:
version "2.9.3"
resolved "https://registry.npmmirror.com/colord/-/colord-2.9.3.tgz#4f8ce919de456f1d5c1c368c307fe20f3e59fb43"
integrity sha512-jeC1axXpnb0/2nn/Y1LPuLdgXBLH7aDcHu4KEKfqw3CUhX7ZpfBSlPKyqXE6btIgEzfWtrX3/tyBCaCvXvMkOw==

commander@^11.0.0, commander@^11.1.0:
version "11.1.0"
resolved "https://registry.npmmirror.com/commander/-/commander-11.1.0.tgz#62fdce76006a68e5c1ab3314dc92e800eb83d906"
Expand Down

0 comments on commit 41ced09

Please sign in to comment.