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;
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Brightness {
brightness: f32,
}
impl Brightness {
#[must_use]
pub fn new(brightness: f32) -> Option<Self> {
if (0.0..=1.0).contains(&brightness) {
Some(Self { brightness })
} else {
None
}
}
#[must_use]
pub fn with_clamped(brightness: f32) -> Self {
Self {
brightness: brightness.clamp(0.0, 1.0),
}
}
#[must_use]
pub fn as_f32(self) -> f32 {
self.brightness
}
}
pub trait BrightnessExt {
fn brightness(&self) -> Brightness;
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(())
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Gamma {
pub red: GammaRamp,
pub green: GammaRamp,
pub blue: GammaRamp,
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct GammaParam {
pub red: Option<GammaRamp>,
pub green: Option<GammaRamp>,
pub blue: Option<GammaRamp>,
}
pub trait GammaExt {
fn gamma(&self) -> Result<Gamma>;
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(())
}
}