//! Look (assets/design/shared/DECISIONS.md "Look"): every app's papers, inks, faces and sizes, one definition each, //! read from the kit (`kits/look/look.json`). Words' papers and inks are its own writing kit's //! (`kits/notes/writing.json`, [crate::rules::notes_writing]); Days', Story's, Comms' and Secrets' are here. A paper has a light //! and a dark half; an ink's light values are laid over a half whose ground is light and its dark values over a dark //! one, so a paper that stays light in a dark phone (Days' and Story's Grey) keeps its light ink. use std::collections::BTreeMap; use std::sync::OnceLock; use serde::{Deserialize, Serialize}; use crate::rules::notes_writing; /// One colour per token, as `#RRGGBB`. pub type Tokens = BTreeMap; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct FaceFile { pub file: String, pub weight: u16, #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub italic: bool, /// A variable face: one file draws every weight on its `wght` axis. #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub variable: bool, } /// A bundled typeface, openly licensed. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Face { pub id: String, pub family: String, pub files: Vec, pub licence: String, pub licence_file: String, /// Where the files stand in the repository (each phone's build copies them from there). pub dir: String, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Paper { pub id: String, pub light: Tokens, pub dark: Tokens, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Ink { pub id: String, pub light: Tokens, pub dark: Tokens, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Defaults { pub light: String, pub dark: String, pub ink: String, pub face: String, pub size: f64, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct AppLook { /// The face `own` names. pub own: String, /// The face settings rows the choices sync in: the light row (paper, ink, face, size) and the dark row (its paper). pub rows: [String; 2], /// The token an ink's swatch is drawn in. pub accent: String, /// `points` (Words: the text size itself) or `scale` (a share of the app's own sizes). pub sizes: String, #[serde(default)] pub tokens: Vec, #[serde(default)] pub papers: Vec, #[serde(default)] pub inks: Vec, #[serde(default)] pub defaults: Option, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Step { pub id: String, pub value: f64, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Kit { pub format_version: u32, #[serde(default)] pub about: String, pub faces: Vec, /// The face choices every app offers, in order; `own` is the app's own face. pub choices: Vec, pub apps: BTreeMap, /// The sizes of an app whose size is a `scale`. pub scale: Vec, } /// The apps that have a Look, in the order the work goes. pub const APPS: [&str; 8] = ["notes", "days", "messages", "story", "keys", "mail", "data", "sites"]; /// The kit's look data. pub fn kit() -> &'static Kit { static KIT: OnceLock = OnceLock::new(); KIT.get_or_init(|| serde_json::from_str(include_str!("../kits/look/look.json")).expect("the kit's look data reads")) } /// One app's look, with Words' papers and inks filled from its writing kit. pub fn app(id: &str) -> Option<&'static AppLook> { static APPS_FILLED: OnceLock> = OnceLock::new(); APPS_FILLED .get_or_init(|| kit().apps.iter().map(|(id, look)| (id.clone(), if id == "notes" { words(look) } else { look.clone() })).collect()) .get(id) } fn tokens_of(colours: ¬es_writing::Colours) -> Tokens { serde_json::from_value(serde_json::to_value(colours).expect("colours are JSON")).expect("colours are tokens") } /// Words' look from its writing kit: its themes are the papers, its inks the inks (each ink's own values on Print). fn words(look: &AppLook) -> AppLook { let writing = notes_writing::writing(); let papers: Vec = writing.themes.iter().map(|t| Paper { id: t.id.clone(), light: tokens_of(&t.light), dark: tokens_of(&t.dark) }).collect(); let inks = writing .inks .iter() .map(|ink| { let pair = |c: ¬es_writing::InkColours| Tokens::from([("accent".into(), c.accent.clone()), ("selection".into(), c.selection.clone())]); Ink { id: ink.id.clone(), light: pair(&ink.light), dark: pair(&ink.dark) } }) .collect(); let d = &writing.defaults; AppLook { tokens: papers.first().map(|p| p.light.keys().cloned().collect()).unwrap_or_default(), papers, inks, defaults: Some(Defaults { light: d.light_theme.clone(), dark: d.dark_theme.clone(), ink: d.ink.clone(), face: "own".into(), size: d.font_size }), ..look.clone() } } impl AppLook { pub fn defaults(&self) -> &Defaults { self.defaults.as_ref().expect("every app's look names its defaults") } pub fn paper(&self, id: &str) -> Option<&Paper> { self.papers.iter().find(|p| p.id == id) } pub fn ink(&self, id: &str) -> Option<&Ink> { self.inks.iter().find(|i| i.id == id) } /// The face a choice names: `own` is the app's own. pub fn face(&self, choice: &str) -> Option<&'static Face> { if !kit().choices.iter().any(|c| c == choice) { return None; } let id = if choice == "own" { self.own.as_str() } else { choice }; kit().faces.iter().find(|f| f.id == id) } /// The sizes offered: Words' points across its range, or the kit's scale. pub fn size_steps(&self) -> Vec<(f64, Option)> { if self.sizes == "points" { let r = ¬es_writing::writing().font_size; let mut out = Vec::new(); let mut v = r.min; while v <= r.max + 1e-9 { out.push((v, None)); v += r.step; } out } else { kit().scale.iter().map(|s| (s.value, Some(s.id.clone()))).collect() } } pub fn offers_size(&self, size: f64) -> bool { self.size_steps().iter().any(|(v, _)| (v - size).abs() < 1e-6) } /// [paper]'s [dark] half written in [ink]. Words lays its ink as its writing kit says (Print's own values). pub fn inked(&self, paper: &Paper, ink: &Ink, dark: bool) -> Tokens { if self.sizes == "points" { if let (Some(theme), Some(own)) = (notes_writing::theme(&paper.id), notes_writing::ink(&ink.id)) { return tokens_of(¬es_writing::inked(theme, own, dark)); } } let mut out = if dark { paper.dark.clone() } else { paper.light.clone() }; let ground = out.get("ground").map(String::as_str).unwrap_or("#FFFFFF"); let values = if is_light(ground) { &ink.light } else { &ink.dark }; out.extend(values.clone()); out } } /// The relative luminance of `#RRGGBB` (WCAG 2). pub fn luminance(hex: &str) -> f64 { let h = hex.trim_start_matches('#'); let channel = |i: usize| { let c = u8::from_str_radix(h.get(i..i + 2).unwrap_or("00"), 16).unwrap_or(0) as f64 / 255.0; if c <= 0.03928 { c / 12.92 } else { ((c + 0.055) / 1.055).powf(2.4) } }; 0.2126 * channel(0) + 0.7152 * channel(2) + 0.0722 * channel(4) } /// The contrast between two colours, 1 to 21. pub fn contrast(a: &str, b: &str) -> f64 { let (x, y) = (luminance(a), luminance(b)); (x.max(y) + 0.05) / (x.min(y) + 0.05) } /// A ground light enough that dark words stand on it. pub fn is_light(hex: &str) -> bool { luminance(hex) > 0.18 } #[cfg(test)] #[path = "look_tests.rs"] mod tests;