Files
shadcn-ui/apps/v4/lib/colors.ts
shadcn 1aa35048a5 feat: v4 updates (#7499)
* feat(v4): update home page

* fix

* fix: cards

* feat(v4): charts page

* feat: update pages

* feat: colors

* fix

* feat: add docs

* feat: mdx work

* fix

* fix

* fix: sidebar

* fix: lint

* feat: updates

* feat: update components

* feat: fix docs

* fix: responsive

* feat: implement cmdk

* fix: update navigation menu demo

* fix: code style

* fix: themes

* feat: implement blocks page

* fix: docs config

* refactor

* fix: outputFileTracingIncludes

* fix

* fix: output

* fix

* fix: registry

* refactor: move docs

* debug: docs

* debug

* revert

* fix: mjs

* deps: pin fumadocs

* debug

* fix: downgrade next

* fix: index page

* refactor: move mdx components

* fix: remove copy button

* fix

* was it zod

* yes it was

* remove copy page

* fix: color page

* fix: colors page

* fix: meta colors

* fix: copy button

* feat: sync registry

* fix: registry build script

* feat: update port

* feat: clean up examples

* fix

* fix: mobile nav

* fix: blur for mobile

* fix: sidebar nav

* feat: update examples

* fix: build scripts

* feat: update components

* feat: restyle

* fix: types

* fix: styles

* fix: margins

* fix: screenshots

* fix

* feat: update theme

* fix: charts nav

* fix: image

* feat: optimize images

* fix: menu

* fix: card

* fix: border

* check

* feat: implement charts page

* fix: charts

* fix: og images

* feat: extend touch

* fix: static

* fix: sizing

* fix: mobile screenshots

* fix: page nav

* fix

* feat: update favicon

* fix: theme selector

* fix: feedback

* fix: sink

* docs: update

* fix: styles

* chore: update registry

* fix: command

* fix

* fix: minor updates

* fix: typography on smaller devices

* fix: format

* fix: remove unused icon

* feat: update favicon

* fix: typography

* docs: typography page

* fix: steps
2025-05-30 11:35:16 +04:00

97 lines
2.3 KiB
TypeScript

import { z } from "zod"
import { colors } from "@/registry/registry-colors"
const colorSchema = z.object({
name: z.string(),
id: z.string(),
scale: z.number(),
className: z.string(),
hex: z.string(),
rgb: z.string(),
hsl: z.string(),
foreground: z.string(),
oklch: z.string(),
var: z.string(),
})
const colorPaletteSchema = z.object({
name: z.string(),
colors: z.array(colorSchema),
})
export type ColorPalette = z.infer<typeof colorPaletteSchema>
export function getColorFormat(color: Color) {
return {
className: `bg-${color.name}-100`,
hex: color.hex,
rgb: color.rgb,
hsl: color.hsl,
oklch: color.oklch,
var: `--color-${color.name}-${color.scale}`,
}
}
export type ColorFormat = keyof ReturnType<typeof getColorFormat>
export function getColors() {
const tailwindColors = colorPaletteSchema.array().parse(
Object.entries(colors)
.map(([name, color]) => {
if (!Array.isArray(color)) {
return null
}
return {
name,
colors: color.map((color) => {
const rgb = color.rgb.replace(
/^rgb\((\d+),(\d+),(\d+)\)$/,
"$1 $2 $3"
)
return {
...color,
name,
id: `${name}-${color.scale}`,
className: `${name}-${color.scale}`,
var: `--color-${name}-${color.scale}`,
rgb,
hsl: color.hsl.replace(
/^hsl\(([\d.]+),([\d.]+%),([\d.]+%)\)$/,
"$1 $2 $3"
),
oklch: `oklch(${color.oklch.replace(
/^oklch\(([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\)$/,
"$1 $2 $3"
)})`,
foreground: getForegroundFromBackground(rgb),
}
}),
}
})
.filter(Boolean)
)
return tailwindColors
}
export type Color = ReturnType<typeof getColors>[number]["colors"][number]
function getForegroundFromBackground(rgb: string) {
const [r, g, b] = rgb.split(" ").map(Number)
function toLinear(number: number): number {
const base = number / 255
return base <= 0.04045
? base / 12.92
: Math.pow((base + 0.055) / 1.055, 2.4)
}
const luminance =
0.2126 * toLinear(r) + 0.7152 * toLinear(g) + 0.0722 * toLinear(b)
return luminance > 0.179 ? "#000" : "#fff"
}