//! 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};usecrate::rules::notes_writing;/// One colour per token, as `#RRGGBB`.pubtypeTokens = BTreeMap<String, String>;#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pubstructFaceFile {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)]pubstructFace {pub id: String,pub family: String,pub files: Vec<FaceFile>,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)]pubstructPaper {pub id: String,pub light: Tokens,pub dark: Tokens,}#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pubstructInk {pub id: String,pub light: Tokens,pub dark: Tokens,}#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pubstructDefaults {pub light: String,pub dark: String,pub ink: String,pub face: String,pub size: f64,}#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pubstructAppLook {/// 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<String>, #[serde(default)]pub papers: Vec<Paper>, #[serde(default)]pub inks: Vec<Ink>, #[serde(default)]pub defaults: Option<Defaults>,}#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pubstructStep {pub id: String,pub value: f64,}#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pubstructKit {pub format_version: u32, #[serde(default)]pub about: String,pub faces: Vec<Face>,/// The face choices every app offers, in order; `own` is the app's own face.pub choices: Vec<String>,pub apps: BTreeMap<String, AppLook>,/// The sizes of an app whose size is a `scale`.pub scale: Vec<Step>,}/// The apps that have a Look, in the order the work goes.pubconst APPS: [&str; 8] = ["notes", "days", "messages", "story", "keys", "mail", "data", "sites"];/// The kit's look data.pubfn kit() -> &'staticKit {static KIT: OnceLock<Kit> = 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.pubfn app(id: &str) -> Option<&'staticAppLook> {static APPS_FILLED: OnceLock<BTreeMap<String, AppLook>> = 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<Paper> = 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() }}implAppLook {pubfn defaults(&self) -> &Defaults {self.defaults.as_ref().expect("every app's look names its defaults") }pubfn paper(&self, id: &str) -> Option<&Paper> {self.papers.iter().find(|p| p.id == id) }pubfn 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.pubfn face(&self, choice: &str) -> Option<&'staticFace> {if !kit().choices.iter().any(|c| c == choice) {returnNone; }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.pubfn size_steps(&self) -> Vec<(f64, Option<String>)> {ifself.sizes == "points" {let r = ¬es_writing::writing().font_size;letmut out = Vec::new();letmut 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() } }pubfn 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).pubfn inked(&self, paper: &Paper, ink: &Ink, dark: bool) -> Tokens {ifself.sizes == "points" {iflet (Some(theme), Some(own)) = (notes_writing::theme(&paper.id), notes_writing::ink(&ink.id)) {return tokens_of(¬es_writing::inked(theme, own, dark)); } }letmut 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).pubfn 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.pubfn 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.pubfn is_light(hex: &str) -> bool { luminance(hex) > 0.18}#[cfg(test)]#[path = "look_tests.rs"]mod tests;