Skip to content

Commit 8d2fb44

Browse files
SeaDvebilelmoussaoui
authored andcommitted
gdk: Simplify RGBABuilder code
1 parent 274c3a9 commit 8d2fb44

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

gdk4/src/rgba.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ use crate::RGBA;
44
use glib::{translate::*, IntoGStr};
55
use std::{fmt, str::FromStr};
66

7-
#[derive(Debug, Default)]
7+
#[derive(Debug)]
88
// rustdoc-stripper-ignore-next
99
/// A [builder-pattern] type to construct [`RGBA`] objects.
1010
///
1111
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
1212
#[must_use = "The builder must be built to be used"]
13-
pub struct RGBABuilder {
14-
red: Option<f32>,
15-
green: Option<f32>,
16-
blue: Option<f32>,
17-
alpha: Option<f32>,
13+
pub struct RGBABuilder(RGBA);
14+
15+
impl Default for RGBABuilder {
16+
fn default() -> Self {
17+
Self(RGBA::WHITE)
18+
}
1819
}
1920

2021
impl RGBABuilder {
@@ -25,43 +26,30 @@ impl RGBABuilder {
2526
}
2627

2728
pub fn blue(mut self, blue: f32) -> Self {
28-
self.blue = Some(blue);
29+
self.0.set_blue(blue);
2930
self
3031
}
3132

3233
pub fn green(mut self, green: f32) -> Self {
33-
self.green = Some(green);
34+
self.0.set_green(green);
3435
self
3536
}
3637

3738
pub fn red(mut self, red: f32) -> Self {
38-
self.red = Some(red);
39+
self.0.set_red(red);
3940
self
4041
}
4142

4243
pub fn alpha(mut self, alpha: f32) -> Self {
43-
self.alpha = Some(alpha);
44+
self.0.set_alpha(alpha);
4445
self
4546
}
4647

4748
// rustdoc-stripper-ignore-next
4849
/// Build the [`RGBA`].
4950
#[must_use = "The RGBA returned by this builder should probably be used"]
5051
pub fn build(self) -> RGBA {
51-
let mut rgba = RGBA::WHITE;
52-
if let Some(blue) = self.blue {
53-
rgba.set_blue(blue);
54-
}
55-
if let Some(red) = self.red {
56-
rgba.set_red(red);
57-
}
58-
if let Some(green) = self.green {
59-
rgba.set_green(green);
60-
}
61-
if let Some(alpha) = self.alpha {
62-
rgba.set_alpha(alpha);
63-
}
64-
rgba
52+
self.0
6553
}
6654
}
6755

0 commit comments

Comments
 (0)