From 22755fa60ad32135692ef02aab83bcc419dcd273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Mon, 30 Dec 2024 14:33:11 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84Color=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E4=B8=BA=E6=9B=B4=E5=8A=A0=E7=AE=80=E5=8D=95?= =?UTF-8?q?=E7=9A=84=E5=BD=A2=E5=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ColorStand.module.css | 5 +- src/components/ColorStand.tsx | 90 +++++++++++++--------------- 2 files changed, 42 insertions(+), 53 deletions(-) diff --git a/src/components/ColorStand.module.css b/src/components/ColorStand.module.css index 2294075..a55fe2e 100644 --- a/src/components/ColorStand.module.css +++ b/src/components/ColorStand.module.css @@ -38,14 +38,11 @@ .color_value { display: flex; flex-direction: row; - justify-content: space-between; + justify-content: flex-end; gap: var(--spacing-xxs); font-size: var(--font-size-xs); line-height: var(--font-size-xs); padding-inline: var(--spacing-xxs); - h6 { - font-size: var(--font-size-xs); - } .na_value { display: flex; flex-direction: row; diff --git a/src/components/ColorStand.tsx b/src/components/ColorStand.tsx index 56c84b2..f3f336a 100644 --- a/src/components/ColorStand.tsx +++ b/src/components/ColorStand.tsx @@ -1,18 +1,18 @@ import { Icon } from '@iconify/react/dist/iconify.js'; import { isNil } from 'lodash-es'; -import { useCallback, useEffect, useMemo } from 'react'; +import { useCallback, useEffect, useMemo, useState } from 'react'; import { useCopyToClipboard } from 'react-use'; import NoColor from '../assets/NoColor.svg'; import { useColorFunction } from '../ColorFunctionContext'; import styles from './ColorStand.module.css'; import { NotificationType, useNotification } from './Notifications'; +import { SegmentedControl } from './SegmentedControl'; type ColorValueProps = { - title: string; value: string | null; }; -function ColorValue({ title, value }: ColorValueProps) { +function ColorValue({ value }: ColorValueProps) { const [cpState, copyToClipboard] = useCopyToClipboard(); const { showToast } = useNotification(); const handleCopy = useCallback(() => { @@ -36,7 +36,6 @@ function ColorValue({ title, value }: ColorValueProps) { return (
-
{title}
{isNil(value) ? (
@@ -58,48 +57,34 @@ type ColorStandProps = { export function ColorStand({ title, color }: ColorStandProps) { const { colorFn } = useColorFunction(); - const rgb = useMemo(() => { - try { - const rgbValues = colorFn?.represent_rgb(color); - return `rgb(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]})`; - } catch (e) { - console.error('[Convert RGB]', e); + const [viewColor, setViewColor] = useState<'hex' | 'rgb' | 'hsl' | 'lab' | 'oklch'>('hex'); + const displayColorValue = useMemo(() => { + if (isNil(color)) { + return null; } - return null; - }, [color]); - const hsl = useMemo(() => { - try { - const hslValues = colorFn?.represent_hsl(color); - return `hsl(${hslValues[0].toFixed(2)}, ${(hslValues[1] * 100).toFixed(2)}%, ${( - hslValues[2] * 100 - ).toFixed(2)}%)`; - } catch (e) { - console.error('[Convert HSL]', e); + switch (viewColor) { + case 'hex': + return color; + case 'rgb': { + const rgb = colorFn?.represent_rgb(color); + return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`; + } + case 'hsl': { + const hsl = colorFn?.represent_hsl(color); + return `hsl(${hsl[0]}, ${hsl[1]}%, ${hsl[2]}%)`; + } + case 'lab': { + const lab = colorFn?.represent_lab(color); + return `lab(${lab[0]}, ${lab[1]}, ${lab[2]})`; + } + case 'oklch': { + const oklch = colorFn?.represent_oklch(color); + return `oklch(${oklch[0]}%, ${oklch[1]}, ${oklch[2]})`; + } + default: + return null; } - return null; - }, [color]); - const lab = useMemo(() => { - try { - const labValues = colorFn?.represent_lab(color); - return `lab(${labValues[0].toFixed(2)}, ${labValues[1].toFixed(2)}, ${labValues[2].toFixed( - 2, - )})`; - } catch (e) { - console.error('[Convert LAB]', e); - } - return null; - }, [color]); - const oklch = useMemo(() => { - try { - const oklchValues = colorFn?.represent_oklch(color); - return `oklch(${oklchValues[0].toFixed(2)}, ${oklchValues[1].toFixed( - 2, - )}, ${oklchValues[2].toFixed(2)})`; - } catch (e) { - console.error('[Convert OKLCH]', e); - } - return null; - }, [color]); + }, [viewColor, color]); return (
@@ -112,11 +97,18 @@ export function ColorStand({ title, color }: ColorStandProps) {
- - - - - + setViewColor(value as typeof viewColor)} + /> +
);