Skip to main content

perceive_cvd/
types.rs

1/// Type of color vision deficiency.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4#[non_exhaustive]
5pub enum CvdType {
6    /// Red-blind (L-cone deficiency).
7    Protan,
8    /// Green-blind (M-cone deficiency).
9    Deutan,
10    /// Blue-blind (S-cone deficiency).
11    Tritan,
12    /// Total color blindness (achromatopsia).
13    Achromat,
14}
15
16/// Severity of color vision deficiency [0, 1].
17///
18/// 0.0 = normal vision, 1.0 = complete dichromacy.
19#[derive(Debug, Clone, Copy, PartialEq)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21pub struct Severity(f64);
22
23impl Severity {
24    /// Create a new severity value, clamped to [0, 1].
25    #[must_use]
26    pub fn new(value: f64) -> Self {
27        Self(value.clamp(0.0, 1.0))
28    }
29
30    /// Get the severity value.
31    #[must_use]
32    pub fn value(self) -> f64 {
33        self.0
34    }
35
36    /// Full severity (complete dichromacy).
37    pub const FULL: Self = Self(1.0);
38
39    /// No deficiency (normal vision).
40    pub const NONE: Self = Self(0.0);
41}
42
43impl Default for Severity {
44    fn default() -> Self {
45        Self::FULL
46    }
47}