libnotcurses_sys/pixel/
methods.rs

1use crate::{
2    c_api::{self, NcPixel_u32},
3    NcPixel, NcRgb, NcRgba,
4};
5
6impl NcPixel {
7    /// Returns a new `NcPixel`.
8    pub fn new(value: c_api::NcPixel_u32) -> Self {
9        Self(value)
10    }
11
12    /// Constructs a libav-compatible ABGR pixel from RGB components.
13    pub fn from_rgb(rgb: impl Into<NcRgb>) -> Self {
14        let (r, g, b) = rgb.into().into();
15        c_api::ncpixel(r, g, b).into()
16    }
17
18    /// Constructs a libav-compatible ABGR pixel from RGBA components.
19    pub fn from_rgba(rgba: impl Into<NcRgba>) -> Self {
20        let (r, g, b, a) = rgba.into().into();
21
22        let bgra = (a as NcPixel_u32) << 24
23            | (b as NcPixel_u32) << 16
24            | (g as NcPixel_u32) << 8
25            | r as NcPixel_u32;
26        bgra.into()
27    }
28
29    /// Converts to an RGB pixel.
30    pub fn to_rgb(&self) -> NcRgb {
31        NcRgb::new(
32            c_api::ncpixel_r(self.0),
33            c_api::ncpixel_g(self.0),
34            c_api::ncpixel_b(self.0),
35        )
36    }
37
38    /// Converts to an RGBA pixel.
39    pub fn to_rgba(&self) -> NcRgba {
40        NcRgba::new(
41            c_api::ncpixel_r(self.0),
42            c_api::ncpixel_g(self.0),
43            c_api::ncpixel_b(self.0),
44            c_api::ncpixel_a(self.0),
45        )
46    }
47
48    /// Extracts the 8-bit alpha component from an ABGR pixel.
49    pub fn a(self) -> u8 {
50        c_api::ncpixel_a(self.into())
51    }
52
53    /// Extracts the 8 bit blue component from an ABGR pixel.
54    pub fn b(self) -> u8 {
55        c_api::ncpixel_b(self.into())
56    }
57
58    /// Extracts the 8 bit green component from an ABGR pixel.
59    pub fn g(self) -> u8 {
60        c_api::ncpixel_g(self.into())
61    }
62
63    /// Extracts the 8 bit red component from an ABGR pixel.
64    pub fn r(self) -> u8 {
65        c_api::ncpixel_r(self.into())
66    }
67
68    /// Sets the 8-bit alpha component of an ABGR pixel.
69    pub fn set_a(&mut self, alpha: u8) {
70        c_api::ncpixel_set_a(self.into(), alpha)
71    }
72
73    /// Sets the 8-bit green component of an ABGR pixel.
74    pub fn set_g(&mut self, green: u8) {
75        c_api::ncpixel_set_b(self.into(), green)
76    }
77
78    /// Sets the 8-bit blue component of an ABGR pixel.
79    pub fn set_b(&mut self, blue: u8) {
80        c_api::ncpixel_set_b(self.into(), blue)
81    }
82
83    /// Sets the 8-bit red component of an ABGR pixel.
84    pub fn set_r(&mut self, red: u8) {
85        c_api::ncpixel_set_r(self.into(), red)
86    }
87
88    /// Sets the RGB components of an ABGR pixel.
89    pub fn set_rgb(&mut self, rgb: impl Into<NcRgb>) {
90        let (r, g, b) = rgb.into().into();
91        c_api::ncpixel_set_rgb8(self.into(), r, g, b);
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    #[test]
100    fn abgr() {
101        let rgba: NcRgba = 0x11223344.into();
102        let abgr: NcPixel = rgba.into();
103
104        assert_eq![0x44332211_u32, abgr.into()];
105    }
106}