493 lines
23 KiB
Rust
493 lines
23 KiB
Rust
// Colours. Light values come from the original's stylesheet
|
||
// (index CSS, ":root,.sand-1vbcnu7" block). Dark values come from the renderer's appearance primitives.
|
||
|
||
use eframe::egui::Color32;
|
||
|
||
// ── 設計規格:一套字級、行高、間距、圓角,桌面和手機同值(mobile/index.html :root 的
|
||
// --fs-* --lh-read --sp-* --r-*;`the_phone_uses_the_same_design_spec` 比對兩邊)。
|
||
// 依 Claude 的設計原則:定一套字級並守住、卡片樣式按角色用、重複的元素一致。
|
||
// 畫面上的字級、圓角只從這裡拿(`no_new_literal_sizes_radii_or_colours` 看守)。
|
||
|
||
/// 字級(px)。介面用思源黑體,閱讀(回答、頁面)用思源宋體。
|
||
pub mod fs {
|
||
/// 時間、標記、圖表的軸。
|
||
pub const CAPTION: f32 = 12.0;
|
||
/// 說明、次要的字。
|
||
pub const SMALL: f32 = 13.0;
|
||
/// 介面:按鈕、清單、輸入框、設定列。
|
||
pub const UI: f32 = 14.0;
|
||
/// 閱讀:Bot 的回答、頁面內文、輸入框裡你打的字。
|
||
pub const READ: f32 = 16.0;
|
||
/// 小標、對話框標題。
|
||
pub const H3: f32 = 18.0;
|
||
pub const H2: f32 = 22.0;
|
||
pub const H1: f32 = 28.0;
|
||
}
|
||
|
||
/// 閱讀字的行高倍數(中文長文 1.7 讀起來不擠)。
|
||
pub const LH_READ: f32 = 1.7;
|
||
|
||
/// The line height for a size on the scale: reading text at `LH_READ`, headings tighter,
|
||
/// interface text at about 1.45.
|
||
pub fn line_height(size: f32) -> f32 {
|
||
if size >= fs::H1 {
|
||
36.0
|
||
} else if size >= fs::H2 {
|
||
30.0
|
||
} else if size >= fs::H3 {
|
||
26.0
|
||
} else if size >= fs::READ {
|
||
(size * LH_READ).round()
|
||
} else if size >= fs::UI {
|
||
20.0
|
||
} else {
|
||
18.0
|
||
}
|
||
}
|
||
|
||
/// 間距(px):4 的倍數六階。
|
||
pub mod sp {
|
||
pub const S1: f32 = 4.0;
|
||
pub const S2: f32 = 8.0;
|
||
pub const S3: f32 = 12.0;
|
||
pub const S4: f32 = 16.0;
|
||
pub const S5: f32 = 24.0;
|
||
pub const S6: f32 = 32.0;
|
||
}
|
||
|
||
/// 圓角:三種,按角色用。`FULL` 是膠囊和圓(egui 會限在高度的一半)。
|
||
pub mod radius {
|
||
/// 標籤、色塊、小控制項。
|
||
pub const S: u8 = 6;
|
||
/// 輸入框、按鈕、程式碼、提示框、表格外框。
|
||
pub const M: u8 = 10;
|
||
/// 卡片、對話框、面板。
|
||
pub const L: u8 = 14;
|
||
pub const FULL: u8 = 255;
|
||
}
|
||
|
||
#[derive(Clone, Copy)]
|
||
pub struct Palette {
|
||
pub pane: Color32,
|
||
pub sidebar: Color32,
|
||
pub elevated: Color32,
|
||
pub hover: Color32,
|
||
pub active: Color32,
|
||
pub primary: Color32,
|
||
pub text: Color32,
|
||
pub text2: Color32,
|
||
pub text3: Color32,
|
||
pub on_primary: Color32,
|
||
pub bubble_user: Color32,
|
||
pub border: Color32,
|
||
pub focus: Color32,
|
||
pub accent: Color32,
|
||
pub danger: Color32,
|
||
/// 需要你: the one colour for what waits for the person (a request, a newer build).
|
||
pub attention: Color32,
|
||
/// Behind a request's card.
|
||
pub attention_bg: Color32,
|
||
/// The quiet fill behind secondary blocks: filled inputs, list trays, segmented tracks
|
||
/// (the shadcn 'muted' layer — it is what makes groups read as groups).
|
||
pub muted: Color32,
|
||
/// Chart series colours, in fixed order (Atlassian/Carbon practice: a protocol, not a
|
||
/// preference — the same hue always means the same rank of series; max six categories).
|
||
/// Chosen low-saturation so many series never fight, with light/dark variants.
|
||
pub chart_cat: [Color32; 6],
|
||
/// The one faint gridline (value axis only; the category axis never gets one).
|
||
pub grid: Color32,
|
||
/// Up / good (charts, stat tiles). Down and bad is `danger`.
|
||
pub up: Color32,
|
||
}
|
||
|
||
const fn hexa(r: u8, g: u8, b: u8, a: u8) -> Color32 {
|
||
Color32::from_rgba_unmultiplied_const(r, g, b, a)
|
||
}
|
||
|
||
pub const LIGHT: Palette = Palette {
|
||
pane: hexa(0xfa, 0xf9, 0xf6, 0xff),
|
||
sidebar: hexa(0xf2, 0xf0, 0xeb, 0xff),
|
||
elevated: hexa(0xff, 0xff, 0xff, 0xff),
|
||
hover: hexa(0x25, 0x22, 0x1f, 0x0d),
|
||
active: hexa(0x25, 0x22, 0x1f, 0x15),
|
||
primary: hexa(0x25, 0x22, 0x1f, 0xff),
|
||
text: hexa(0x25, 0x22, 0x1f, 0xff),
|
||
text2: hexa(0x25, 0x22, 0x1f, 0xb0),
|
||
text3: hexa(0x25, 0x22, 0x1f, 0x85),
|
||
on_primary: hexa(0xfe, 0xfc, 0xfa, 0xff),
|
||
bubble_user: hexa(0x25, 0x22, 0x1f, 0xff),
|
||
border: hexa(0x25, 0x22, 0x1f, 0x1a),
|
||
focus: hexa(0xc0, 0x62, 0x44, 0xff),
|
||
accent: hexa(0xc0, 0x62, 0x44, 0xff),
|
||
danger: hexa(0xb4, 0x2a, 0x2a, 0xff),
|
||
attention: hexa(0xd9, 0xa4, 0x41, 0xff),
|
||
attention_bg: hexa(0xf6, 0xec, 0xd9, 0xff),
|
||
muted: hexa(0xf0, 0xee, 0xe8, 0xff),
|
||
// One warm family in six hues (the shell stays achromatic; colour is earned).
|
||
chart_cat: [
|
||
hexa(0xc0, 0x62, 0x44, 0xff),
|
||
hexa(0x4f, 0x6f, 0x92, 0xff),
|
||
hexa(0x3e, 0x7c, 0x77, 0xff),
|
||
hexa(0x8a, 0x7a, 0x33, 0xff),
|
||
hexa(0x8a, 0x5a, 0x78, 0xff),
|
||
hexa(0x7a, 0x74, 0x6c, 0xff),
|
||
],
|
||
grid: hexa(0x25, 0x22, 0x1f, 0x10),
|
||
up: hexa(0x2e, 0x7d, 0x3f, 0xff),
|
||
};
|
||
|
||
// From the original's renderer (index.eager-common: appearance primitives dark neutral/0..6 and the
|
||
// g_ alpha table): pane #1f1f1f, elevated #161616, chrome #121212, sidebar #0e0e0e, ink #f5f5f5,
|
||
// primary hover #dbdbdb; translucent tokens are the ink at hover 6%, active 8%, agent bubble 4%,
|
||
// user bubble 18%, text 66/54%, border 10%. Accent blue/0 #1884fc, danger red/0 #fc2c3e.
|
||
// Measured from the original's dark window: chat rgb 19, sidebar 16, bubbles/cards 31–33.
|
||
// Kept as the documented reference of 「紙」dark; the live dark palette comes from `shell`.
|
||
#[allow(dead_code)]
|
||
pub const DARK: Palette = Palette {
|
||
pane: hexa(0x1e, 0x1d, 0x1b, 0xff),
|
||
sidebar: hexa(0x19, 0x18, 0x17, 0xff),
|
||
elevated: hexa(0x26, 0x24, 0x22, 0xff),
|
||
hover: hexa(0xf2, 0xf0, 0xeb, 0x0f),
|
||
active: hexa(0xf2, 0xf0, 0xeb, 0x14),
|
||
primary: hexa(0xf2, 0xf0, 0xeb, 0xff),
|
||
text: hexa(0xf2, 0xf0, 0xeb, 0xff),
|
||
text2: hexa(0xf2, 0xf0, 0xeb, 0xa8),
|
||
text3: hexa(0xf2, 0xf0, 0xeb, 0x8a),
|
||
on_primary: hexa(0x1e, 0x1d, 0x1b, 0xff),
|
||
bubble_user: hexa(0xf2, 0xf0, 0xeb, 0x2e),
|
||
border: hexa(0xf2, 0xf0, 0xeb, 0x1a),
|
||
focus: hexa(0xd8, 0x83, 0x66, 0xff),
|
||
accent: hexa(0xd8, 0x83, 0x66, 0xff),
|
||
danger: hexa(0xe2, 0x6e, 0x6e, 0xff),
|
||
attention: hexa(0xd9, 0xa4, 0x41, 0xff),
|
||
attention_bg: hexa(0x38, 0x31, 0x1f, 0xff),
|
||
muted: hexa(0x26, 0x24, 0x22, 0xff),
|
||
chart_cat: [
|
||
hexa(0xd8, 0x83, 0x66, 0xff),
|
||
hexa(0x7f, 0x9d, 0xc0, 0xff),
|
||
hexa(0x6f, 0xae, 0x9c, 0xff),
|
||
hexa(0xc2, 0xb0, 0x60, 0xff),
|
||
hexa(0xb7, 0x8a, 0xa4, 0xff),
|
||
hexa(0xa0, 0x9a, 0x92, 0xff),
|
||
],
|
||
grid: hexa(0xf2, 0xf0, 0xeb, 0x14),
|
||
up: hexa(0x6f, 0xb9, 0x7d, 0xff),
|
||
};
|
||
|
||
/// A bot's colour as a quiet background (its selected tile, its cards): a little of it over the
|
||
/// page — more on a dark page, where a tenth would not show.
|
||
pub fn mist(col: Color32, p: Palette) -> Color32 {
|
||
let k = if p.pane.r() < 128 { 0.24 } else { 0.10 };
|
||
let mix = |a: u8, b: u8| (a as f32 + (b as f32 - a as f32) * k).round() as u8;
|
||
Color32::from_rgb(mix(p.pane.r(), col.r()), mix(p.pane.g(), col.g()), mix(p.pane.b(), col.b()))
|
||
}
|
||
|
||
/// Concept themes for screenshots only (`ANYBOT_SHOT_PAL=paper|night|ink|moss`, with
|
||
/// `ANYBOT_SHOT_THEME=dark` for the dark one): the shell is generated from three inputs —
|
||
/// one ink, one paper, one accent — the Linear way (98 hand-set colours became 3 inputs).
|
||
/// Surfaces are the paper stepped toward the ink in equal little steps; text is the ink
|
||
/// faded back from full; colour appears only as the accent and as meaning (danger, up).
|
||
/// When a direction is chosen it becomes LIGHT/DARK proper; this stays as the generator seed.
|
||
pub fn shot_palette(name: &str, dark: bool) -> Option<Palette> {
|
||
// (ink, paper, accent, danger, up) — the three inputs, plus the two meanings.
|
||
let (ink, paper, accent, danger, up) = match (name, dark) {
|
||
// 紙: warm ivory page, near-black warm ink, one clay accent (Claude's register).
|
||
("paper", false) => (rgb(0x25, 0x22, 0x1f), rgb(0xfa, 0xf9, 0xf6), rgb(0xc0, 0x62, 0x44), rgb(0xb4, 0x2a, 0x2a), rgb(0x2e, 0x7d, 0x3f)),
|
||
("paper", true) => (rgb(0xf2, 0xf0, 0xeb), rgb(0x1e, 0x1d, 0x1b), rgb(0xd8, 0x83, 0x66), rgb(0xe2, 0x6e, 0x6e), rgb(0x6f, 0xb9, 0x7d)),
|
||
// 夜: Linear's warm near-black, one desaturated indigo, everything else grey.
|
||
("night", false) => (rgb(0x20, 0x21, 0x24), rgb(0xf6, 0xf6, 0xf8), rgb(0x5e, 0x6a, 0xd2), rgb(0xc2, 0x37, 0x37), rgb(0x2c, 0x7a, 0x55)),
|
||
("night", true) => (rgb(0xf0, 0xf0, 0xf2), rgb(0x10, 0x11, 0x12), rgb(0x7c, 0x87, 0xe0), rgb(0xe5, 0x73, 0x73), rgb(0x67, 0xb3, 0x8b)),
|
||
// 墨: pure monochrome, high contrast — the accent is the ink itself (ChatGPT's rule).
|
||
("ink", false) => (rgb(0x0d, 0x0d, 0x0d), rgb(0xf9, 0xf9, 0xf9), rgb(0x0d, 0x0d, 0x0d), rgb(0x9c, 0x22, 0x22), rgb(0x1d, 0x6b, 0x3a)),
|
||
("ink", true) => (rgb(0xf7, 0xf7, 0xf7), rgb(0x0a, 0x0a, 0x0a), rgb(0xf7, 0xf7, 0xf7), rgb(0xe0, 0x60, 0x60), rgb(0x6c, 0xb7, 0x84)),
|
||
// 苔: a green-grey paper, moss accent — the quiet one with a colour temperature.
|
||
("moss", false) => (rgb(0x1f, 0x24, 0x20), rgb(0xf4, 0xf6, 0xf2), rgb(0x4a, 0x7c, 0x59), rgb(0xa9, 0x35, 0x2e), rgb(0x2e, 0x7d, 0x4f)),
|
||
("moss", true) => (rgb(0xec, 0xf0, 0xea), rgb(0x15, 0x18, 0x15), rgb(0x7f, 0xaa, 0x8a), rgb(0xdd, 0x6f, 0x66), rgb(0x76, 0xc0, 0x90)),
|
||
_ => return None,
|
||
};
|
||
Some(build(ink, paper, accent, danger, up, 1.0, dark))
|
||
}
|
||
|
||
const fn rgb(r: u8, g: u8, b: u8) -> Color32 {
|
||
Color32::from_rgb(r, g, b)
|
||
}
|
||
|
||
/// The accents the person can pick (設定 › 一般): name, 中文名, light, dark.
|
||
pub const ACCENTS: [(&str, &str, Color32, Color32); 8] = [
|
||
("clay", "黏土", rgb(0xc0, 0x62, 0x44), rgb(0xd8, 0x83, 0x66)),
|
||
("indigo", "靛藍", rgb(0x5e, 0x6a, 0xd2), rgb(0x7c, 0x87, 0xe0)),
|
||
("moss", "苔綠", rgb(0x4a, 0x7c, 0x59), rgb(0x7f, 0xaa, 0x8a)),
|
||
("teal", "藍綠", rgb(0x2e, 0x7d, 0x77), rgb(0x5f, 0xa8, 0xa0)),
|
||
("rose", "玫紅", rgb(0xb0, 0x44, 0x6e), rgb(0xd6, 0x7e, 0xa0)),
|
||
("amber", "琥珀", rgb(0xb0, 0x7f, 0x2e), rgb(0xd9, 0xa4, 0x41)),
|
||
("slate", "石板", rgb(0x64, 0x74, 0x8b), rgb(0x94, 0xa3, 0xb8)),
|
||
("ink", "墨黑", rgb(0x22, 0x20, 0x1e), rgb(0xe8, 0xe6, 0xe1)),
|
||
];
|
||
|
||
/// The shell from three inputs (Linear's method, in the settings): `tone` 0=cool…1=warm,
|
||
/// `accent` one of `ACCENTS`, `contrast` 0.75柔和…1.35鮮明 (1 = as designed). Surfaces step
|
||
/// from paper toward ink; text is ink stepped back; the chart family rotates the accent's hue.
|
||
pub fn shell(tone: f32, accent: &str, contrast: f32, dark: bool) -> Palette {
|
||
let tone = tone.clamp(0.0, 1.0);
|
||
let c = contrast.clamp(0.6, 1.5);
|
||
// Cool end is Linear's neutral grey; warm end is 「紙」 (the shipped default, tone=1).
|
||
let (paper, ink) = if dark {
|
||
(mix(rgb(0x10, 0x11, 0x13), rgb(0x1e, 0x1d, 0x1b), tone), mix(rgb(0xf1, 0xf1, 0xf3), rgb(0xf2, 0xf0, 0xeb), tone))
|
||
} else {
|
||
(mix(rgb(0xf6, 0xf6, 0xf8), rgb(0xfa, 0xf9, 0xf6), tone), mix(rgb(0x20, 0x21, 0x24), rgb(0x25, 0x22, 0x1f), tone))
|
||
};
|
||
let a = ACCENTS.iter().find(|(n, _, _, _)| *n == accent).unwrap_or(&ACCENTS[0]);
|
||
let (danger, up) = if dark { (rgb(0xe2, 0x6e, 0x6e), rgb(0x6f, 0xb9, 0x7d)) } else { (rgb(0xb4, 0x2a, 0x2a), rgb(0x2e, 0x7d, 0x3f)) };
|
||
build(ink, paper, if dark { a.3 } else { a.2 }, danger, up, c, dark)
|
||
}
|
||
|
||
/// Shared body of `shell` and `shot_palette`: the ladder. `contrast` scales every step of it
|
||
/// (surfaces, hairlines, text fades, chart chroma) — one dial, the whole shell follows.
|
||
fn build(ink: Color32, paper: Color32, accent: Color32, danger: Color32, up: Color32, contrast: f32, dark: bool) -> Palette {
|
||
let step = |k: f32| mix(paper, ink, k * contrast);
|
||
let s = (0.52 * contrast).clamp(0.15, 0.8);
|
||
let six = |l: f32| [hsl_shift(accent, 0.0, s, l), hsl_shift(accent, 52.0, s * 0.8, l), hsl_shift(accent, 110.0, s * 0.77, l), hsl_shift(accent, 190.0, s * 0.87, l), hsl_shift(accent, 265.0, s * 0.77, l), hsl_shift(accent, 325.0, s * 0.23, l)];
|
||
let cat = if dark { six(0.62) } else { six(0.48) };
|
||
Palette {
|
||
pane: paper,
|
||
// In light the ladder steps down from the page toward ink; in dark the sidebar steps
|
||
// down toward black (a dark sidebar is a darker dark, not a lighter one — Linear).
|
||
sidebar: if dark { mix(paper, rgb(0, 0, 0), 0.17 * contrast) } else { step(0.045) },
|
||
elevated: if dark { step(0.10) } else { rgb(0xff, 0xff, 0xff) },
|
||
hover: with(ink, (15.0 * contrast) as u8),
|
||
active: with(ink, (26.0 * contrast) as u8),
|
||
primary: ink,
|
||
text: ink,
|
||
text2: mix(ink, paper, 0.34 / contrast),
|
||
text3: mix(ink, paper, 0.50 / contrast),
|
||
on_primary: paper,
|
||
bubble_user: if dark { with(ink, 46) } else { ink },
|
||
border: with(ink, ((if dark { 26.0 } else { 20.0 }) * contrast) as u8),
|
||
focus: accent,
|
||
accent,
|
||
danger,
|
||
attention: rgb(0xd9, 0xa4, 0x41),
|
||
attention_bg: with(rgb(0xd9, 0xa4, 0x41), ((if dark { 38.0 } else { 26.0 }) * contrast) as u8),
|
||
muted: if dark { step(0.10) } else { step(0.035) },
|
||
chart_cat: cat,
|
||
grid: with(ink, (14.0 * contrast) as u8),
|
||
up,
|
||
}
|
||
}
|
||
|
||
fn with(c: Color32, a: u8) -> Color32 {
|
||
Color32::from_rgba_unmultiplied(c.r(), c.g(), c.b(), a)
|
||
}
|
||
|
||
fn mix(a: Color32, b: Color32, t: f32) -> Color32 {
|
||
let m = |x: u8, y: u8| (x as f32 + (y as f32 - x as f32) * t).round().clamp(0.0, 255.0) as u8;
|
||
Color32::from_rgb(m(a.r(), b.r()), m(a.g(), b.g()), m(a.b(), b.b()))
|
||
}
|
||
|
||
/// The accent turned into the six series colours: hue moved by `dh`, chroma and lightness set
|
||
/// to the quiet chart register (low saturation, one lightness step for the whole set).
|
||
fn hsl_shift(c: Color32, dh: f32, s: f32, l: f32) -> Color32 {
|
||
let (mut h, _, _) = rgb_to_hsl(c);
|
||
h = (h + dh).rem_euclid(360.0);
|
||
hsl_to_rgb(h, s, l)
|
||
}
|
||
|
||
fn rgb_to_hsl(c: Color32) -> (f32, f32, f32) {
|
||
let (r, g, b) = (c.r() as f32 / 255.0, c.g() as f32 / 255.0, c.b() as f32 / 255.0);
|
||
let (mx, mn) = (r.max(g).max(b), r.min(g).min(b));
|
||
let l = (mx + mn) / 2.0;
|
||
let d = mx - mn;
|
||
if d < 1e-6 {
|
||
return (0.0, 0.0, l);
|
||
}
|
||
let s = d / (1.0 - (2.0 * l - 1.0).abs());
|
||
let h = if mx == r {
|
||
60.0 * (((g - b) / d) % 6.0)
|
||
} else if mx == g {
|
||
60.0 * ((b - r) / d + 2.0)
|
||
} else {
|
||
60.0 * ((r - g) / d + 4.0)
|
||
};
|
||
(h.rem_euclid(360.0), s, l)
|
||
}
|
||
|
||
fn hsl_to_rgb(h: f32, s: f32, l: f32) -> Color32 {
|
||
let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
|
||
let x = c * (1.0 - ((h / 60.0) % 2.0 - 1.0).abs());
|
||
let m = l - c / 2.0;
|
||
let (r, g, b) = match (h / 60.0).floor() as i32 {
|
||
0 => (c, x, 0.0),
|
||
1 => (x, c, 0.0),
|
||
2 => (0.0, c, x),
|
||
3 => (0.0, x, c),
|
||
4 => (x, 0.0, c),
|
||
_ => (c, 0.0, x),
|
||
};
|
||
rgb(((r + m) * 255.0).round() as u8, ((g + m) * 255.0).round() as u8, ((b + m) * 255.0).round() as u8)
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn shell_defaults_land_on_the_shipped_paper_palette() {
|
||
// tone=1 (warm) + clay + contrast 1 must reproduce 「紙」 within rounding.
|
||
let s = shell(1.0, "clay", 1.0, false);
|
||
assert_eq!(s.pane, LIGHT.pane);
|
||
assert_eq!(s.text, LIGHT.text);
|
||
assert_eq!(s.accent, LIGHT.accent);
|
||
// The ladder goes the right way: sidebar darker than pane, elevated lighter.
|
||
assert!(s.sidebar.r() < s.pane.r());
|
||
assert!(s.elevated.r() >= s.pane.r());
|
||
// Text hierarchy by brightness, not size: in light mode primary text is the darkest
|
||
// thing on the page, and each rank steps back toward the paper.
|
||
assert!(luma(s.text) < luma(s.text2));
|
||
assert!(luma(s.text2) < luma(s.text3));
|
||
assert!(luma(s.text3) < luma(s.pane));
|
||
// Dark mode is the mirror: text lighter than every surface.
|
||
let d = shell(1.0, "clay", 1.0, true);
|
||
assert!(luma(d.text) > luma(d.pane) + 40.0);
|
||
assert!(luma(d.pane) > luma(d.sidebar));
|
||
}
|
||
|
||
#[test]
|
||
fn contrast_dial_moves_the_whole_shell_monotonically() {
|
||
let soft = shell(1.0, "clay", 0.75, false);
|
||
let mid = shell(1.0, "clay", 1.0, false);
|
||
let sharp = shell(1.0, "clay", 1.35, false);
|
||
// Surfaces step further from the page, hairlines darken, text fades deepen.
|
||
assert!(soft.sidebar.r() > mid.sidebar.r() && mid.sidebar.r() > sharp.sidebar.r());
|
||
assert!(soft.border.a() < mid.border.a() && mid.border.a() < sharp.border.a());
|
||
assert!(luma(soft.text2) > luma(mid.text2) && luma(mid.text2) > luma(sharp.text2));
|
||
// Nothing leaves the valid range at the edges.
|
||
for c in [0.6, 1.5] {
|
||
let p = shell(0.0, "ink", c, true);
|
||
let _ = [p.pane, p.sidebar, p.text2, p.border, p.grid];
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn tone_slides_cool_to_warm_and_accents_are_taken_verbatim() {
|
||
let cool = shell(0.0, "indigo", 1.0, false);
|
||
let warm = shell(1.0, "indigo", 1.0, false);
|
||
// The warm paper's red channel sits above the cool one's; same ink warmth rule.
|
||
assert!(warm.pane.r() as i32 - warm.pane.b() as i32 > cool.pane.r() as i32 - cool.pane.b() as i32);
|
||
assert_eq!(cool.accent, rgb(0x5e, 0x6a, 0xd2));
|
||
assert_eq!(shell(0.5, "moss", 1.0, true).accent, rgb(0x7f, 0xaa, 0x8a));
|
||
// An unknown accent falls back to clay, never panics.
|
||
assert_eq!(shell(0.5, "what", 1.0, false).accent, rgb(0xc0, 0x62, 0x44));
|
||
}
|
||
|
||
/// Perceptual-ish brightness for ordering checks (Rec. 709).
|
||
fn luma(c: Color32) -> f32 {
|
||
0.2126 * c.r() as f32 + 0.7152 * c.g() as f32 + 0.0722 * c.b() as f32
|
||
}
|
||
}
|
||
|
||
/// Whether the palette reads dark. A luma test, not `pane == DARK.pane`: the tone dial moves
|
||
/// the pane's colour, and every light/dark branch in the app must follow it.
|
||
pub fn is_dark(p: Palette) -> bool {
|
||
(0.2126 * p.pane.r() as f32 + 0.7152 * p.pane.g() as f32 + 0.0722 * p.pane.b() as f32) < 128.0
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod dark_tests {
|
||
use super::*;
|
||
#[test]
|
||
fn is_dark_follows_the_tone_dial_not_a_constant() {
|
||
assert!(!is_dark(LIGHT));
|
||
assert!(is_dark(DARK));
|
||
// The coolest light shell (blue-grey pane) is still light, the warmest dark still dark.
|
||
assert!(!is_dark(shell(0.0, "indigo", 1.35, false)));
|
||
assert!(is_dark(shell(1.0, "clay", 0.75, true)));
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod spec_tests {
|
||
use super::*;
|
||
|
||
/// 手機的 :root 用同一套數字(同名的 CSS 變數)。
|
||
#[test]
|
||
fn the_phone_uses_the_same_design_spec() {
|
||
let html = include_str!("../mobile/index.html");
|
||
let root = &html[html.find(":root{").or_else(|| html.find(":root {")).expect(":root block")..];
|
||
let root = &root[..root.find('}').unwrap()];
|
||
let var = |name: &str| -> String {
|
||
let at = root.find(&format!("--{name}:")).unwrap_or_else(|| panic!("mobile :root has no --{name}"));
|
||
root[at + name.len() + 3..].split(';').next().unwrap().trim().to_string()
|
||
};
|
||
for (name, v) in [("fs-caption", fs::CAPTION), ("fs-small", fs::SMALL), ("fs-ui", fs::UI), ("fs-read", fs::READ), ("fs-h3", fs::H3), ("fs-h2", fs::H2), ("fs-h1", fs::H1)] {
|
||
assert_eq!(var(name), format!("{v}px"), "--{name}");
|
||
}
|
||
for (name, v) in [("sp-1", sp::S1), ("sp-2", sp::S2), ("sp-3", sp::S3), ("sp-4", sp::S4), ("sp-5", sp::S5), ("sp-6", sp::S6)] {
|
||
assert_eq!(var(name), format!("{v}px"), "--{name}");
|
||
}
|
||
for (name, v) in [("r-s", radius::S), ("r-m", radius::M), ("r-l", radius::L)] {
|
||
assert_eq!(var(name), format!("{v}px"), "--{name}");
|
||
}
|
||
assert_eq!(var("lh-read"), format!("{LH_READ}"));
|
||
}
|
||
|
||
/// The screens take sizes and corner radii from the spec only, never a number of their own,
|
||
/// and the colours written outside the palette only ever get fewer. (A Spinner's `.size` is
|
||
/// its diameter, not type; `0` radius is "square".)
|
||
#[test]
|
||
fn no_new_literal_sizes_radii_or_colours() {
|
||
const COLOURS_LEFT: usize = 64;
|
||
// Frames drawn as a card (a fill and a border) outside the kit: only ever fewer — a
|
||
// card is the kit's `setting_card` / `rows_card`, and never inside another card.
|
||
const CARDS_LEFT: usize = 13;
|
||
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
|
||
let mut files = vec![dir.join("app.rs")];
|
||
for e in std::fs::read_dir(dir.join("app")).unwrap().flatten() {
|
||
let p = e.path();
|
||
if p.extension().is_some_and(|x| x == "rs") && !p.ends_with("layout_audit.rs") {
|
||
files.push(p);
|
||
}
|
||
}
|
||
let starts_digit = |rest: &str| rest.trim_start().chars().next().is_some_and(|c| c.is_ascii_digit());
|
||
let (mut sizes, mut radii, mut colours, mut cards) = (vec![], vec![], 0usize, 0usize);
|
||
for f in &files {
|
||
let text = std::fs::read_to_string(f).unwrap();
|
||
let kit = f.ends_with("kit.rs");
|
||
for (n, line) in text.lines().enumerate() {
|
||
if !kit && line.contains("Frame::new()") && line.contains(".fill(") && line.contains(".stroke(") {
|
||
cards += 1;
|
||
}
|
||
let at = format!("{}:{}", f.file_name().unwrap().to_string_lossy(), n + 1);
|
||
for pat in [".size(", "FontId::proportional(", "FontId::monospace(", "FontId::new("] {
|
||
for (i, _) in line.match_indices(pat) {
|
||
if starts_digit(&line[i + pat.len()..]) && !line.contains("Spinner") {
|
||
sizes.push(at.clone());
|
||
}
|
||
}
|
||
}
|
||
for pat in ["corner_radius(", "CornerRadius::same("] {
|
||
for (i, _) in line.match_indices(pat) {
|
||
let rest = line[i + pat.len()..].trim_start();
|
||
if starts_digit(rest) && !rest.starts_with("0)") && !rest.starts_with("0.0)") {
|
||
radii.push(at.clone());
|
||
}
|
||
}
|
||
}
|
||
for pat in ["Color32::from_rgb(", "Color32::from_rgba_unmultiplied(", "Color32::from_rgba_premultiplied(", "Color32::from_gray(", "Color32::from_black_alpha(", "Color32::from_white_alpha("] {
|
||
colours += line.matches(pat).count();
|
||
}
|
||
}
|
||
}
|
||
assert!(sizes.is_empty(), "font sizes off the scale (use FONT_*): {sizes:?}");
|
||
assert!(radii.is_empty(), "corner radii off the spec (use R_S / R_M / R_L / R_FULL): {radii:?}");
|
||
assert!(colours <= COLOURS_LEFT, "{colours} colours written outside the palette (was {COLOURS_LEFT}): take them from Palette");
|
||
assert!(cards <= CARDS_LEFT, "{cards} hand-made cards outside kit.rs (was {CARDS_LEFT}): use setting_card / rows_card");
|
||
}
|
||
|
||
#[test]
|
||
fn line_heights_follow_the_scale() {
|
||
assert_eq!(line_height(fs::READ), 27.0);
|
||
assert!(line_height(fs::UI) < line_height(fs::READ));
|
||
assert!(line_height(fs::H1) > line_height(fs::H2) && line_height(fs::H2) > line_height(fs::H3));
|
||
}
|
||
}
|