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) {
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    }
29
30    /// Get the current pixel format
31    pub fn get_pixel_format(&self) -> PixelFormat {
32        unsafe {
33            let value = crate::ffi::sc_stream_configuration_get_pixel_format(self.as_ptr());
34            PixelFormat::from(value)
35        }
36    }
37
38    /// Set the background color for captured content
39    ///
40    /// Available on macOS 13.0+
41    ///
42    /// # Parameters
43    ///
44    /// - `r`: Red component (0.0 - 1.0)
45    /// - `g`: Green component (0.0 - 1.0)
46    /// - `b`: Blue component (0.0 - 1.0)
47    pub fn set_background_color(&mut self, r: f32, g: f32, b: f32) {
48        unsafe {
49            crate::ffi::sc_stream_configuration_set_background_color(self.as_ptr(), r, g, b);
50        }
51    }
52
53    /// Set the color space name for captured content
54    ///
55    /// Available on macOS 13.0+
56    pub fn set_color_space_name(&mut self, name: &str) {
57        if let Ok(c_name) = std::ffi::CString::new(name) {
58            unsafe {
59                crate::ffi::sc_stream_configuration_set_color_space_name(
60                    self.as_ptr(),
61                    c_name.as_ptr(),
62                );
63            }
64        }
65    }
66
67    /// Set the color matrix for captured content
68    ///
69    /// Available on macOS 13.0+. The matrix should be a 3x3 array in row-major order.
70    pub fn set_color_matrix(&mut self, matrix: &str) {
71        if let Ok(c_matrix) = std::ffi::CString::new(matrix) {
72            unsafe {
73                crate::ffi::sc_stream_configuration_set_color_matrix(
74                    self.as_ptr(),
75                    c_matrix.as_ptr(),
76                );
77            }
78        }
79    }
80}