|
| 1 | +//! Green-lock palette asserts. |
| 2 | +//! |
| 3 | +//! Split out of [`crate::lock_proof`] and [`crate::lock_v2`] the same way |
| 4 | +//! [`crate::splash_chrome`] was extracted from lock boards: the signed accent |
| 5 | +//! is banner green `#1F4945`, and those files must stay at their source-policy |
| 6 | +//! line-count baseline. Historical violet `#A78BFA`, wash `#221A38`, and gold |
| 7 | +//! `#C9A95C` are never painted. |
| 8 | +
|
| 9 | +use crate::lock_proof::{LockFrame, lock_scene_ids, render_lock_scene}; |
| 10 | +use crate::lock_v2::{LOCK_V2_NARROW_IDS, LOCK_V2_WIDE_IDS, render_lock_v2_scene}; |
| 11 | +use ratatui::buffer::Buffer; |
| 12 | +use ratatui::style::Color; |
| 13 | + |
| 14 | +const SIZES: [(u16, u16); 2] = [(40, 12), (120, 40)]; |
| 15 | + |
| 16 | +/// Mint, navy, historical lock violet, retired wash, thinking gold — SGR tuples. |
| 17 | +const BANNED_ANSI: [&str; 7] = [ |
| 18 | + "0;245;212", |
| 19 | + "26;51;48", |
| 20 | + "0;255;163", |
| 21 | + "10;22;40", |
| 22 | + "167;139;250", |
| 23 | + "34;26;56", |
| 24 | + "201;169;92", |
| 25 | +]; |
| 26 | + |
| 27 | +/// `#A78BFA` violet, `#221A38` wash, `#C9A95C` gold. |
| 28 | +const BANNED_RGB: [(u8, u8, u8); 3] = [(167, 139, 250), (34, 26, 56), (201, 169, 92)]; |
| 29 | + |
| 30 | +const RETIRED: [Color; 3] = [ |
| 31 | + Color::Rgb(167, 139, 250), |
| 32 | + Color::Rgb(34, 26, 56), |
| 33 | + Color::Rgb(201, 169, 92), |
| 34 | +]; |
| 35 | + |
| 36 | +const ROUNDED: &[char] = &['╭', '╮', '╰', '╯']; |
| 37 | + |
| 38 | +pub(crate) fn is_retired_rgb(r: u8, g: u8, b: u8) -> bool { |
| 39 | + BANNED_RGB.contains(&(r, g, b)) |
| 40 | +} |
| 41 | + |
| 42 | +pub(crate) fn is_hairline_box_edge(row: &str) -> bool { |
| 43 | + row.contains('─') && (row.contains('╭') || row.contains('╰')) |
| 44 | +} |
| 45 | + |
| 46 | +fn row_text(buf: &Buffer, y: u16) -> String { |
| 47 | + (0..buf.area.width) |
| 48 | + .map(|x| buf[(x, y)].symbol().to_string()) |
| 49 | + .collect() |
| 50 | +} |
| 51 | + |
| 52 | +fn count_retired_style(frame: &LockFrame) -> u32 { |
| 53 | + let mut retired = 0u32; |
| 54 | + for y in 0..frame.buffer.area.height { |
| 55 | + for x in 0..frame.buffer.area.width { |
| 56 | + let cell = &frame.buffer[(x, y)]; |
| 57 | + if let Some(Color::Rgb(r, g, b)) = cell.style().fg { |
| 58 | + if is_retired_rgb(r, g, b) { |
| 59 | + retired += 1; |
| 60 | + } |
| 61 | + } |
| 62 | + if let Some(Color::Rgb(r, g, b)) = cell.style().bg { |
| 63 | + if is_retired_rgb(r, g, b) { |
| 64 | + retired += 1; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + retired |
| 70 | +} |
| 71 | + |
| 72 | +fn count_retired_cells(frame: &LockFrame) -> u32 { |
| 73 | + let mut retired = 0u32; |
| 74 | + for y in 0..frame.buffer.area.height { |
| 75 | + for x in 0..frame.buffer.area.width { |
| 76 | + let cell = &frame.buffer[(x, y)]; |
| 77 | + if RETIRED.contains(&cell.fg) || RETIRED.contains(&cell.bg) { |
| 78 | + retired += 1; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + retired |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + use super::*; |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn v1_retired_palette_is_absent() { |
| 91 | + let mut retired = 0u32; |
| 92 | + for id in lock_scene_ids() { |
| 93 | + for size in SIZES { |
| 94 | + let frame = render_lock_scene(id, size.0, size.1).expect(id); |
| 95 | + for banned in BANNED_ANSI { |
| 96 | + assert!( |
| 97 | + !frame.ansi.contains(banned), |
| 98 | + "{id} paints banned color {banned} at {size:?}" |
| 99 | + ); |
| 100 | + } |
| 101 | + retired += count_retired_style(&frame); |
| 102 | + for y in 0..frame.buffer.area.height { |
| 103 | + for x in 0..frame.buffer.area.width { |
| 104 | + if let Some(Color::Rgb(r, g, b)) = frame.buffer[(x, y)].style().bg { |
| 105 | + assert!( |
| 106 | + r == g && g == b, |
| 107 | + "{id} paints a tinted background {r},{g},{b} at {size:?} ({x},{y})" |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + assert_eq!( |
| 115 | + retired, 0, |
| 116 | + "retired violet/wash/gold cells in lock v1 frames" |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn v1_rounded_glyphs_stay_on_hairline_boxes() { |
| 122 | + for id in lock_scene_ids() { |
| 123 | + for size in SIZES { |
| 124 | + let frame = render_lock_scene(id, size.0, size.1).expect(id); |
| 125 | + let buf = &frame.buffer; |
| 126 | + for y in 0..buf.area.height { |
| 127 | + let row = row_text(buf, y); |
| 128 | + for x in 0..buf.area.width { |
| 129 | + let Some(ch) = buf[(x, y)].symbol().chars().next() else { |
| 130 | + continue; |
| 131 | + }; |
| 132 | + if !ROUNDED.contains(&ch) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + assert!( |
| 136 | + is_hairline_box_edge(&row), |
| 137 | + "{id} paints rounded `{ch}` off a hairline box at {size:?} ({x},{y}):\n{row}" |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn v2_retired_palette_is_absent() { |
| 147 | + let mut retired = 0u32; |
| 148 | + for (width, height, ids) in [ |
| 149 | + (120u16, 40u16, LOCK_V2_WIDE_IDS), |
| 150 | + (40u16, 12u16, LOCK_V2_NARROW_IDS), |
| 151 | + ] { |
| 152 | + for id in ids { |
| 153 | + let frame = |
| 154 | + render_lock_v2_scene(id, width, height).unwrap_or_else(|e| panic!("{id}: {e}")); |
| 155 | + retired += count_retired_cells(&frame); |
| 156 | + } |
| 157 | + } |
| 158 | + assert_eq!( |
| 159 | + retired, 0, |
| 160 | + "retired violet/wash/gold cells in lock v2 frames" |
| 161 | + ); |
| 162 | + } |
| 163 | +} |
0 commit comments