Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

1.4 KB
Raw Blame
import { expect } from "vite-plus/test";
import { themeColorToHex, toCanonicalThemeColor } from "./themePalette";

export function asHex(value: string): string {
  const hex = themeColorToHex(value);

  if (!hex) throw new Error(`Expected a theme color, received ${value}`);

  return hex.slice(0, 7);
}

export function canonical(value: string): string {
  const color = toCanonicalThemeColor(value);

  if (!color) throw new Error(`Expected a theme color, received ${value}`);

  return color;
}

export function expectThemeColors(
  colors: Readonly<Record<string, string>>,
  expected: Readonly<Record<string, string>>,
): void {
  for (const [role, value] of Object.entries(expected)) {
    expect(asHex(colors[role]!)).toBe(value);
  }
}

export function contrastRatio(first: string, second: string): number {
  const toRgb = (value: string) => {
    const hex = asHex(value).slice(1);

    return [0, 1, 2].map(
      (channel) => Number.parseInt(hex.slice(channel * 2, channel * 2 + 2), 16) / 255,
    );
  };

  const luminance = (value: string) =>
    toRgb(value)
      .map((channel) => (channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4))
      .reduce((sum, channel, index) => sum + channel * [0.2126, 0.7152, 0.0722][index]!, 0);

  const lighter = Math.max(luminance(first), luminance(second));
  const darker = Math.min(luminance(first), luminance(second));

  return (lighter + 0.05) / (darker + 0.05);
}