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
use crate::gamma_ramp::GammaRamp;
use crate::{bind, Result, Sdl, SdlError};

use super::Window;

/// A brightness in the window.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Brightness {
    brightness: f32,
}

impl Brightness {
    /// Constructs from brightness, or `None` if the value is not in `0.0..=1.0`.
    #[must_use]
    pub fn new(brightness: f32) -> Option<Self> {
        if (0.0..=1.0).contains(&brightness) {
            Some(Self { brightness })
        } else {
            None
        }
    }

    /// Constructs from brightness, clamping to `0.0,,=1.0`.
    #[must_use]
    pub fn with_clamped(brightness: f32) -> Self {
        Self {
            brightness: brightness.clamp(0.0, 1.0),
        }
    }

    /// Converts into `f32`.
    #[must_use]
    pub fn as_f32(self) -> f32 {
        self.brightness
    }
}

/// An extension for [`Window`] to get/set the brightness.
pub trait BrightnessExt {
    /// Returns the brightness of the window.
    fn brightness(&self) -> Brightness;
    /// Sets the brightness of the Window.
    ///
    /// # Errors
    ///
    /// Returns `Err` if setting a brightness is unsupported.
    fn set_brightness(&self, brightness: Brightness) -> Result<()>;
}

impl BrightnessExt for Window<'_> {
    fn brightness(&self) -> Brightness {
        let brightness = unsafe { bind::SDL_GetWindowBrightness(self.as_ptr()) };
        Brightness { brightness }
    }

    fn set_brightness(&self, brightness: Brightness) -> Result<()> {
        let ret = unsafe { bind::SDL_SetWindowBrightness(self.as_ptr(), brightness.as_f32()) };
        if ret != 0 {
            return Err(SdlError::UnsupportedFeature);
        }
        Ok(())
    }
}

/// A gamma ramps for a window.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Gamma {
    /// A gamma ramp of red component.
    pub red: GammaRamp,
    /// A gamma ramp of green component.
    pub green: GammaRamp,
    /// A gamma ramp of blue component.
    pub blue: GammaRamp,
}

/// A gamma ramps for setting to a window.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct GammaParam {
    /// A gamma ramp of red component. It will not set if `None`.
    pub red: Option<GammaRamp>,
    /// A gamma ramp of green component. It will not set if `None`.
    pub green: Option<GammaRamp>,
    /// A gamma ramp of blue component. It will not set if `None`.
    pub blue: Option<GammaRamp>,
}

/// An extension for [`Window`] to get/set the gamma ramp.
pub trait GammaExt {
    /// Returns the gamma ramps of the window.
    ///
    /// # Errors
    ///
    /// Returns `Err` if failed to allocate a gamma ramp, or unsupported.
    fn gamma(&self) -> Result<Gamma>;
    /// Sets the gamma ramps of the window.
    ///
    /// # Errors
    ///
    /// Returns `Err` if failed to allocate a gamma ramp, or unsupported.
    fn set_gamma(&self, gamma: GammaParam) -> Result<()>;
}

impl GammaExt for Window<'_> {
    fn gamma(&self) -> Result<Gamma> {
        let mut gamma = Gamma::default();
        let ret = unsafe {
            bind::SDL_GetWindowGammaRamp(
                self.as_ptr(),
                gamma.red.0.as_mut_ptr().cast(),
                gamma.green.0.as_mut_ptr().cast(),
                gamma.blue.0.as_mut_ptr().cast(),
            )
        };
        if ret != 0 {
            let msg = Sdl::error();
            return Err(if msg == "Out of memory" {
                SdlError::OutOfMemory
            } else {
                SdlError::UnsupportedFeature
            });
        }
        Ok(gamma)
    }

    fn set_gamma(&self, GammaParam { red, green, blue }: GammaParam) -> Result<()> {
        let ramp_as_ptr =
            |ramp: Option<&GammaRamp>| ramp.map_or(std::ptr::null(), |ramp| ramp.0.as_ptr().cast());
        let ret = unsafe {
            bind::SDL_SetWindowGammaRamp(
                self.as_ptr(),
                ramp_as_ptr(red.as_ref()),
                ramp_as_ptr(green.as_ref()),
                ramp_as_ptr(blue.as_ref()),
            )
        };
        if ret != 0 {
            let msg = Sdl::error();
            return Err(if msg == "Out of memory" {
                SdlError::OutOfMemory
            } else {
                SdlError::UnsupportedFeature
            });
        }
        Ok(())
    }
}