//! App icons, drawn rather than loaded: each app's sign on a plate split as //! Pinnacle's mark is (`docs/brand/`), by a straight seam slanting from the //! top a little left of the middle to the bottom a little right of it. The //! left half is the lighter, as the mark's gold half is, and both grow //! lighter towards the top. Signs are straight-edged, as the mark is: where //! a circle would be, an octagon. An app without a sign of its own gets its //! title's first letter on a plate of its own colour. use alloc::string::String; use alloc::vec; use alloc::vec::Vec; use pinnacle_gfx::{Canvas, Color, Font, Rect, blend}; use pinnacle_vector::Path; use crate::theme; /// The name that draws Pinnacle's mark alone, with no plate (no app's /// name has an `@`). pub const MARK: &str = "@mark"; /// The start of a view's `` name that shows an icon. const CANVAS: &str = "icon:"; /// The `data-name` of a view's `` showing `name`'s icon, for /// [`Icons::draw_canvas`]; `title` gives the letter of an app with no sign. pub fn canvas_name(name: &str, title: &str) -> String { alloc::format!("{CANVAS}{name}:{title}") } /// A plate's halves, each (bottom, top) as 0x00RRGGBB. #[derive(Clone, Copy)] struct Plate { left: (u32, u32), right: (u32, u32), } /// What a part of a sign is filled with. #[derive(Clone, Copy)] enum Ink { /// White, a little of the plate's colour in it right of the seam. Light, /// The plate itself: a cut through a lighter part. Plate, /// From `bottom` at `y1` to `top` at `y0` (grid units): the mark's gold /// and orange. Shade { bottom: u32, top: u32, y0: f32, y1: f32 }, } const GOLD: (u32, u32) = (0xb8892a, 0xf5dc96); const ORANGE: (u32, u32) = (0xc4410a, 0xfd6012); fn gold() -> Ink { Ink::Shade { bottom: GOLD.0, top: GOLD.1, y0: 4.0, y1: 20.0, } } fn orange(y0: f32, y1: f32) -> Ink { Ink::Shade { bottom: ORANGE.0, top: ORANGE.1, y0, y1, } } fn mix(a: u32, b: u32, t: f32) -> u32 { let t = t.clamp(0.0, 1.0); let mut out = 0; for shift in [0, 8, 16] { let x = ((a >> shift) & 255) as f32; let y = ((b >> shift) & 255) as f32; out |= ((x + (y - x) * t + 0.5) as u32).min(255) << shift; } out } /// A plate from an app's colour, its deep and its bright tone. fn tone(deep: u32, bright: u32) -> Plate { Plate { left: (mix(deep, bright, 0.5), mix(bright, 0xffffff, 0.2)), right: (mix(deep, 0x000000, 0.12), bright), } } /// The colour of an app with no sign, the same every time for its name. fn tone_for(name: &str) -> Plate { const TONES: [(u32, u32); 8] = [ (0x1c4fb0, 0x4f8af0), (0x17804c, 0x42c47f), (0xb3521a, 0xf08a3c), (0xa5335f, 0xe0679a), (0x0b6e7d, 0x2cbfd0), (0xa8322a, 0xec6a4f), (0x5e7a12, 0xa3c93a), (0x8e2f86, 0xd45bc6), ]; let hash = name .bytes() .fold(5381u32, |h, b| h.wrapping_mul(33) ^ u32::from(b)); let (deep, bright) = TONES[hash as usize % TONES.len()]; tone(deep, bright) } fn plate(name: &str) -> Plate { match name { // The Store sells what runs here: the mark's own two halves. "store" => Plate { left: GOLD, right: ORANGE, }, "about" => Plate { left: (0x161e21, 0x2c3a40), right: (0x0b0f11, 0x1d272c), }, "settings" => tone(0x37414a, 0x67737e), "terminal" => tone(0x0f1c1a, 0x2a453f), "calculator" => tone(0x2a2f34, 0x515962), "filer" | "files" => tone(0x12618a, 0x33a0d6), "fien" => tone(0x5e7a12, 0xa3c93a), "clock" => tone(0xa8322a, 0xec6a4f), "messages" => tone(0x17804c, 0x42c47f), "calls" => tone(0x0b6e7d, 0x2cbfd0), "notes" => tone(0xb06f12, 0xeeb43c), "contacts" => tone(0xa5335f, 0xe0679a), "bluetooth" => tone(0x1c4fb0, 0x4f8af0), "paint" => tone(0x8e2f86, 0xd45bc6), "cartopolis" => tone(0x1d6f5c, 0x3cb08f), _ => tone_for(name), } } fn sqrt(v: f32) -> f32 { if v <= 0.0 { return 0.0; } let mut x = f32::from_bits((v.to_bits() >> 1) + 0x1fbd_1df5); for _ in 0..3 { x = 0.5 * (x + v / x); } x } /// A closed polygon, wound the one way every part of a sign is, so that /// parts which overlap add rather than cancel. fn poly(points: &[(f32, f32)]) -> Path { let area: f32 = (0..points.len()) .map(|i| { let (p, q) = (points[i], points[(i + 1) % points.len()]); p.0 * q.1 - q.0 * p.1 }) .sum(); let mut path = Path::new(); let mut ordered: Vec<(f32, f32)> = points.to_vec(); if area < 0.0 { ordered.reverse(); } path.move_to(ordered[0].0, ordered[0].1); for p in &ordered[1..] { path.line_to(p.0, p.1); } path.close(); path } fn rect(x: f32, y: f32, w: f32, h: f32) -> Path { poly(&[(x, y), (x + w, y), (x + w, y + h), (x, y + h)]) } /// An octagon with flat sides at the top, bottom and either side. fn octagon(cx: f32, cy: f32, r: f32) -> Path { // A corner's offsets: r·cos 22.5°, r·sin 22.5°. let (a, b) = (r * 0.923_88, r * 0.382_68); poly(&[ (cx - b, cy - a), (cx + b, cy - a), (cx + a, cy - b), (cx + a, cy + b), (cx + b, cy + a), (cx - b, cy + a), (cx - a, cy + b), (cx - a, cy - b), ]) } /// A straight bar from `a` to `b`, `half` its half thickness, square-ended. fn bar(a: (f32, f32), b: (f32, f32), half: f32) -> Path { let (dx, dy) = (b.0 - a.0, b.1 - a.1); let len = sqrt(dx * dx + dy * dy).max(0.001); let (nx, ny) = (-dy / len * half, dx / len * half); poly(&[ (a.0 + nx, a.1 + ny), (b.0 + nx, b.1 + ny), (b.0 - nx, b.1 - ny), (a.0 - nx, a.1 - ny), ]) } /// Pinnacle's mark (`docs/brand/mark.svg`), its base's middle at (x, y), /// `height` tall. fn spire(x: f32, y: f32, height: f32, y0: f32) -> [(Path, Ink); 2] { let k = height / 90.0; let at = |px: f32, py: f32| (x + px * k, y + py * k); [ ( poly(&[at(0.0, -90.0), at(6.4, 0.0), at(-32.0, 0.0)]), Ink::Shade { bottom: GOLD.0, top: GOLD.1, y0, y1: y, }, ), ( poly(&[at(5.0, -75.94), at(32.0, 0.0), at(10.4, 0.0)]), orange(y0, y), ), ] } /// An app's sign on a grid 24 units square (the whole plate), its parts /// drawn in order; none for an app that has no sign of its own. fn sign(name: &str) -> Option> { use Ink::{Light, Plate as Cut}; let parts = match name { MARK => spire(12.0, 23.0, 22.0, 1.0).to_vec(), "about" => spire(12.0, 18.5, 13.0, 5.5).to_vec(), // A bag with a squared handle, the mark cut into it. "store" => vec![ ( poly(&[ (8.5, 9.5), (8.5, 6.0), (10.0, 4.5), (14.0, 4.5), (15.5, 6.0), (15.5, 9.5), (14.0, 9.5), (14.0, 6.6), (13.4, 6.0), (10.6, 6.0), (10.0, 6.6), (10.0, 9.5), ]), Light, ), (poly(&[(5.0, 9.0), (19.0, 9.0), (18.0, 20.0), (6.0, 20.0)]), Light), (poly(&[(12.0, 11.0), (15.0, 18.0), (9.0, 18.0)]), Cut), ], // Three sliders. "settings" => { let mut parts = Vec::new(); for (y, knob) in [(7.0, 15.0), (12.0, 9.0), (17.0, 13.5)] { parts.push((rect(4.5, y - 0.8, 15.0, 1.6), Light)); parts.push((octagon(knob, y, 2.7), Light)); parts.push((octagon(knob, y, 1.1), Cut)); } parts } // A prompt and a cursor. "terminal" => vec![ ( poly(&[ (5.0, 8.4), (6.7, 6.7), (12.0, 12.0), (6.7, 17.3), (5.0, 15.6), (8.6, 12.0), ]), Light, ), (rect(12.8, 15.2, 6.4, 2.2), gold()), ], // A folder, its front apart from its back. "filer" | "files" => vec![ ( poly(&[ (4.5, 5.5), (10.0, 5.5), (12.0, 7.5), (19.5, 7.5), (19.5, 18.5), (4.5, 18.5), ]), Light, ), (rect(4.5, 9.4, 15.0, 1.0), Cut), ], // A pencil, its point to the bottom left. "fien" => vec![ (bar((8.0, 16.0), (14.6, 9.4), 2.2), Light), (bar((15.5, 8.5), (18.0, 6.0), 2.2), Light), (poly(&[(6.44, 13.56), (10.44, 17.56), (5.0, 19.0)]), Light), ], // An octagonal face, its hands at ten past ten. "clock" => vec![ (octagon(12.0, 12.0, 8.0), Light), (octagon(12.0, 12.0, 6.2), Cut), (bar((12.0, 12.0), (12.0, 7.6), 0.9), Light), (bar((12.0, 12.0), (15.4, 14.0), 0.9), Light), (octagon(12.0, 12.0, 1.4), Light), ], // A speech bubble, three dots cut into it. "messages" => vec![ ( poly(&[ (4.5, 5.5), (19.5, 5.5), (19.5, 16.0), (11.5, 16.0), (7.0, 19.5), (7.5, 16.0), (4.5, 16.0), ]), Light, ), (rect(7.6, 9.9, 1.8, 1.8), Cut), (rect(11.1, 9.9, 1.8, 1.8), Cut), (rect(14.6, 9.9, 1.8, 1.8), Cut), ], // A video camera. "calls" => vec![ (rect(4.0, 7.5, 11.0, 9.0), Light), ( poly(&[(16.0, 11.0), (20.0, 8.0), (20.0, 16.0), (16.0, 13.0)]), Light, ), (rect(6.0, 9.5, 2.0, 2.0), orange(9.5, 11.5)), ], // A page with a corner folded down, lines cut into it. "notes" => vec![ ( poly(&[(6.0, 4.5), (14.5, 4.5), (18.0, 8.0), (18.0, 19.5), (6.0, 19.5)]), Light, ), (poly(&[(14.5, 4.5), (14.5, 8.0), (18.0, 8.0)]), Cut), (rect(8.5, 10.5, 7.0, 1.2), Cut), (rect(8.5, 13.4, 7.0, 1.2), Cut), (rect(8.5, 16.3, 4.5, 1.2), Cut), ], // A person: an octagonal head over squared shoulders. "contacts" => vec![ (octagon(12.0, 8.5, 3.6), Light), ( poly(&[ (5.0, 19.5), (6.5, 14.5), (9.0, 13.0), (15.0, 13.0), (17.5, 14.5), (19.0, 19.5), ]), Light, ), ], // The rune, as straight bars. "bluetooth" => { let points = [ (7.5, 8.5), (16.0, 15.5), (12.0, 19.5), (12.0, 4.5), (16.0, 8.5), (7.5, 15.5), ]; let mut parts: Vec<(Path, Ink)> = points .windows(2) .map(|w| (bar(w[0], w[1], 1.05), Light)) .collect(); for &(x, y) in &points[1..5] { parts.push((octagon(x, y, 1.05), Light)); } parts } // Four keys, = on the mark's orange. "calculator" => vec![ (rect(4.5, 4.5, 6.8, 6.8), Light), (rect(12.7, 4.5, 6.8, 6.8), Light), (rect(4.5, 12.7, 6.8, 6.8), Light), (rect(12.7, 12.7, 6.8, 6.8), orange(12.7, 19.5)), (rect(6.1, 7.3, 3.6, 1.2), Cut), (rect(7.3, 6.1, 1.2, 3.6), Cut), (rect(14.3, 7.3, 3.6, 1.2), Cut), (bar((6.6, 14.8), (9.2, 17.4), 0.6), Cut), (bar((9.2, 14.8), (6.6, 17.4), 0.6), Cut), (rect(14.3, 14.6, 3.6, 1.1), Light), (rect(14.3, 16.6, 3.6, 1.1), Light), ], // A brush, its tip to the bottom left. "paint" => vec![ (bar((11.6, 12.4), (18.5, 5.5), 1.3), Light), (bar((10.0, 14.0), (11.8, 12.2), 2.0), gold()), ( poly(&[(8.5, 12.9), (11.1, 15.5), (8.0, 19.0), (4.5, 19.5), (5.0, 16.0)]), Light, ), ], // A map folded in three, the middle panel in shadow. "cartopolis" => vec![ (poly(&[(4.0, 7.0), (9.4, 5.0), (9.4, 17.0), (4.0, 19.0)]), Light), ( poly(&[(14.6, 7.0), (20.0, 5.0), (20.0, 17.0), (14.6, 19.0)]), Light, ), ( poly(&[(10.2, 5.2), (13.8, 7.0), (13.8, 19.0), (10.2, 17.2)]), gold(), ), ], _ => return None, }; Some(parts) } /// An icon's pixels: colours and their coverage, `size` square. struct Image { size: i32, colors: Vec, alpha: Vec, } /// Where a pixel's middle is against the seam: 0 left of it, 1 right, /// anti-aliased across it. fn right_of_seam(x: f32, y: f32, s: f32) -> f32 { let (ax, bx) = (0.40 * s, 0.60 * s); let (dx, dy) = (bx - ax, s); let d = (dx * y - dy * (x - ax)) / sqrt(dx * dx + dy * dy); (0.5 - d).clamp(0.0, 1.0) } fn render(name: &str, title: &str, size: i32) -> Image { let n = (size * size) as usize; let s = size as f32; let plate = plate(name); // The plate's colour and how far right of the seam, per pixel. let mut base = vec![0u32; n]; let mut right = vec![0f32; n]; for y in 0..size { for x in 0..size { let (px, py) = (x as f32 + 0.5, y as f32 + 0.5); let t = right_of_seam(px, py, s); let down = py / s; let left = mix(plate.left.1, plate.left.0, down); let rightc = mix(plate.right.1, plate.right.0, down); let i = (y * size + x) as usize; base[i] = mix(left, rightc, t); right[i] = t; } } let (mut colors, mut alpha) = if name == MARK { (vec![0u32; n], vec![0u8; n]) } else { let shape = Path::rounded_rect(0.0, 0.0, s, s, s * 0.22); ( base.clone(), pinnacle_vector::mask(&shape, size as usize, size as usize), ) }; let tint = mix(0xffffff, plate.right.1, 0.28); let ink = |ink: Ink, i: usize, y: f32| match ink { Ink::Light => mix(0xffffff, tint, right[i]), Ink::Plate => base[i], Ink::Shade { bottom, top, y0, y1 } => mix(top, bottom, (y / s * 24.0 - y0) / (y1 - y0)), }; let mut put = |coverage: &[u8], ink_of: &dyn Fn(usize, f32) -> u32| { for (i, &c) in coverage.iter().enumerate() { if c == 0 { continue; } let color = ink_of(i, (i as i32 / size) as f32 + 0.5); if alpha[i] == 0 { colors[i] = color; } else { colors[i] = blend(colors[i], color, u32::from(c)); } if name == MARK { alpha[i] = alpha[i].max(c); } } }; match sign(name) { Some(parts) => { for (path, kind) in parts { let scaled = path.transformed(s / 24.0, 0.0, 0.0); let coverage = pinnacle_vector::mask(&scaled, size as usize, size as usize); put(&coverage, &|i, y| ink(kind, i, y)); } } None => { // The title's first letter, white on its plate. let initial: String = title.chars().take(1).collect(); let font = Font::new(theme::body().face(), s * 0.5); let mut letter = vec![0u32; n]; Canvas::new(&mut letter, size as u32, size as u32).text_centered( &font, Rect::new(0, 0, size, size), &initial, Color(0xffffff), ); let coverage: Vec = letter.iter().map(|p| (p & 255) as u8).collect(); put(&coverage, &|i, y| ink(Ink::Light, i, y)); } } Image { size, colors, alpha } } /// Icons drawn so far, by name and size: a home screen draws each again on /// every press. #[derive(Default)] pub struct Icons { drawn: Vec<(String, Image)>, } impl Icons { pub fn new() -> Icons { Icons::default() } /// Draws the icon a view's `` named by [`canvas_name`] shows, in /// the `rect` the view gave it; any other canvas is left as it is. pub fn draw_canvas(&mut self, canvas: &mut Canvas, rect: Rect, data_name: &str) { if let Some(rest) = data_name.strip_prefix(CANVAS) { let (name, title) = rest.split_once(':').unwrap_or((rest, "")); self.draw(canvas, rect, name, title); } } /// Draws `name`'s icon as large as fits in the middle of `rect`; /// `title` gives the letter of an app with no sign of its own. pub fn draw(&mut self, canvas: &mut Canvas, rect: Rect, name: &str, title: &str) { let size = rect.width.min(rect.height); if size <= 0 { return; } let at = match self.drawn.iter().position(|(n, i)| n == name && i.size == size) { Some(at) => at, None => { // Sizes change with the window: keep only the recent. if self.drawn.len() >= 64 { self.drawn.clear(); } self.drawn.push((String::from(name), render(name, title, size))); self.drawn.len() - 1 } }; let image = &self.drawn[at].1; canvas.draw_image( &image.colors, &image.alpha, size, size, rect.x + (rect.width - size) / 2, rect.y + (rect.height - size) / 2, ); } }