Skip to main content

osmic_render/
style_eval.rs

1use osmic_core::Color;
2use osmic_osm::feature::{
3    FeatureKind, HighwayKind, LanduseKind, LeisureKind, NaturalKind, RailwayKind, WaterKind,
4};
5
6use crate::scene::{LineCap, LineJoin};
7
8/// Resolve fill color for a feature kind.
9pub fn fill_color(kind: &FeatureKind) -> Option<Color> {
10    match kind {
11        FeatureKind::Building(_) => Color::from_hex("#dfdbd7"),
12        FeatureKind::Landuse(k) => match k {
13            LanduseKind::Forest => Color::from_hex("#add19e"),
14            LanduseKind::Grass | LanduseKind::Meadow => Color::from_hex("#cdebb0"),
15            LanduseKind::Farmland => Color::from_hex("#d5e29e"),
16            LanduseKind::Residential => Color::from_hex("#e0d6d0"),
17            LanduseKind::Commercial => Color::from_hex("#f2dad9"),
18            LanduseKind::Industrial => Color::from_hex("#ebdbe8"),
19            LanduseKind::Cemetery => Color::from_hex("#aacbaf"),
20            _ => Color::from_hex("#d5cfc8"),
21        },
22        FeatureKind::Natural(k) => match k {
23            NaturalKind::Wood => Color::from_hex("#add19e"),
24            NaturalKind::Scrub => Color::from_hex("#c8d7ab"),
25            NaturalKind::Grassland => Color::from_hex("#cdebb0"),
26            NaturalKind::Sand | NaturalKind::Beach => Color::from_hex("#f5e9c6"),
27            NaturalKind::Glacier => Color::from_hex("#ddecec"),
28            NaturalKind::Water => Color::from_hex("#aad3df"),
29            _ => None,
30        },
31        FeatureKind::Water(
32            WaterKind::Lake | WaterKind::Pond | WaterKind::Reservoir | WaterKind::Basin,
33        ) => Color::from_hex("#aad3df"),
34        FeatureKind::Leisure(k) => match k {
35            LeisureKind::Park | LeisureKind::Garden | LeisureKind::NatureReserve => {
36                Color::from_hex("#c8facc")
37            }
38            LeisureKind::GolfCourse => Color::from_hex("#b5e3b5"),
39            _ => None,
40        },
41        FeatureKind::Amenity(osmic_osm::feature::AmenityKind::Parking) => {
42            Color::from_hex("#eeeeee")
43        }
44        _ => None,
45    }
46}
47
48/// Resolve stroke color and width for a feature kind.
49pub fn stroke_style(kind: &FeatureKind) -> Option<(Color, f32, LineCap, LineJoin)> {
50    match kind {
51        FeatureKind::Highway(k) => {
52            let (color, width) = match k {
53                HighwayKind::Motorway | HighwayKind::MotorwayLink => ("#e892a2", 4.0),
54                HighwayKind::Trunk | HighwayKind::TrunkLink => ("#f9b29c", 3.5),
55                HighwayKind::Primary | HighwayKind::PrimaryLink => ("#fcd6a4", 3.0),
56                HighwayKind::Secondary | HighwayKind::SecondaryLink => ("#f7fabf", 2.5),
57                HighwayKind::Tertiary | HighwayKind::TertiaryLink => ("#ffffff", 2.0),
58                HighwayKind::Residential | HighwayKind::Unclassified => ("#ffffff", 1.5),
59                HighwayKind::Service => ("#ffffff", 1.0),
60                _ => ("#cccccc", 0.75),
61            };
62            Some((
63                Color::from_hex(color).unwrap(),
64                width,
65                LineCap::Round,
66                LineJoin::Round,
67            ))
68        }
69        FeatureKind::Railway(k) => {
70            let width = match k {
71                RailwayKind::Rail => 1.5,
72                _ => 1.0,
73            };
74            Some((
75                Color::from_hex("#bfbfbf").unwrap(),
76                width,
77                LineCap::Butt,
78                LineJoin::Miter,
79            ))
80        }
81        FeatureKind::Water(k) => match k {
82            WaterKind::River => Some((
83                Color::from_hex("#aad3df").unwrap(),
84                2.5,
85                LineCap::Round,
86                LineJoin::Round,
87            )),
88            WaterKind::Stream | WaterKind::Canal => Some((
89                Color::from_hex("#aad3df").unwrap(),
90                1.5,
91                LineCap::Round,
92                LineJoin::Round,
93            )),
94            _ => None,
95        },
96        FeatureKind::Boundary(_) => Some((
97            Color::from_hex("#9e9cab").unwrap(),
98            1.5,
99            LineCap::Butt,
100            LineJoin::Miter,
101        )),
102        _ => None,
103    }
104}
105
106/// Z-order for rendering layers (lower = drawn first).
107pub fn z_order(kind: &FeatureKind) -> i32 {
108    match kind {
109        FeatureKind::Landuse(_) => 10,
110        FeatureKind::Natural(_) => 20,
111        FeatureKind::Leisure(_) => 30,
112        FeatureKind::Water(WaterKind::Lake | WaterKind::Pond | WaterKind::Reservoir) => 40,
113        FeatureKind::Building(_) => 50,
114        FeatureKind::Boundary(_) => 60,
115        FeatureKind::Railway(_) => 70,
116        FeatureKind::Water(_) => 80,
117        FeatureKind::Highway(_) => 100,
118        FeatureKind::Amenity(_) => 110,
119        FeatureKind::Shop(_) => 115,
120        FeatureKind::Tourism(_) => 120,
121        FeatureKind::Office(_) => 125,
122        FeatureKind::Healthcare(_) => 130,
123        FeatureKind::Craft(_) => 135,
124        FeatureKind::Historic(_) => 140,
125        FeatureKind::Club(_) => 115,
126        FeatureKind::Emergency(_) => 112,
127        FeatureKind::Education(_) => 113,
128        FeatureKind::Place(_) => 200,
129    }
130}