screencapturekit/stream/configuration/
colors.rs

1//! Color and pixel format configuration
2//!
3//! Methods for configuring color space, pixel format, and background color.
4
5use crate::utils::four_char_code::FourCharCode;
6
7use super::{internal::SCStreamConfiguration, pixel_format::PixelFormat};
8
9impl SCStreamConfiguration {
10    /// Set the pixel format for captured frames
11    ///
12    /// # Examples
13    ///
14    /// ```
15    /// use screencapturekit::stream::configuration::{SCStreamConfiguration, PixelFormat};
16    ///
17    /// let mut config = SCStreamConfiguration::default();
18    /// config.set_pixel_format(PixelFormat::BGRA);
19    /// ```
20    pub fn set_pixel_format(&mut self, pixel_format: PixelFormat) -> &mut Self {
21        let four_char_code: FourCharCode = pixel_format.into();
22        unsafe {
23            crate::ffi::sc_stream_configuration_set_pixel_format(
24                self.as_ptr(),
25                four_char_code.as_u32(),
26            );
27        }
28        self
29    }
30
31    /// Set the pixel format (builder pattern)
32    #[must_use]
33    pub fn with_pixel_format(mut self, pixel_format: PixelFormat) -> Self {
34        self.set_pixel_format(pixel_format);
35        self
36    }
37
38    /// Get the current pixel format
39    pub fn pixel_format(&self) -> PixelFormat {
40        unsafe {
41            let value = crate::ffi::sc_stream_configuration_get_pixel_format(self.as_ptr());
42            PixelFormat::from(value)
43        }
44    }
45
46    /// Set the background color for captured content
47    ///
48    /// Available on macOS 13.0+
49    ///
50    /// # Parameters
51    ///
52    /// - `r`: Red component (0.0 - 1.0)
53    /// - `g`: Green component (0.0 - 1.0)
54    /// - `b`: Blue component (0.0 - 1.0)
55    pub fn set_background_color(&mut self, r: f32, g: f32, b: f32) -> &mut Self {
56        unsafe {
57            crate::ffi::sc_stream_configuration_set_background_color(self.as_ptr(), r, g, b);
58        }
59        self
60    }
61
62    /// Set the background color (builder pattern)
63    #[must_use]
64    pub fn with_background_color(mut self, r: f32, g: f32, b: f32) -> Self {
65        self.set_background_color(r, g, b);
66        self
67    }
68
69    /// Set the color space name for captured content
70    ///
71    /// Available on macOS 13.0+
72    pub fn set_color_space_name(&mut self, name: &str) -> &mut Self {
73        if let Ok(c_name) = std::ffi::CString::new(name) {
74            unsafe {
75                crate::ffi::sc_stream_configuration_set_color_space_name(
76                    self.as_ptr(),
77                    c_name.as_ptr(),
78                );
79            }
80        }
81        self
82    }
83
84    /// Set the color space name (builder pattern)
85    #[must_use]
86    pub fn with_color_space_name(mut self, name: &str) -> Self {
87        self.set_color_space_name(name);
88        self
89    }
90
91    /// Set the color matrix for captured content
92    ///
93    /// Available on macOS 13.0+. The matrix should be a 3x3 array in row-major order.
94    pub fn set_color_matrix(&mut self, matrix: &str) -> &mut Self {
95        if let Ok(c_matrix) = std::ffi::CString::new(matrix) {
96            unsafe {
97                crate::ffi::sc_stream_configuration_set_color_matrix(
98                    self.as_ptr(),
99                    c_matrix.as_ptr(),
100                );
101            }
102        }
103        self
104    }
105
106    /// Get the color matrix for captured content
107    ///
108    /// Returns the color matrix as a string, or None if not set.
109    pub fn color_matrix(&self) -> Option<String> {
110        let mut buffer = [0i8; 256];
111        let success = unsafe {
112            crate::ffi::sc_stream_configuration_get_color_matrix(
113                self.as_ptr(),
114                buffer.as_mut_ptr(),
115                buffer.len(),
116            )
117        };
118        if success {
119            let c_str = unsafe { std::ffi::CStr::from_ptr(buffer.as_ptr()) };
120            c_str.to_str().ok().map(ToString::to_string)
121        } else {
122            None
123        }
124    }
125
126    /// Set the color matrix (builder pattern)
127    #[must_use]
128    pub fn with_color_matrix(mut self, matrix: &str) -> Self {
129        self.set_color_matrix(matrix);
130        self
131    }
132}