The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | EMBER_THEME, |
| 5 | GROVE_THEME, |
| 6 | IRIS_THEME, |
| 7 | OCEAN_THEME, |
| 8 | T3_CHAT_THEME, |
| 9 | themeColorToHex, |
| 10 | type ThemeColors, |
| 11 | } from "../../themePalette"; |
| 12 | import { clerkAppearance } from "./clerkAppearance"; |
| 13 | |
| 14 | function contrastRatio(first: string, second: string): number { |
| 15 | const toRgb = (value: string) => { |
| 16 | const hex = themeColorToHex(value)?.slice(1, 7); |
| 17 | if (!hex) throw new Error(`Expected a theme color, received ${value}`); |
| 18 | return [0, 1, 2].map( |
| 19 | (channel) => Number.parseInt(hex.slice(channel * 2, channel * 2 + 2), 16) / 255, |
| 20 | ); |
| 21 | }; |
| 22 | const luminance = (value: string) => |
| 23 | toRgb(value) |
| 24 | .map((channel) => (channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4)) |
| 25 | .reduce((sum, channel, index) => sum + channel * [0.2126, 0.7152, 0.0722][index]!, 0); |
| 26 | const lighter = Math.max(luminance(first), luminance(second)); |
| 27 | const darker = Math.min(luminance(first), luminance(second)); |
| 28 | return (lighter + 0.05) / (darker + 0.05); |
| 29 | } |
| 30 | |
| 31 | function mixThemeColors(first: string, second: string, firstWeight: number): string { |
| 32 | const channels = [first, second].map((value) => { |
| 33 | const hex = themeColorToHex(value)?.slice(1, 7); |
| 34 | if (!hex) throw new Error(`Expected a theme color, received ${value}`); |
| 35 | return [0, 1, 2].map((channel) => Number.parseInt(hex.slice(channel * 2, channel * 2 + 2), 16)); |
| 36 | }); |
| 37 | const mixed = channels[0]!.map((channel, index) => |
| 38 | Math.round(channel * firstWeight + channels[1]![index]! * (1 - firstWeight)), |
| 39 | ); |
| 40 | return `#${mixed.map((channel) => channel.toString(16).padStart(2, "0")).join("")}`; |
| 41 | } |
| 42 | |
| 43 | const builtInThemeModes = [ |
| 44 | T3_CHAT_THEME, |
| 45 | GROVE_THEME, |
| 46 | OCEAN_THEME, |
| 47 | EMBER_THEME, |
| 48 | IRIS_THEME, |
| 49 | ].flatMap((theme) => |
| 50 | [theme.colors, theme.variants?.dark].filter((colors): colors is ThemeColors => !!colors), |
| 51 | ); |
| 52 | |
| 53 | describe("clerkAppearance", () => { |
| 54 | it.each(builtInThemeModes)("keeps Clerk text readable across a built-in palette", (colors) => { |
| 55 | expect(clerkAppearance.variables).toMatchObject({ |
| 56 | colorForeground: "var(--contrast-foreground)", |
| 57 | colorBackground: "var(--card)", |
| 58 | colorMutedForeground: "var(--contrast-muted-foreground)", |
| 59 | colorMuted: "color-mix(in srgb, var(--card) 98%, var(--contrast-foreground))", |
| 60 | colorInputForeground: "var(--contrast-foreground)", |
| 61 | colorInput: "var(--secondary)", |
| 62 | colorPrimary: "var(--update-foreground)", |
| 63 | colorPrimaryForeground: "var(--card)", |
| 64 | }); |
| 65 | const mutedSurface = mixThemeColors(colors.surface, colors.text, 0.98); |
| 66 | |
| 67 | expect(contrastRatio(colors.text, colors.surface)).toBeGreaterThanOrEqual(4.5); |
| 68 | expect(contrastRatio(colors.mutedForeground, mutedSurface)).toBeGreaterThanOrEqual(4.5); |
| 69 | expect(contrastRatio(colors.text, colors.secondary)).toBeGreaterThanOrEqual(4.5); |
| 70 | expect(contrastRatio(colors.updateForeground, colors.surface)).toBeGreaterThanOrEqual(4.5); |
| 71 | expect(contrastRatio(colors.errorForeground, colors.surface)).toBeGreaterThanOrEqual(4.5); |
| 72 | expect(contrastRatio(colors.warningForeground, colors.surface)).toBeGreaterThanOrEqual(4.5); |
| 73 | }); |
| 74 | }); |