1
//! Built-in map themes: `MapCSS` sheets for the vector-tile renderer
2
//! (`azul_dll::desktop::extra::map::svg`), one per [`MapTheme`] preset.
3
//!
4
//! THE DIALECT the renderer understands is deliberately small — `selector
5
//! { fill: …; stroke: …; stroke-width: …; }` where the selector is an
6
//! `OpenMapTiles` LAYER name (`water`, `building`, `transportation`, …), a
7
//! `layer.class` pair (`transportation.motorway`) or `canvas` (the tile's
8
//! base colour, i.e. land). Every colour is a plain hex so the SVG
9
//! rasteriser never has to parse `hsl()`.
10
//!
11
//! # Provenance and licences (credit is REQUIRED, see `MapTheme::credit`)
12
//!
13
//! * `POSITRON`, `DARK`, `BRIGHT`, `LIBERTY` are palette extractions from the styles `OpenFreeMap`
14
//!   publishes at `tiles.openfreemap.org/styles/*` (repo `hyperknot/openfreemap-styles`, MIT). The
15
//!   styles themselves: Positron and Dark (Dark Matter) — CARTO basemaps designed by Stamen and
16
//!   Paul Norman, forked via `openmaptiles/positron-gl-style` and
17
//!   `openmaptiles/dark-matter-gl-style`; Bright — `openmaptiles/ osm-bright-gl-style`; Liberty —
18
//!   `maputnik/osm-liberty`. Each is BSD-3-Clause (code) and CC BY 4.0 (design); the design licence
19
//!   is what applies to a palette, and CC BY 4.0 permits this use WITH attribution — which the
20
//!   widget shows in its attribution line.
21
//! * `GOOGLE_NIGHT` is the "Night mode" style array from the Google Maps Platform documentation
22
//!   (`developers.google.com/maps/documentation/ javascript/examples/style-array`), whose code
23
//!   samples are Apache-2.0.
24
//! * `GOOGLE_LIGHT`, `APPLE_LIGHT`, `APPLE_DARK` are AUTHORED here to resemble the familiar default
25
//!   looks of those products; no asset, style file or trademark of theirs is used — colour values
26
//!   only.
27

            
28
/// CARTO Positron via `OpenFreeMap` — the light, desaturated reference look.
29
pub const POSITRON: &str = r"
30
canvas { fill: #f2f3f0; }
31
water { fill: #c2c8ca; stroke: none; }
32
waterway { fill: none; stroke: #c2c8ca; stroke-width: 0.6; }
33
park { fill: #e6e9e5; stroke: none; }
34
landcover { fill: #e6e9e5; stroke: none; }
35
landuse { fill: #eaeae6; stroke: none; }
36
building { fill: #eaeae5; stroke: #dbdbda; stroke-width: 0.3; }
37
transportation { fill: none; stroke: #e0e0e0; stroke-width: 0.8; }
38
transportation.tertiary { fill: none; stroke: #ffffff; stroke-width: 1.2; }
39
transportation.secondary { fill: none; stroke: #ffffff; stroke-width: 1.4; }
40
transportation.primary { fill: none; stroke: #ffffff; stroke-width: 1.6; }
41
transportation.trunk { fill: none; stroke: #ffffff; stroke-width: 1.8; }
42
transportation.motorway { fill: none; stroke: #ffffff; stroke-width: 2.0; }
43
transportation.rail { fill: none; stroke: #d5d5d5; stroke-width: 0.6; }
44
boundary { fill: none; stroke: #b3b3b3; stroke-width: 0.6; }
45
aeroway { fill: #e9e9e6; stroke: none; }
46
";
47

            
48
/// CARTO Dark Matter via `OpenFreeMap` — the dark reference look.
49
pub const DARK: &str = r"
50
canvas { fill: #0c0c0c; }
51
water { fill: #1b1b1d; stroke: none; }
52
waterway { fill: none; stroke: #1b1b1d; stroke-width: 0.6; }
53
park { fill: #202020; stroke: none; }
54
landcover { fill: #1a1a1a; stroke: none; }
55
landuse { fill: #0d0c0c; stroke: none; }
56
building { fill: #0a0a0a; stroke: #1b1b1d; stroke-width: 0.3; }
57
transportation { fill: none; stroke: #222222; stroke-width: 0.8; }
58
transportation.tertiary { fill: none; stroke: #2a2a2a; stroke-width: 1.2; }
59
transportation.secondary { fill: none; stroke: #2c2c2c; stroke-width: 1.4; }
60
transportation.primary { fill: none; stroke: #2e2e2e; stroke-width: 1.6; }
61
transportation.trunk { fill: none; stroke: #303030; stroke-width: 1.8; }
62
transportation.motorway { fill: none; stroke: #333333; stroke-width: 2.0; }
63
transportation.rail { fill: none; stroke: #1f1f1f; stroke-width: 0.6; }
64
boundary { fill: none; stroke: #3b3b3b; stroke-width: 0.6; }
65
aeroway { fill: #161616; stroke: none; }
66
";
67

            
68
/// OSM Bright via `OpenFreeMap` — the colourful general-purpose look.
69
pub const BRIGHT: &str = r"
70
canvas { fill: #f8f4f0; }
71
water { fill: #aecfe2; stroke: none; }
72
waterway { fill: none; stroke: #aecfe2; stroke-width: 0.8; }
73
park { fill: #d8e8c8; stroke: none; }
74
landcover { fill: #d8e8c8; stroke: none; }
75
landcover.wood { fill: #c3e6a9; stroke: none; }
76
landuse { fill: #f2eee9; stroke: none; }
77
building { fill: #f2eae2; stroke: #dfdbd7; stroke-width: 0.3; }
78
transportation { fill: none; stroke: #ffffff; stroke-width: 0.9; }
79
transportation.tertiary { fill: none; stroke: #ffeeaa; stroke-width: 1.2; }
80
transportation.secondary { fill: none; stroke: #ffeeaa; stroke-width: 1.4; }
81
transportation.primary { fill: none; stroke: #ffeeaa; stroke-width: 1.6; }
82
transportation.trunk { fill: none; stroke: #ffeeaa; stroke-width: 1.8; }
83
transportation.motorway { fill: none; stroke: #ffcc88; stroke-width: 2.0; }
84
transportation.rail { fill: none; stroke: #bbbbbb; stroke-width: 0.6; }
85
boundary { fill: none; stroke: #a4a2ae; stroke-width: 0.7; }
86
aeroway { fill: #e8e8e8; stroke: none; }
87
";
88

            
89
/// OSM Liberty via `OpenFreeMap` — the Bright lineage with a brighter sea.
90
pub const LIBERTY: &str = r"
91
canvas { fill: #f8f4f0; }
92
water { fill: #9ebdff; stroke: none; }
93
waterway { fill: none; stroke: #9ebdff; stroke-width: 0.8; }
94
park { fill: #d8e8c8; stroke: none; }
95
landcover { fill: #b0d59a; stroke: none; }
96
landcover.wood { fill: #c3e6a9; stroke: none; }
97
landuse { fill: #dbd8d8; stroke: none; }
98
building { fill: #dcd9d6; stroke: #d0ccc7; stroke-width: 0.3; }
99
transportation { fill: none; stroke: #ffffff; stroke-width: 0.9; }
100
transportation.tertiary { fill: none; stroke: #ffeeaa; stroke-width: 1.2; }
101
transportation.secondary { fill: none; stroke: #ffeeaa; stroke-width: 1.4; }
102
transportation.primary { fill: none; stroke: #ffeeaa; stroke-width: 1.6; }
103
transportation.trunk { fill: none; stroke: #ffeeaa; stroke-width: 1.8; }
104
transportation.motorway { fill: none; stroke: #ffcc88; stroke-width: 2.0; }
105
transportation.rail { fill: none; stroke: #bbbbbb; stroke-width: 0.6; }
106
boundary { fill: none; stroke: #686869; stroke-width: 0.7; }
107
aeroway { fill: #e8e8e8; stroke: none; }
108
";
109

            
110
/// Google Maps' published "Night mode" style array (Apache-2.0 sample).
111
pub const GOOGLE_NIGHT: &str = r"
112
canvas { fill: #242f3e; }
113
water { fill: #17263c; stroke: none; }
114
waterway { fill: none; stroke: #17263c; stroke-width: 0.8; }
115
park { fill: #263c3f; stroke: none; }
116
landcover { fill: #263c3f; stroke: none; }
117
landuse { fill: #242f3e; stroke: none; }
118
building { fill: #2b3646; stroke: #212a37; stroke-width: 0.3; }
119
transportation { fill: none; stroke: #38414e; stroke-width: 0.9; }
120
transportation.tertiary { fill: none; stroke: #38414e; stroke-width: 1.2; }
121
transportation.secondary { fill: none; stroke: #38414e; stroke-width: 1.4; }
122
transportation.primary { fill: none; stroke: #38414e; stroke-width: 1.6; }
123
transportation.trunk { fill: none; stroke: #746855; stroke-width: 1.8; }
124
transportation.motorway { fill: none; stroke: #746855; stroke-width: 2.0; }
125
transportation.rail { fill: none; stroke: #2f3948; stroke-width: 0.7; }
126
boundary { fill: none; stroke: #3e4a5c; stroke-width: 0.7; }
127
aeroway { fill: #2f3948; stroke: none; }
128
";
129

            
130
/// A Google-Maps-like light look (authored here; colour values only).
131
pub const GOOGLE_LIGHT: &str = r"
132
canvas { fill: #f2efe9; }
133
water { fill: #aadaff; stroke: none; }
134
waterway { fill: none; stroke: #aadaff; stroke-width: 0.8; }
135
park { fill: #c9e8b5; stroke: none; }
136
landcover { fill: #d5ecc2; stroke: none; }
137
landuse { fill: #ede9e2; stroke: none; }
138
building { fill: #e8e6e1; stroke: #dcd9d3; stroke-width: 0.3; }
139
transportation { fill: none; stroke: #ffffff; stroke-width: 0.9; }
140
transportation.tertiary { fill: none; stroke: #ffffff; stroke-width: 1.2; }
141
transportation.secondary { fill: none; stroke: #ffffff; stroke-width: 1.4; }
142
transportation.primary { fill: none; stroke: #fdf3c4; stroke-width: 1.6; }
143
transportation.trunk { fill: none; stroke: #f9d776; stroke-width: 1.8; }
144
transportation.motorway { fill: none; stroke: #f9d776; stroke-width: 2.0; }
145
transportation.rail { fill: none; stroke: #d4d2cd; stroke-width: 0.6; }
146
boundary { fill: none; stroke: #c9c6be; stroke-width: 0.7; }
147
aeroway { fill: #e6e4df; stroke: none; }
148
";
149

            
150
/// An Apple-Maps-like light look (authored here; colour values only).
151
pub const APPLE_LIGHT: &str = r"
152
canvas { fill: #f5f3ef; }
153
water { fill: #a7d3ee; stroke: none; }
154
waterway { fill: none; stroke: #a7d3ee; stroke-width: 0.8; }
155
park { fill: #c6e5b4; stroke: none; }
156
landcover { fill: #d3ebc2; stroke: none; }
157
landuse { fill: #eeebe5; stroke: none; }
158
building { fill: #e7e3dc; stroke: #d9d5cd; stroke-width: 0.3; }
159
transportation { fill: none; stroke: #ffffff; stroke-width: 0.9; }
160
transportation.tertiary { fill: none; stroke: #ffffff; stroke-width: 1.2; }
161
transportation.secondary { fill: none; stroke: #ffffff; stroke-width: 1.4; }
162
transportation.primary { fill: none; stroke: #fbf1c4; stroke-width: 1.6; }
163
transportation.trunk { fill: none; stroke: #f9d578; stroke-width: 1.8; }
164
transportation.motorway { fill: none; stroke: #f9d578; stroke-width: 2.0; }
165
transportation.rail { fill: none; stroke: #d2cfc8; stroke-width: 0.6; }
166
boundary { fill: none; stroke: #cfcac0; stroke-width: 0.7; }
167
aeroway { fill: #e9e6e0; stroke: none; }
168
";
169

            
170
/// An Apple-Maps-like dark look (authored here; colour values only).
171
pub const APPLE_DARK: &str = r"
172
canvas { fill: #1c1c1e; }
173
water { fill: #0f2236; stroke: none; }
174
waterway { fill: none; stroke: #0f2236; stroke-width: 0.8; }
175
park { fill: #1e2f22; stroke: none; }
176
landcover { fill: #1b2a1f; stroke: none; }
177
landuse { fill: #222224; stroke: none; }
178
building { fill: #2a2a2d; stroke: #353538; stroke-width: 0.3; }
179
transportation { fill: none; stroke: #2f2f33; stroke-width: 0.9; }
180
transportation.tertiary { fill: none; stroke: #343438; stroke-width: 1.2; }
181
transportation.secondary { fill: none; stroke: #383840; stroke-width: 1.4; }
182
transportation.primary { fill: none; stroke: #4a4633; stroke-width: 1.6; }
183
transportation.trunk { fill: none; stroke: #5c5133; stroke-width: 1.8; }
184
transportation.motorway { fill: none; stroke: #5c5133; stroke-width: 2.0; }
185
transportation.rail { fill: none; stroke: #2b2b2f; stroke-width: 0.7; }
186
boundary { fill: none; stroke: #45454a; stroke-width: 0.7; }
187
aeroway { fill: #26262a; stroke: none; }
188
";
189

            
190
/// The `canvas { fill: #rrggbb; }` colour of a sheet, if it declares one.
191
#[must_use]
192
9
pub fn canvas_fill(sheet: &str) -> Option<[u8; 3]> {
193
9
    let start = sheet.find("canvas")?;
194
8
    let rest = &sheet[start..];
195
8
    let open = rest.find('{')?;
196
8
    let close = rest.find('}')?;
197
8
    if close < open {
198
        return None;
199
8
    }
200
8
    let body = &rest[open + 1..close];
201
8
    let fill = body.split(';').find_map(|decl| {
202
8
        let (prop, val) = decl.split_once(':')?;
203
8
        let prop = prop.trim().to_ascii_lowercase();
204
8
        (prop == "fill" || prop == "fill-color").then(|| val.trim())
205
8
    })?;
206
8
    parse_hex_rgb(fill)
207
9
}
208

            
209
/// `#rrggbb` → channels; anything else → `None`.
210
#[must_use]
211
90
pub fn parse_hex_rgb(s: &str) -> Option<[u8; 3]> {
212
90
    let hex = s.strip_prefix('#')?;
213
89
    if hex.len() != 6 {
214
        return None;
215
89
    }
216
89
    let r = u8::from_str_radix(&hex[0..2], 16).ok()?;
217
89
    let g = u8::from_str_radix(&hex[2..4], 16).ok()?;
218
89
    let b = u8::from_str_radix(&hex[4..6], 16).ok()?;
219
89
    Some([r, g, b])
220
90
}
221

            
222
/// Perceived lightness of an sRGB colour, 0 (black) .. 1 (white).
223
#[must_use]
224
8
pub fn luma(rgb: [u8; 3]) -> f32 {
225
8
    let [r, g, b] = rgb;
226
8
    0.0722f32.mul_add(
227
8
        f32::from(b),
228
8
        0.2126f32.mul_add(f32::from(r), 0.7152 * f32::from(g)),
229
8
    ) / 255.0
230
8
}
231

            
232
#[cfg(test)]
233
mod tests {
234
    use super::*;
235

            
236
    const ALL: [(&str, &str, bool); 8] = [
237
        ("positron", POSITRON, false),
238
        ("dark", DARK, true),
239
        ("bright", BRIGHT, false),
240
        ("liberty", LIBERTY, false),
241
        ("google-night", GOOGLE_NIGHT, true),
242
        ("google-light", GOOGLE_LIGHT, false),
243
        ("apple-light", APPLE_LIGHT, false),
244
        ("apple-dark", APPLE_DARK, true),
245
    ];
246

            
247
    #[test]
248
1
    fn every_preset_styles_the_layers_the_renderer_draws_with_hex_colours_only() {
249
9
        for (name, sheet, _) in ALL {
250
56
            for key in [
251
                "canvas",
252
8
                "water",
253
8
                "park",
254
8
                "building",
255
8
                "transportation",
256
8
                "transportation.motorway",
257
8
                "boundary",
258
            ] {
259
56
                assert!(
260
56
                    sheet.contains(&format!("\n{key} {{")),
261
                    "{name}: no rule for `{key}`"
262
                );
263
            }
264
            // hex only: the SVG rasteriser must never be handed hsl()/rgb()
265
340
            for decl in sheet.split(';') {
266
340
                if let Some((prop, val)) = decl.split_once(':') {
267
332
                    let prop = prop.trim();
268
332
                    let val = val.trim();
269
332
                    if prop == "fill" || prop == "stroke" {
270
122
                        assert!(
271
122
                            val == "none" || parse_hex_rgb(val).is_some(),
272
                            "{name}: `{prop}: {val}` is not `none` or #rrggbb"
273
                        );
274
210
                    }
275
8
                }
276
            }
277
        }
278
1
    }
279

            
280
    #[test]
281
1
    fn dark_presets_have_a_dark_canvas_and_light_presets_a_light_one() {
282
9
        for (name, sheet, dark) in ALL {
283
8
            let canvas = canvas_fill(sheet).unwrap_or_else(|| panic!("{name}: no canvas fill"));
284
8
            let l = luma(canvas);
285
8
            assert_eq!(
286
8
                l < 0.5,
287
                dark,
288
                "{name}: canvas luma {l} does not match dark = {dark}"
289
            );
290
        }
291
1
        assert_eq!(canvas_fill("water { fill: #000000; }"), None);
292
1
        assert_eq!(parse_hex_rgb("#0c0c0c"), Some([12, 12, 12]));
293
1
        assert_eq!(parse_hex_rgb("rgb(1,2,3)"), None);
294
1
    }
295
}