Skip to content

Commit

Permalink
refactor: Color.color is a tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 authored and bilelmoussaoui committed Mar 4, 2024
1 parent 234d331 commit 358f0c7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
29 changes: 8 additions & 21 deletions src/desktop/color.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
use crate::{
zvariant::{self, DeserializeDict, Type},
Error,
};
use crate::zvariant::{self, DeserializeDict, Type};

#[derive(DeserializeDict, Clone, Copy, PartialEq, Type)]
#[derive(DeserializeDict, Clone, Copy, PartialEq, Type, zvariant::Value, zvariant::OwnedValue)]
/// A color as a RGB tuple.
///
/// **Note** the values are normalized in the [0.0, 1.0] range.
#[zvariant(signature = "dict")]
pub struct Color {
color: [f64; 3],
color: (f64, f64, f64),
}

impl Color {
pub(crate) fn new(r: f64, g: f64, b: f64) -> Self {
Self { color: [r, g, b] }
pub(crate) fn new(color: (f64, f64, f64)) -> Self {
Self { color }
}

/// Red.
pub fn red(&self) -> f64 {
self.color[0]
self.color.0
}

/// Green.
pub fn green(&self) -> f64 {
self.color[1]
self.color.1
}

/// Blue.
pub fn blue(&self) -> f64 {
self.color[2]
self.color.2
}
}

Expand All @@ -44,16 +41,6 @@ impl From<Color> for gtk4::gdk::RGBA {
}
}

impl TryFrom<zvariant::OwnedValue> for Color {
type Error = Error;

fn try_from(value: zvariant::OwnedValue) -> Result<Self, Self::Error> {
value
.try_into()
.map_err(|_| crate::Error::ParseError("Failed to parse color"))
}
}

impl std::fmt::Debug for Color {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Color")
Expand Down
4 changes: 2 additions & 2 deletions src/desktop/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl<'a> Settings<'a> {
pub async fn accent_color(&self) -> Result<Color, Error> {
self.read::<(f64, f64, f64)>(APPEARANCE_NAMESPACE, ACCENT_COLOR_SCHEME_KEY)
.await
.map(|(r, g, b)| Color::new(r, g, b))
.map(Color::new)
}

/// Retrieves the system's preferred color scheme
Expand Down Expand Up @@ -244,7 +244,7 @@ impl<'a> Settings<'a> {
ACCENT_COLOR_SCHEME_KEY,
)
.await?
.filter_map(|t| ready(t.ok().map(|(r, g, b)| Color::new(r, g, b)))))
.filter_map(|t| ready(t.ok().map(Color::new))))
}

/// Listen to changes of the system's contrast level
Expand Down

0 comments on commit 358f0c7

Please sign in to comment.