| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use topcoat::{ |
| 6 | context::Cx, |
| 7 | cookie::{Cookie, Cookies, SameSite, cookies, time::Duration}, |
| 8 | }; |
| 9 | |
| 10 | |
| 11 | pub const SCHEME_COOKIE: &str = "scheme"; |
| 12 | |
| 13 | |
| 14 | pub const SCHEME_COOKIE_DAYS: i64 = 365; |
| 15 | |
| 16 | |
| 17 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 18 | pub enum Scheme { |
| 19 | Graphite, |
| 20 | Cobalt, |
| 21 | Oxblood, |
| 22 | Teal, |
| 23 | Plum, |
| 24 | Mono, |
| 25 | Tangerine, |
| 26 | Sage, |
| 27 | Spark, |
| 28 | } |
| 29 | |
| 30 | |
| 31 | pub const ALL: [Scheme; 9] = [ |
| 32 | Scheme::Graphite, |
| 33 | Scheme::Cobalt, |
| 34 | Scheme::Oxblood, |
| 35 | Scheme::Teal, |
| 36 | Scheme::Plum, |
| 37 | Scheme::Mono, |
| 38 | Scheme::Tangerine, |
| 39 | Scheme::Sage, |
| 40 | Scheme::Spark, |
| 41 | ]; |
| 42 | |
| 43 | |
| 44 | pub const DEFAULT: Scheme = Scheme::Sage; |
| 45 | |
| 46 | impl Scheme { |
| 47 | |
| 48 | pub fn id(self) -> &'static str { |
| 49 | match self { |
| 50 | Self::Graphite => "graphite", |
| 51 | Self::Cobalt => "cobalt", |
| 52 | Self::Oxblood => "oxblood", |
| 53 | Self::Teal => "teal", |
| 54 | Self::Plum => "plum", |
| 55 | Self::Mono => "mono", |
| 56 | Self::Tangerine => "tangerine", |
| 57 | Self::Sage => "sage", |
| 58 | Self::Spark => "spark", |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | pub fn from_id(id: &str) -> Option<Self> { |
| 64 | ALL.into_iter().find(|s| s.id() == id) |
| 65 | } |
| 66 | |
| 67 | |
| 68 | pub fn name(self) -> &'static str { |
| 69 | match self { |
| 70 | Self::Graphite => "Graphite & Highlighter", |
| 71 | Self::Cobalt => "Cobalt Ink", |
| 72 | Self::Oxblood => "Oxblood", |
| 73 | Self::Teal => "Teal Ledger", |
| 74 | Self::Plum => "Plum", |
| 75 | Self::Mono => "Monochrome", |
| 76 | Self::Tangerine => "Tangerine", |
| 77 | Self::Sage => "Sage", |
| 78 | Self::Spark => "Spark", |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | |
| 84 | pub fn dots(self) -> [(&'static str, &'static str); 3] { |
| 85 | match self { |
| 86 | |
| 87 | Self::Graphite => [("#eceef2", "#1d2026"), ("#f2b600", "#ffc93c"), ("#ffe08a", "#5c4700")], |
| 88 | Self::Cobalt => [("#eef3fb", "#121a2c"), ("#1f4fd1", "#5b8bff"), ("#dce7ff", "#1d2f5c")], |
| 89 | Self::Oxblood => [("#eeeae8", "#1c1818"), ("#8e1b22", "#d85c63"), ("#f8dcd9", "#4a1d21")], |
| 90 | Self::Teal => [("#e8f0f0", "#121d1c"), ("#0f5e5a", "#3fb3a6"), ("#cdeee9", "#15403b")], |
| 91 | Self::Plum => [("#efeaf3", "#1a1620"), ("#7b2d6e", "#d27ac4"), ("#f1d9ee", "#4a2446")], |
| 92 | Self::Mono => [("#f0f0f0", "#161616"), ("#111111", "#f2f2f2"), ("#e4e4e4", "#2e2e2e")], |
| 93 | Self::Tangerine => [("#eceef0", "#16191c"), ("#f59e0b", "#ffb347"), ("#fde9c4", "#4f3412")], |
| 94 | Self::Sage => [("#e9efe6", "#111211"), ("#567752", "#4a6847"), ("#d9ebd3", "#233024")], |
| 95 | Self::Spark => [("#eeeef0", "#1a1512"), ("#ff8205", "#ff9a2e"), ("#ffe8c7", "#58320a")], |
| 96 | |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | pub fn scheme(cx: &Cx) -> Scheme { |
| 104 | cookies(cx) |
| 105 | .get(SCHEME_COOKIE) |
| 106 | .and_then(|cookie| Scheme::from_id(cookie.value())) |
| 107 | .unwrap_or(DEFAULT) |
| 108 | } |
| 109 | |
| 110 | |
| 111 | pub fn scheme_cookie(scheme: Scheme) -> Cookie<'static> { |
| 112 | Cookie::build((SCHEME_COOKIE, scheme.id())) |
| 113 | .path("/") |
| 114 | .same_site(SameSite::Lax) |
| 115 | .max_age(Duration::days(SCHEME_COOKIE_DAYS)) |
| 116 | .build() |
| 117 | } |