Home / File/ colors.ts — ui Source File

colors.ts — ui Source File

Architecture documentation for colors.ts, a typescript file in the ui codebase. 2 imports, 0 dependents.

File typescript ComponentRegistry Styles 2 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  955bc1fa_55c7_f589_a22c_805b535067fd["colors.ts"]
  6802ce19_522d_e5fb_e458_8826d9f6952e["zod"]
  955bc1fa_55c7_f589_a22c_805b535067fd --> 6802ce19_522d_e5fb_e458_8826d9f6952e
  ce470dae_2d85_258b_06b5_4a4159c2eb08["registry-colors"]
  955bc1fa_55c7_f589_a22c_805b535067fd --> ce470dae_2d85_258b_06b5_4a4159c2eb08
  style 955bc1fa_55c7_f589_a22c_805b535067fd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

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(),
})

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,
  }
}

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}`,
              rgb,
              hsl: color.hsl.replace(
                /^hsl\(([\d.]+),([\d.]+%),([\d.]+%)\)$/,
                "$1 $2 $3"
              ),
              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"
}

Subdomains

Dependencies

  • registry-colors
  • zod

Frequently Asked Questions

What does colors.ts do?
colors.ts is a source file in the ui codebase, written in typescript. It belongs to the ComponentRegistry domain, Styles subdomain.
What functions are defined in colors.ts?
colors.ts defines 3 function(s): getColorFormat, getColors, getForegroundFromBackground.
What does colors.ts depend on?
colors.ts imports 2 module(s): registry-colors, zod.
Where is colors.ts in the architecture?
colors.ts is located at deprecated/www/lib/colors.ts (domain: ComponentRegistry, subdomain: Styles, directory: deprecated/www/lib).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free