libnotcurses_sys/pixel/
methods.rs1use crate::{
2 c_api::{self, NcPixel_u32},
3 NcPixel, NcRgb, NcRgba,
4};
5
6impl NcPixel {
7 pub fn new(value: c_api::NcPixel_u32) -> Self {
9 Self(value)
10 }
11
12 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 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 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 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 pub fn a(self) -> u8 {
50 c_api::ncpixel_a(self.into())
51 }
52
53 pub fn b(self) -> u8 {
55 c_api::ncpixel_b(self.into())
56 }
57
58 pub fn g(self) -> u8 {
60 c_api::ncpixel_g(self.into())
61 }
62
63 pub fn r(self) -> u8 {
65 c_api::ncpixel_r(self.into())
66 }
67
68 pub fn set_a(&mut self, alpha: u8) {
70 c_api::ncpixel_set_a(self.into(), alpha)
71 }
72
73 pub fn set_g(&mut self, green: u8) {
75 c_api::ncpixel_set_b(self.into(), green)
76 }
77
78 pub fn set_b(&mut self, blue: u8) {
80 c_api::ncpixel_set_b(self.into(), blue)
81 }
82
83 pub fn set_r(&mut self, red: u8) {
85 c_api::ncpixel_set_r(self.into(), red)
86 }
87
88 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}