| 1 |
|
| 2 |
|
| 3 |
use crate::{Rgb, mix}; |
| 4 |
|
| 5 |
|
| 6 |
#[allow(unused_imports)] |
| 7 |
use crate::DISTINCT; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 24 |
pub enum Emphasis { |
| 25 |
|
| 26 |
Full, |
| 27 |
|
| 28 |
Secondary, |
| 29 |
|
| 30 |
Muted, |
| 31 |
} |
| 32 |
|
| 33 |
impl Emphasis { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#[must_use] |
| 42 |
pub const fn ratio(self) -> f32 { |
| 43 |
match self { |
| 44 |
Self::Full => 0.0, |
| 45 |
Self::Secondary => 0.12, |
| 46 |
Self::Muted => 0.42, |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#[must_use] |
| 57 |
pub const fn suffix(self) -> Option<&'static str> { |
| 58 |
match self { |
| 59 |
Self::Full => None, |
| 60 |
Self::Secondary => Some("-secondary"), |
| 61 |
Self::Muted => Some("-muted"), |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
#[must_use] |
| 67 |
pub fn token(self, token: &str) -> String { |
| 68 |
match self.suffix() { |
| 69 |
Some(suffix) => format!("{token}{suffix}"), |
| 70 |
None => token.to_string(), |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
pub const STEP_FLOOR: f32 = 1.21; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
#[must_use] |
| 117 |
pub fn tonal(base: Rgb, ground: Rgb, ratio: f32) -> Rgb { |
| 118 |
mix(base, ground, ratio.clamp(0.0, 1.0)) |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
#[must_use] |
| 127 |
pub fn emphasized(base: Rgb, ground: Rgb, emphasis: Emphasis) -> Rgb { |
| 128 |
tonal(base, ground, emphasis.ratio()) |
| 129 |
} |
| 130 |
|
| 131 |
#[cfg(test)] |
| 132 |
mod tests { |
| 133 |
use super::*; |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn a_tonal_step_lands_between_its_base_and_its_ground() { |
| 137 |
let ink = Rgb::from_hex("#d8dee9").unwrap(); |
| 138 |
let page = Rgb::from_hex("#2e3440").unwrap(); |
| 139 |
for step in [Emphasis::Full, Emphasis::Secondary, Emphasis::Muted] { |
| 140 |
let out = emphasized(ink, page, step).to_oklab().l; |
| 141 |
assert!( |
| 142 |
out <= ink.to_oklab().l && out >= page.to_oklab().l, |
| 143 |
"{step:?} left the interval between the ink and the page" |
| 144 |
); |
| 145 |
} |
| 146 |
assert_eq!(emphasized(ink, page, Emphasis::Full).to_hex(), ink.to_hex()); |
| 147 |
} |
| 148 |
|
| 149 |
#[test] |
| 150 |
fn tonal_steps_compose_rather_than_compound() { |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
let ink = Rgb::from_hex("#d8dee9").unwrap(); |
| 155 |
let page = Rgb::from_hex("#2e3440").unwrap(); |
| 156 |
let (a, b) = (0.12f32, 0.42f32); |
| 157 |
let twice = tonal(tonal(ink, page, a), page, b); |
| 158 |
let once = tonal(ink, page, a + b - a * b); |
| 159 |
let (x, y) = (twice.tuple(), once.tuple()); |
| 160 |
for (l, r) in [(x.0, y.0), (x.1, y.1), (x.2, y.2)] { |
| 161 |
assert!(l.abs_diff(r) <= 1, "{twice:?} is not {once:?}"); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn a_ratio_outside_the_interval_is_clamped_rather_than_extrapolated() { |
| 167 |
let ink = Rgb::from_hex("#d8dee9").unwrap(); |
| 168 |
let page = Rgb::from_hex("#2e3440").unwrap(); |
| 169 |
assert_eq!(tonal(ink, page, -1.0).to_hex(), ink.to_hex()); |
| 170 |
assert_eq!(tonal(ink, page, 2.0).to_hex(), page.to_hex()); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn a_derived_token_key_is_the_family_plus_the_step() { |
| 175 |
assert_eq!(Emphasis::Muted.token("content"), "content-muted"); |
| 176 |
assert_eq!(Emphasis::Secondary.token("content"), "content-secondary"); |
| 177 |
assert_eq!(Emphasis::Full.token("content"), "content"); |
| 178 |
|
| 179 |
|
| 180 |
assert_eq!(Emphasis::Muted.token("danger"), "danger-muted"); |
| 181 |
} |
| 182 |
} |
| 183 |
|