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
94
95
96
97
98
99
100
101
102
use std::fmt;
/// Error type for palette lookup and color conversion.
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
/// The requested palette family does not exist.
UnknownFamily {
/// Requested family name.
family: String,
},
/// The requested variant does not exist within a known family.
UnknownVariant {
/// Requested family name.
family: String,
/// Requested variant name.
variant: String,
},
/// A palette specification was not written as `family:variant`.
InvalidPaletteSpec {
/// Requested palette specification.
spec: String,
},
/// A hex color string was malformed.
InvalidHexColor {
/// Requested hex color.
input: String,
},
/// A floating-point alpha value was outside its operation's valid range.
InvalidAlpha {
/// Requested alpha value.
alpha: f32,
},
/// More colors were requested than a discrete palette contains.
TooManyColorsRequested {
/// Palette family.
family: &'static str,
/// Palette variant.
variant: &'static str,
/// Requested number of colors.
requested: usize,
/// Available number of colors.
available: usize,
},
/// A discrete-only operation was requested for a continuous palette.
NotDiscretePalette {
/// Palette family.
family: &'static str,
/// Palette variant.
variant: &'static str,
},
/// A continuous-only operation was requested for a discrete palette.
NotContinuousPalette {
/// Palette family.
family: &'static str,
/// Palette variant.
variant: &'static str,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownFamily { family } => {
write!(f, "unknown ggsci palette family `{family}`")
}
Self::UnknownVariant { family, variant } => {
write!(f, "unknown ggsci palette variant `{variant}` for family `{family}`")
}
Self::InvalidPaletteSpec { spec } => {
write!(f, "invalid palette spec `{spec}`; expected `family:variant`")
}
Self::InvalidHexColor { input } => {
write!(f, "invalid hex color `{input}`; expected `#RRGGBB`")
}
Self::InvalidAlpha { alpha } => {
write!(
f,
"invalid alpha `{alpha}`; expected a finite value in 0.0..=1.0 (continuous interpolation requires alpha > 0.0)"
)
}
Self::TooManyColorsRequested {
family,
variant,
requested,
available,
} => write!(
f,
"requested {requested} colors from discrete palette `{family}:{variant}`, but only {available} category colors are available"
),
Self::NotDiscretePalette { family, variant } => write!(
f,
"`{family}:{variant}` is a continuous palette; this operation requires a discrete palette"
),
Self::NotContinuousPalette { family, variant } => write!(
f,
"`{family}:{variant}` is a discrete palette; this operation requires a continuous palette"
),
}
}
}
impl std::error::Error for Error {}