Add support for transarent png images

This commit is contained in:
Nick Hahn
2021-12-28 02:27:37 +01:00
parent 95af2a0aca
commit b7330e6da6
2 changed files with 7 additions and 5 deletions

View File

@@ -6,19 +6,20 @@ pub struct Color {
r: u8, r: u8,
g: u8, g: u8,
b: u8, b: u8,
a: u8,
} }
impl Color { impl Color {
/// Constructor. /// Constructor.
/// ///
/// The color channels must be between 0 and 255. /// The color channels must be between 0 and 255.
pub fn from(r: u8, g: u8, b: u8) -> Color { pub fn from(r: u8, g: u8, b: u8, a: u8) -> Color {
Color { r, g, b } Color { r, g, b, a}
} }
/// Get a hexadecimal representation of the color, /// Get a hexadecimal representation of the color,
/// such as `FFFFFF` for white and `FF0000` for red. /// such as `FFFFFF` for white and `FF0000` for red.
pub fn as_hex(self) -> String { pub fn as_hex(self) -> String {
format!("{:02X}{:02X}{:02X}", self.r, self.g, self.b) format!("{:02X}{:02X}{:02X}{:02X}", self.r, self.g, self.b, self.a)
} }
} }

View File

@@ -51,7 +51,7 @@ impl Painter {
} }
// Get an RGB image // Get an RGB image
let image = self.image.as_mut().unwrap().to_rgb(); let image = self.image.as_mut().unwrap().to_rgba();
// Loop through all the pixels, and set their color // Loop through all the pixels, and set their color
for x in 0..self.area.w { for x in 0..self.area.w {
@@ -68,7 +68,8 @@ impl Painter {
let channels = pixel.channels(); let channels = pixel.channels();
// Define the color // Define the color
let color = Color::from(channels[0], channels[1], channels[2]); let color = Color::from(channels[0], channels[1], channels[2], channels[3]);
// Set the pixel // Set the pixel
if let Some(client) = &mut self.client { if let Some(client) = &mut self.client {