From 17c32ffa86e2c34899df6734064b8d1d804b4288 Mon Sep 17 00:00:00 2001 From: v1rtl Date: Wed, 4 Dec 2024 00:08:12 +0200 Subject: [PATCH] move out unused color utils --- components/src/index.ts | 1 + components/src/tokens/color.ts | 31 ------------------ .../components/PaletteModal/PaletteModal.tsx | 4 +-- docs/src/utils/color.ts | 32 +++++++++++++++++++ 4 files changed, 34 insertions(+), 34 deletions(-) create mode 100644 docs/src/utils/color.ts diff --git a/components/src/index.ts b/components/src/index.ts index f27dac72..f4dfbb85 100644 --- a/components/src/index.ts +++ b/components/src/index.ts @@ -5,5 +5,6 @@ export * from './icons' export { tokens, baseTheme, lightTheme, darkTheme } from './tokens' export type { DefaultTheme, EmptyObject, Accent, Mode, ReactNodeNoStrings } from './types' export type { Hue, Colors, Space } from './tokens' +export type { RawColor } from './tokens/color' export * from './css/theme.css' export * from './tokens/color' diff --git a/components/src/tokens/color.ts b/components/src/tokens/color.ts index 38573716..7cb84d0d 100644 --- a/components/src/tokens/color.ts +++ b/components/src/tokens/color.ts @@ -355,37 +355,6 @@ export type Color = PaletteColor | AdditionalColor | Gradient export const rawColorToRGB = (color: RawColor): string => `rgb(${color.join(', ')})` -export const rawColorToRGBA = (color: RawColor, opacity = 1): string => - `rgba(${[...color, opacity].join(', ')})` - -export const rawColorToHex = (color: RawColor): string => { - return `#${color.map(c => c.toString(16)).join('')}` -} - -export const rawColorToHSL = ([r, g, b]: RawColor): string => { - r /= 255 - g /= 255 - b /= 255 - const l = Math.max(r, g, b) - const s = l - Math.min(r, g, b) - const h = s - ? l === r - ? (g - b) / s - : l === g - ? 2 + (b - r) / s - : 4 + (r - g) / s - : 0 - const rawHsl = [ - 60 * h < 0 ? 60 * h + 360 : 60 * h, - 100 * (s ? (l <= 0.5 ? s / (2 * l - s) : s / (2 - (2 * l - s))) : 0), - (100 * (2 * l - s)) / 2, - ] - - return `hsl(${rawHsl[0].toFixed(0)}, ${rawHsl[1].toFixed( - 0, - )}%, ${rawHsl[2].toFixed(0)}%)` -} - const convertMapColors = ( map: { [key in T]: RawColor }, converter: (color: RawColor) => string, diff --git a/docs/src/components/PaletteModal/PaletteModal.tsx b/docs/src/components/PaletteModal/PaletteModal.tsx index 7bef61b4..102c25f7 100644 --- a/docs/src/components/PaletteModal/PaletteModal.tsx +++ b/docs/src/components/PaletteModal/PaletteModal.tsx @@ -8,12 +8,10 @@ import { Dialog, RecordItem, modeVars, - rawColorToHSL, - rawColorToHex, rawColorToRGB, - rawColorToRGBA, } from '@ensdomains/thorin' import { match, P } from 'ts-pattern' +import { rawColorToHex, rawColorToHSL, rawColorToRGBA } from '~/utils/color' const capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1) diff --git a/docs/src/utils/color.ts b/docs/src/utils/color.ts new file mode 100644 index 00000000..64ff7977 --- /dev/null +++ b/docs/src/utils/color.ts @@ -0,0 +1,32 @@ +import type { RawColor } from '@ensdomains/thorin' + +export const rawColorToRGBA = (color: RawColor, opacity = 1): string => + `rgba(${[...color, opacity].join(', ')})` + +export const rawColorToHex = (color: RawColor): string => { + return `#${color.map(c => c.toString(16)).join('')}` +} + +export const rawColorToHSL = ([r, g, b]: RawColor): string => { + r /= 255 + g /= 255 + b /= 255 + const l = Math.max(r, g, b) + const s = l - Math.min(r, g, b) + const h = s + ? l === r + ? (g - b) / s + : l === g + ? 2 + (b - r) / s + : 4 + (r - g) / s + : 0 + const rawHsl = [ + 60 * h < 0 ? 60 * h + 360 : 60 * h, + 100 * (s ? (l <= 0.5 ? s / (2 * l - s) : s / (2 - (2 * l - s))) : 0), + (100 * (2 * l - s)) / 2, + ] + + return `hsl(${rawHsl[0].toFixed(0)}, ${rawHsl[1].toFixed( + 0, + )}%, ${rawHsl[2].toFixed(0)}%)` +}