keyset_color/
skia.rs

1use super::Color;
2
3use tiny_skia::Color as SkiaColor;
4
5#[allow(clippy::fallible_impl_from)] // It's not really fallible due to the clamp
6impl From<Color> for SkiaColor {
7    fn from(value: Color) -> Self {
8        let (r, g, b) = value.map(|c| c.clamp(0.0, 1.0)).into();
9        Self::from_rgba(r, g, b, 1.0).unwrap()
10    }
11}
12
13impl From<SkiaColor> for Color {
14    fn from(value: SkiaColor) -> Self {
15        let (r, g, b) = (value.red(), value.green(), value.blue());
16        Self::new(r, g, b)
17    }
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23
24    #[test]
25    fn from_skia() {
26        let skia = SkiaColor::from_rgba(0.2, 0.4, 0.6, 1.0).unwrap();
27        let color = Color::from(skia);
28
29        assert_eq!(color.0[0], 0.2);
30        assert_eq!(color.0[1], 0.4);
31        assert_eq!(color.0[2], 0.6);
32    }
33
34    #[test]
35    fn into_rgbf32() {
36        let color = Color::new(0.2, 0.4, 0.6);
37        let skia: SkiaColor = color.into();
38
39        assert_eq!(skia.red(), 0.2);
40        assert_eq!(skia.green(), 0.4);
41        assert_eq!(skia.blue(), 0.6);
42        assert_eq!(skia.alpha(), 1.0);
43    }
44}