Debian Codegraph @

src/desktop/color.rs

raw
1 use serde::{Deserialize, Serialize};
2
3 use crate::zvariant::{self, Type, as_value};
4
5 #[derive(
6 Serialize, Deserialize, Clone, Copy, PartialEq, Type, zvariant::Value, zvariant::OwnedValue,
7 )]
8 /// A color as a RGB tuple.
9 ///
10 /// **Note** the values are normalized in the [0.0, 1.0] range.
11 #[zvariant(signature = "dict")]
12 pub struct Color {
13 #[serde(with = "as_value")]
14 color: (f64, f64, f64),
15 }
16
17 impl From<(f64, f64, f64)> for Color {
18 fn from(value: (f64, f64, f64)) -> Self {
19 Self::new(value.0, value.1, value.2)
20 }
21 }
22
23 impl Color {
24 /// Create a new instance of Color.
25 pub fn new(red: f64, green: f64, blue: f64) -> Self {
26 Self {
27 color: (red, green, blue),
28 }
29 }
30
31 /// Red.
32 pub fn red(&self) -> f64 {
33 self.color.0
34 }
35
36 /// Green.
37 pub fn green(&self) -> f64 {
38 self.color.1
39 }
40
41 /// Blue.
42 pub fn blue(&self) -> f64 {
43 self.color.2
44 }
45 }
46
47 #[cfg(feature = "gtk4")]
48 impl From<Color> for gtk4::gdk::RGBA {
49 fn from(color: Color) -> Self {
50 gtk4::gdk::RGBA::builder()
51 .red(color.red() as f32)
52 .green(color.green() as f32)
53 .blue(color.blue() as f32)
54 .build()
55 }
56 }
57
58 impl std::fmt::Debug for Color {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 f.debug_struct("Color")
61 .field("red", &self.red())
62 .field("green", &self.green())
63 .field("blue", &self.blue())
64 .finish()
65 }
66 }
67
68 impl std::fmt::Display for Color {
69 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70 f.write_str(&format!(
71 "({}, {}, {})",
72 self.red(),
73 self.green(),
74 self.blue()
75 ))
76 }
77 }