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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::{Error, IccIntent, ObjectType, SpaceFlags, Trc};

define_object!(Space);

impl Space {
    #[doc(alias = "babl_space")]
    pub fn from_name(name: &str) -> Self {
        unsafe { Self::from_raw_full(ffi::babl_space(name.as_ptr() as *const std::ffi::c_char)) }
    }

    #[doc(alias = "babl_space_from_icc")]
    pub fn from_icc(icc_data: Vec<u8>, intent: IccIntent) -> Result<Self, Error> {
        unsafe {
            let error = "";

            let space = ffi::babl_space_from_icc(
                icc_data.as_ptr() as *const std::ffi::c_char,
                icc_data.len() as _,
                intent.into(),
                error.as_ptr() as *mut *const std::ffi::c_char,
            );

            match error {
                "" => Ok(Self::from_raw_full(space)),
                err => Err(Error::Space(err.to_string())),
            }
        }
    }

    #[allow(clippy::too_many_arguments)]
    #[doc(alias = "babl_space_from_chromaticities")]
    pub fn from_chromaticities(
        name: &str,
        wx: f64,
        wy: f64,
        rx: f64,
        ry: f64,
        gx: f64,
        gy: f64,
        bx: f64,
        by: f64,
        trc_red: &Trc,
        trc_green: &Trc,
        trc_blue: &Trc,
        flags: SpaceFlags,
    ) -> Self {
        unsafe {
            Self::from_raw_full(ffi::babl_space_from_chromaticities(
                name.as_ptr() as *const std::ffi::c_char,
                wx,
                wy,
                rx,
                ry,
                gx,
                gy,
                bx,
                by,
                trc_red.inner(),
                trc_green.inner(),
                trc_blue.inner(),
                flags.into(),
            ))
        }
    }

    #[allow(clippy::too_many_arguments)]
    #[doc(alias = "babl_space_from_rgbxyz_matrix")]
    pub fn from_rgbxyz_matrix(
        name: &str,
        wx: f64,
        wy: f64,
        wz: f64,
        rx: f64,
        gx: f64,
        bx: f64,
        ry: f64,
        gy: f64,
        by: f64,
        rz: f64,
        gz: f64,
        bz: f64,
        trc_red: &Trc,
        trc_green: &Trc,
        trc_blue: &Trc,
    ) -> Self {
        unsafe {
            Self::from_raw_full(ffi::babl_space_from_rgbxyz_matrix(
                name.as_ptr() as *const std::ffi::c_char,
                wx,
                wy,
                wz,
                rx,
                gx,
                bx,
                ry,
                gy,
                by,
                rz,
                gz,
                bz,
                trc_red.inner(),
                trc_green.inner(),
                trc_blue.inner(),
            ))
        }
    }

    #[doc(alias = "babl_space_with_trc")]
    pub fn with_trc(self, trc: &Trc) -> Self {
        unsafe { Self::from_raw_full(ffi::babl_space_with_trc(self.0, trc.inner())) }
    }

    #[doc(alias = "get_gamma")]
    #[doc(alias = "babl_space_get_gamma")]
    pub fn gamma(&self) -> f64 {
        unsafe { ffi::babl_space_get_gamma(self.0) }
    }

    #[doc(alias = "get_rgb_luminance")]
    #[doc(alias = "babl_space_get_rgb_luminance")]
    pub fn rgb_luminance(&self) -> (f64, f64, f64) {
        unsafe {
            let mut luminance = (0.0, 0.0, 0.0);
            ffi::babl_space_get_rgb_luminance(
                self.0,
                &mut luminance.0,
                &mut luminance.1,
                &mut luminance.2,
            );
            (luminance.0, luminance.1, luminance.2)
        }
    }

    #[doc(alias = "babl_space_is_cmyk")]
    pub fn is_cmyk(&self) -> bool {
        unsafe { ffi::babl_space_is_cmyk(self.0) == 1 }
    }

    #[doc(alias = "babl_space_is_gray")]
    pub fn is_gray(&self) -> bool {
        unsafe { ffi::babl_space_is_gray(self.0) == 1 }
    }

    #[allow(clippy::too_many_arguments)]
    #[doc(alias = "babl_space_get")]
    pub fn get(
        &self,
        xw: &mut f64,
        yw: &mut f64,
        xr: &mut f64,
        yr: &mut f64,
        xg: &mut f64,
        yg: &mut f64,
        xb: &mut f64,
        yb: &mut f64,
        red_trc: &mut Trc,
        green_trc: &mut Trc,
        blue_trc: &mut Trc,
    ) {
        unsafe {
            ffi::babl_space_get(
                self.0,
                xw,
                yw,
                xr,
                yr,
                xg,
                yg,
                xb,
                yb,
                &mut red_trc.inner(),
                &mut green_trc.inner(),
                &mut blue_trc.inner(),
            )
        }
    }
}