Skip to main content

gdk/
rgba.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::Uninitialized;
4
5use crate::RGBA;
6use glib::translate::*;
7use std::fmt;
8use std::str::FromStr;
9
10impl RGBA {
11    pub fn new(red: f64, green: f64, blue: f64, alpha: f64) -> RGBA {
12        skip_assert_initialized!();
13        unsafe {
14            RGBA::unsafe_from(ffi::GdkRGBA {
15                red,
16                green,
17                blue,
18                alpha,
19            })
20        }
21    }
22
23    pub fn red(&self) -> f64 {
24        self.inner.red
25    }
26
27    pub fn set_red(&mut self, red: f64) {
28        self.inner.red = red;
29    }
30
31    pub fn green(&self) -> f64 {
32        self.inner.green
33    }
34
35    pub fn set_green(&mut self, green: f64) {
36        self.inner.green = green;
37    }
38
39    pub fn blue(&self) -> f64 {
40        self.inner.blue
41    }
42
43    pub fn set_blue(&mut self, blue: f64) {
44        self.inner.blue = blue;
45    }
46
47    pub fn alpha(&self) -> f64 {
48        self.inner.alpha
49    }
50
51    pub fn set_alpha(&mut self, alpha: f64) {
52        self.inner.alpha = alpha;
53    }
54
55    /// Parses a textual representation of a color, filling in
56    /// the `red`, `green`, `blue` and `alpha` fields of the `self` [`RGBA`][crate::RGBA].
57    ///
58    /// The string can be either one of:
59    /// - A standard name (Taken from the X11 rgb.txt file).
60    /// - A hexadecimal value in the form “\`rgb`”, “\`rrggbb`”,
61    ///  “\`rrrgggbbb`” or ”\`rrrrggggbbbb`”
62    /// - A RGB color in the form “rgb(r,g,b)” (In this case the color will
63    ///  have full opacity)
64    /// - A RGBA color in the form “rgba(r,g,b,a)”
65    ///
66    /// Where “r”, “g”, “b” and “a” are respectively the red, green, blue and
67    /// alpha color values. In the last two cases, “r”, “g”, and “b” are either integers
68    /// in the range 0 to 255 or percentage values in the range 0% to 100%, and
69    /// a is a floating point value in the range 0 to 1.
70    /// ## `spec`
71    /// the string specifying the color
72    ///
73    /// # Returns
74    ///
75    /// [`true`] if the parsing succeeded
76    #[doc(alias = "gdk_rgba_parse")]
77    pub fn parse(s: &str) -> Result<Self, glib::error::BoolError> {
78        skip_assert_initialized!();
79        unsafe {
80            let mut res = RGBA::uninitialized();
81            glib::result_from_gboolean!(
82                ffi::gdk_rgba_parse(res.to_glib_none_mut().0, s.to_glib_none().0),
83                "Can't parse RGBA"
84            )
85            .map(|_| res)
86        }
87    }
88
89    pub const BLACK: RGBA = RGBA {
90        inner: ffi::GdkRGBA {
91            red: 0f64,
92            green: 0f64,
93            blue: 0f64,
94            alpha: 1f64,
95        },
96    };
97
98    pub const BLUE: RGBA = RGBA {
99        inner: ffi::GdkRGBA {
100            red: 0f64,
101            green: 0f64,
102            blue: 1f64,
103            alpha: 1f64,
104        },
105    };
106
107    pub const GREEN: RGBA = RGBA {
108        inner: ffi::GdkRGBA {
109            red: 0f64,
110            green: 1f64,
111            blue: 0f64,
112            alpha: 1f64,
113        },
114    };
115
116    pub const RED: RGBA = RGBA {
117        inner: ffi::GdkRGBA {
118            red: 1f64,
119            green: 0f64,
120            blue: 0f64,
121            alpha: 1f64,
122        },
123    };
124
125    pub const WHITE: RGBA = RGBA {
126        inner: ffi::GdkRGBA {
127            red: 1f64,
128            green: 1f64,
129            blue: 1f64,
130            alpha: 1f64,
131        },
132    };
133}
134
135impl fmt::Debug for RGBA {
136    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
137        f.debug_struct("RGBA")
138            .field("red", &self.red())
139            .field("green", &self.green())
140            .field("blue", &self.blue())
141            .field("alpha", &self.alpha())
142            .finish()
143    }
144}
145
146impl FromStr for RGBA {
147    type Err = glib::BoolError;
148    fn from_str(s: &str) -> Result<Self, Self::Err> {
149        skip_assert_initialized!();
150        RGBA::parse(s)
151    }
152}