Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gdk: Simplify RGBA builder code #1483

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 23 additions & 77 deletions gdk4/src/rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ use crate::RGBA;
use glib::{translate::*, IntoGStr};
use std::{fmt, str::FromStr};

#[derive(Debug, Default)]
#[derive(Debug)]
// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`RGBA`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
#[must_use = "The builder must be built to be used"]
pub struct RGBABuilder {
red: Option<f32>,
green: Option<f32>,
blue: Option<f32>,
alpha: Option<f32>,
pub struct RGBABuilder(RGBA);

impl Default for RGBABuilder {
fn default() -> Self {
Self(RGBA::WHITE)
bilelmoussaoui marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl RGBABuilder {
Expand All @@ -25,43 +26,30 @@ impl RGBABuilder {
}

pub fn blue(mut self, blue: f32) -> Self {
self.blue = Some(blue);
self.0.set_blue(blue);
self
}

pub fn green(mut self, green: f32) -> Self {
self.green = Some(green);
self.0.set_green(green);
self
}

pub fn red(mut self, red: f32) -> Self {
self.red = Some(red);
self.0.set_red(red);
self
}

pub fn alpha(mut self, alpha: f32) -> Self {
self.alpha = Some(alpha);
self.0.set_alpha(alpha);
self
}

// rustdoc-stripper-ignore-next
/// Build the [`RGBA`].
#[must_use = "The RGBA returned by this builder should probably be used"]
pub fn build(self) -> RGBA {
let mut rgba = RGBA::WHITE;
if let Some(blue) = self.blue {
rgba.set_blue(blue);
}
if let Some(red) = self.red {
rgba.set_red(red);
}
if let Some(green) = self.green {
rgba.set_green(green);
}
if let Some(alpha) = self.alpha {
rgba.set_alpha(alpha);
}
rgba
self.0
}
}

Expand Down Expand Up @@ -220,59 +208,17 @@ impl RGBA {
}
}

pub const BLACK: RGBA = Self {
inner: ffi::GdkRGBA {
red: 0f32,
green: 0f32,
blue: 0f32,
alpha: 1f32,
},
};

pub const BLUE: RGBA = Self {
inner: ffi::GdkRGBA {
red: 0f32,
green: 0f32,
blue: 1f32,
alpha: 1f32,
},
};

pub const GREEN: RGBA = Self {
inner: ffi::GdkRGBA {
red: 0f32,
green: 1f32,
blue: 0f32,
alpha: 1f32,
},
};

pub const RED: RGBA = Self {
inner: ffi::GdkRGBA {
red: 1f32,
green: 0f32,
blue: 0f32,
alpha: 1f32,
},
};

pub const WHITE: RGBA = Self {
inner: ffi::GdkRGBA {
red: 1f32,
green: 1f32,
blue: 1f32,
alpha: 1f32,
},
};

pub const TRANSPARENT: RGBA = Self {
inner: ffi::GdkRGBA {
red: 0f32,
green: 0f32,
blue: 0f32,
alpha: 0f32,
},
};
pub const BLACK: RGBA = Self::new(0f32, 0f32, 0f32, 1f32);

pub const BLUE: RGBA = Self::new(0f32, 0f32, 1f32, 1f32);

pub const GREEN: RGBA = Self::new(0f32, 1f32, 0f32, 1f32);

pub const RED: RGBA = Self::new(1f32, 0f32, 0f32, 1f32);

pub const WHITE: RGBA = Self::new(1f32, 1f32, 1f32, 1f32);

pub const TRANSPARENT: RGBA = Self::new(0f32, 0f32, 0f32, 0f32);
}

impl fmt::Debug for RGBA {
Expand Down
Loading