~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
main
103 lines · 4.4 KB Raw History
import type * as getBrand from '#lib/lexicons/org/atgrp/internal/getBrand.ts';
import type { Answer } from '#lib/data.ts';
import { m } from '#lib/paraglide/messages.js';

/** How the host shows itself, as its operator names it (`getBrand`). */
export type Brand = Answer<typeof getBrand.main>;

/** How the host shows itself when it says nothing: the console's own name, no logo, links, backgrounds or colours. */
export const UNBRANDED: Brand = { links: {}, backgrounds: {}, colors: {} };

/** The host's name, as its operator names it, or the console's own. */
export const siteName = (brand?: Brand) => brand?.name ?? m.site_name();

/** The host's logo, as a page draws it: from the host, at `/img/host`; none where it has none. */
export const siteLogo = (brand?: Brand) => (brand?.logo ? '/img/host' : null);

/** The links to the operator's pages, in the footer's order, as far as set. */
export const siteLinks = (brand?: Brand): { href: string; text: string }[] =>
  [
    { href: brand?.links.home, text: m.site_footer_home() },
    { href: brand?.links.privacyPolicy, text: m.site_footer_privacy() },
    { href: brand?.links.termsOfService, text: m.site_footer_terms() },
    { href: brand?.links.support, text: m.site_footer_support() }
  ].flatMap(({ href, text }) => (href ? [{ href, text }] : []));

/** A colour as the host gives one, and as nothing else is let into a style: #rgb or #rrggbb. */
const HEX = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;

/**
 * The ink of each palette, light then dark: the text on what acts, where
 * white would read worse. The light one is the palette's `--fg`; the dark
 * one is its `--bg`, but paper's, a deeper brown.
 */
const INKS: Record<string, [string, string]> = {
  paper: ['#2a2119', '#23120a'],
  slate: ['#212327', '#121315'],
  neutral: ['#232323', '#131313'],
  sage: ['#212422', '#121412'],
  mauve: ['#242226', '#141314']
};

/**
 * The palette the host is in (`brand.palette`), as the page's
 * `data-palette` names it: only one the console has, and none for its
 * own, paper.
 */
export const paletteOf = (brand?: Brand): string | null => {
  const palette = brand?.palette;
  return palette && palette !== 'paper' && Object.hasOwn(INKS, palette) ? palette : null;
};

/** How bright `hex` is to the eye (WCAG's relative luminance), 0 to 1. */
function luminance(hex: string): number {
  const digits =
    hex.length === 4 ? [...hex.slice(1)].map((d) => d + d) : hex.slice(1).match(/../g)!;
  const [r, g, b] = digits.map((d) => {
    const c = parseInt(d, 16) / 255;
    return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
  });
  return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}

/** What reads better on `hex`, white or `ink`. */
function onColour(hex: string, ink: string): string {
  const white = 1.05 / (luminance(hex) + 0.05);
  const dark = (luminance(hex) + 0.05) / (luminance(ink) + 0.05);
  return white >= dark ? '#fff' : ink;
}

/** The tokens of what acts, from its colour `c` and the text on it, `on`: hover, soft wash and link line mixed from it. */
const acting = (c: string, on: string, dark: boolean) =>
  [
    `--primary: ${c}`,
    `--primary-hover: color-mix(in oklch, ${c}, ${dark ? 'white' : 'black'} 15%)`,
    `--on-primary: ${on}`,
    `--primary-soft: color-mix(in oklch, ${c} ${dark ? 22 : 14}%, var(--panel))`,
    `--link-line: color-mix(in oklch, ${c} 45%, var(--panel))`
  ].join('; ');

/**
 * The styles that make the operator's colour (`brand.colors`) the one that
 * acts, over the tokens, light and dark: nothing when it sets none. Dark,
 * without a colour of its own, is the light one lifted to a lightness
 * dark ink reads on. Only a colour as the host checks one is let in.
 * Three rules, where the tokens have light-dark(): this is written as the
 * page is drawn, past the build that lets older browsers read light-dark().
 */
export function brandStyle(brand?: Brand): string {
  const light = brand?.colors.primary;
  if (!light || !HEX.test(light)) return '';
  const given = brand.colors.primaryDark;
  const [lightInk, darkInk] = INKS[paletteOf(brand) ?? 'paper'];
  const l = acting(light, onColour(light, lightInk), false);
  const d =
    given && HEX.test(given)
      ? acting(given, onColour(given, darkInk), true)
      : acting(`oklch(from ${light} max(l, 0.72) c h)`, darkInk, true);
  return [
    `html:root, html[data-theme='light'] { ${l} }`,
    `@media (prefers-color-scheme: dark) { html:root:not([data-theme='light']) { ${d} } }`,
    `html[data-theme='dark'] { ${d} }`
  ].join('\n');
}