pride_overlay/opacity.rs
1/// Represents an opacity value, ranging from 0% (fully transparent) to 100% (fully opaque).
2pub struct Opacity(u8);
3
4impl Opacity {
5 /// Fully transparent (0%)
6 pub const TRANSPARENT: Self = Self(0);
7 /// Half transparent (50%)
8 pub const HALF: Self = Self(128);
9 /// Fully opaque (100%)
10 pub const OPAQUE: Self = Self(255);
11
12 /// Creates an [Opacity] from a percentage value.
13 ///
14 /// This percentage is clamped between 0.0 and 1.0, then mapped to a [u8].
15 pub const fn new(percentage: f32) -> Self {
16 // clamp between 0% and 100%
17 let clamped = percentage.clamp(0.0, 1.);
18 // turn into a u8 value between 0 and 255
19 Self((clamped * u8::MAX as f32) as u8)
20 }
21
22 /// Returns the [Opacity] as a percentage value
23 pub const fn get(self) -> f32 {
24 (self.0 as f32 / u8::MAX as f32) * 100.
25 }
26
27 /// Creates an [Opacity] with the given raw [u8] value.
28 ///
29 /// This function returns an [Option] for ease of consumption, it will never be [None].
30 pub const fn raw(opacity: u8) -> Option<Self> {
31 Some(Self(opacity))
32 }
33
34 /// Returns the raw [u8] value of the [Opacity].
35 pub const fn get_raw(&self) -> u8 {
36 self.0
37 }
38}