1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
use crate::color::{LinSrgba, Srgba};
use std::collections::HashMap;
/// A set of styling defaults used for coloring texturing geometric primitives that have no entry
/// within the **Draw**'s inner **ColorMap**.
#[derive(Clone, Debug)]
pub struct Theme {
/// Fill color defaults.
pub fill_color: Color,
/// Stroke color defaults.
pub stroke_color: Color,
}
/// A set of defaults used for coloring.
#[derive(Clone, Debug)]
pub struct Color {
pub default: Srgba,
pub primitive: HashMap<Primitive, Srgba>,
}
/// Primitive geometry types that may have unique default styles.
///
/// These are used as keys into the **Theme**'s geometry primitive default values.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum Primitive {
Arrow,
Cuboid,
Ellipse,
Line,
Mesh,
Path,
Polygon,
Quad,
Rect,
Text,
Texture,
Tri,
}
impl Theme {
/// Retrieve the non-linear sRGBA fill color representation for the given primitive.
pub fn fill_srgba(&self, prim: &Primitive) -> Srgba {
self.fill_color
.primitive
.get(prim)
.map(|&c| c)
.unwrap_or(self.fill_color.default)
}
/// Retrieve the linaer sRGBA fill color representation for the given primitive.
pub fn fill_lin_srgba(&self, prim: &Primitive) -> LinSrgba {
self.fill_srgba(prim).into_linear()
}
/// Retrieve the non-linear sRGBA stroke color representation for the given primitive.
pub fn stroke_srgba(&self, prim: &Primitive) -> Srgba {
self.stroke_color
.primitive
.get(prim)
.map(|&c| c)
.unwrap_or(self.stroke_color.default)
}
/// Retrieve the linaer sRGBA stroke color representation for the given primitive.
pub fn stroke_lin_srgba(&self, prim: &Primitive) -> LinSrgba {
self.stroke_srgba(prim).into_linear()
}
}
impl Default for Theme {
fn default() -> Self {
// TODO: This should be pub const.
let default_fill = Srgba::new(1.0, 1.0, 1.0, 1.0);
let default_stroke = Srgba::new(0.0, 0.0, 0.0, 1.0);
let fill_color = Color {
default: default_fill,
primitive: Default::default(),
};
let mut stroke_color = Color {
default: default_stroke,
primitive: Default::default(),
};
stroke_color
.primitive
.insert(Primitive::Arrow, default_fill);
Theme {
fill_color,
stroke_color,
}
}
}