1//! Change the icon of a window.
2use crate::Size;
34use std::mem;
56/// Builds an [`Icon`] from its RGBA pixels in the `sRGB` color space.
7pub fn from_rgba(
8 rgba: Vec<u8>,
9 width: u32,
10 height: u32,
11) -> Result<Icon, Error> {
12const PIXEL_SIZE: usize = mem::size_of::<u8>() * 4;
1314if rgba.len() % PIXEL_SIZE != 0 {
15return Err(Error::ByteCountNotDivisibleBy4 {
16 byte_count: rgba.len(),
17 });
18 }
1920let pixel_count = rgba.len() / PIXEL_SIZE;
2122if pixel_count != (width * height) as usize {
23return Err(Error::DimensionsVsPixelCount {
24 width,
25 height,
26 width_x_height: (width * height) as usize,
27 pixel_count,
28 });
29 }
3031Ok(Icon {
32 rgba,
33 size: Size::new(width, height),
34 })
35}
3637/// An window icon normally used for the titlebar or taskbar.
38#[derive(Debug, Clone)]
39pub struct Icon {
40 rgba: Vec<u8>,
41 size: Size<u32>,
42}
4344impl Icon {
45/// Returns the raw data of the [`Icon`].
46pub fn into_raw(self) -> (Vec<u8>, Size<u32>) {
47 (self.rgba, self.size)
48 }
49}
5051#[derive(Debug, thiserror::Error)]
52/// An error produced when using [`from_rgba`] with invalid arguments.
53pub enum Error {
54/// Produced when the length of the `rgba` argument isn't divisible by 4, thus `rgba` can't be
55 /// safely interpreted as 32bpp RGBA pixels.
56#[error(
57"The provided RGBA data (with length {byte_count}) isn't divisible \
58 by 4. Therefore, it cannot be safely interpreted as 32bpp RGBA pixels"
59)]
60ByteCountNotDivisibleBy4 {
61/// The length of the provided RGBA data.
62byte_count: usize,
63 },
64/// Produced when the number of pixels (`rgba.len() / 4`) isn't equal to `width * height`.
65 /// At least one of your arguments is incorrect.
66#[error(
67"The number of RGBA pixels ({pixel_count}) does not match the \
68 provided dimensions ({width}x{height})."
69)]
70DimensionsVsPixelCount {
71/// The provided width.
72width: u32,
73/// The provided height.
74height: u32,
75/// The product of `width` and `height`.
76width_x_height: usize,
77/// The amount of pixels of the provided RGBA data.
78pixel_count: usize,
79 },
80}