skia_safe/effects/
high_contrast_filter.rs

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
use crate::{high_contrast_config::InvertStyle, prelude::*, scalar, ColorFilter};
use skia_bindings::{self as sb, SkHighContrastConfig};

pub mod high_contrast_config {
    pub use skia_bindings::SkHighContrastConfig_InvertStyle as InvertStyle;
    #[test]
    fn invert_style_naming() {
        let _ = InvertStyle::InvertLightness;
    }
}

#[repr(C)]
#[derive(Clone, PartialEq, Debug)]
pub struct HighContrastConfig {
    pub grayscale: bool,
    pub invert_style: InvertStyle,
    pub contrast: scalar,
}

native_transmutable!(
    SkHighContrastConfig,
    HighContrastConfig,
    high_contrast_config
);

impl Default for HighContrastConfig {
    fn default() -> Self {
        Self {
            grayscale: false,
            invert_style: InvertStyle::NoInvert,
            contrast: 0.0,
        }
    }
}

impl HighContrastConfig {
    pub fn new(grayscale: bool, invert_style: InvertStyle, contrast: scalar) -> Self {
        Self {
            grayscale,
            invert_style,
            contrast,
        }
    }

    pub fn is_valid(&self) -> bool {
        self.contrast >= -1.0 && self.contrast <= 1.0
    }
}

impl ColorFilter {
    pub fn high_contrast(config: &HighContrastConfig) -> Option<Self> {
        new(config)
    }
}

pub fn new(config: &HighContrastConfig) -> Option<ColorFilter> {
    ColorFilter::from_ptr(unsafe { sb::C_SkHighContrastFilter_Make(config.native()) })
}