Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283// A publication's own colors, from `site.standard.publication.basicTheme`.//// The lexicon requires four colors, each one an open union whose only// documented member is `site.standard.theme.color#rgb`. Records are// third-party data, so every field is checked here: a malformed color costs// the theme, never detection.//// A theme is only used when its two text colors are legible against each// other. The rest of the popup keeps the extension's palette either way; this// is the publication's masthead, not a skin for the whole surface.
import type { PublicationTheme, Rgb } from './types'
const RGB_TYPE = 'site.standard.theme.color#rgb'
/** * WCAG AA for normal text. The masthead name is 15px semibold, below the * "large text" exemption, so this is the threshold that applies to it. */export const MIN_CONTRAST = 4.5
function component(value: unknown): number | undefined { return typeof value === 'number' && Number.isInteger(value) && value >= 0 && value <= 255 ? value : undefined}
/** * One color, from either the tagged union member the lexicon documents or a * loose `{r,g,b}` with no `$type`. The shape is unambiguous, and producers in * the wild omit the tag. */function color(value: unknown): Rgb | undefined { if (!value || typeof value !== 'object') return undefined const raw = value as { $type?: unknown; r?: unknown; g?: unknown; b?: unknown } if (raw.$type !== undefined && raw.$type !== RGB_TYPE) return undefined const r = component(raw.r) const g = component(raw.g) const b = component(raw.b) return r === undefined || g === undefined || b === undefined ? undefined : { r, g, b }}
function channel(value: number): number { const s = value / 255 return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4}
export function luminance(c: Rgb): number { return 0.2126 * channel(c.r) + 0.7152 * channel(c.g) + 0.0722 * channel(c.b)}
/** WCAG contrast, from 1 (identical) to 21 (black on white). */export function contrastRatio(a: Rgb, b: Rgb): number { const la = luminance(a) const lb = luminance(b) const [hi, lo] = la >= lb ? [la, lb] : [lb, la] return (hi + 0.05) / (lo + 0.05)}
export function rgbCss(c: Rgb): string { return `rgb(${c.r} ${c.g} ${c.b})`}
/** * The theme a publication declares, or undefined when any color is missing, * malformed, or unreadable together. All-or-nothing: a partly applied theme * would put unowned colors next to the extension's own. */export function parseTheme(raw: unknown): PublicationTheme | undefined { if (!raw || typeof raw !== 'object') return undefined const value = raw as Record<string, unknown> const background = color(value.background) const foreground = color(value.foreground) const accent = color(value.accent) const accentForeground = color(value.accentForeground) if (!background || !foreground || !accent || !accentForeground) return undefined if (contrastRatio(background, foreground) < MIN_CONTRAST) { console.debug('[substandard] publication theme is not legible; ignoring it') return undefined } return { background, foreground, accent, accentForeground }}