pango/
color.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::fmt;
4
5use glib::translate::*;
6
7use crate::{ffi, Color};
8
9impl Color {
10    /// Fill in the fields of a color from a string specification.
11    ///
12    /// The string can either one of a large set of standard names.
13    /// (Taken from the CSS Color [specification](https://www.w3.org/TR/css-color-4/#named-colors),
14    /// or it can be a value in the form `#rgb`, `#rrggbb`,
15    /// `#rrrgggbbb` or `#rrrrggggbbbb`, where `r`, `g` and `b`
16    /// are hex digits of the red, green, and blue components
17    /// of the color, respectively. (White in the four forms is
18    /// `#fff`, `#ffffff`, `#fffffffff` and `#ffffffffffff`.)
19    /// ## `spec`
20    /// a string specifying the new color
21    ///
22    /// # Returns
23    ///
24    /// [`true`] if parsing of the specifier succeeded,
25    ///   otherwise [`false`]
26    #[doc(alias = "pango_color_parse")]
27    pub fn parse(spec: &str) -> Result<Self, glib::BoolError> {
28        unsafe {
29            let mut color = Self::uninitialized();
30            let is_success =
31                ffi::pango_color_parse(color.to_glib_none_mut().0, spec.to_glib_none().0);
32            if from_glib(is_success) {
33                Ok(color)
34            } else {
35                Err(glib::bool_error!("Failed to parse the color"))
36            }
37        }
38    }
39
40    /// Fill in the fields of a color from a string specification.
41    ///
42    /// The string can either one of a large set of standard names.
43    /// (Taken from the CSS Color [specification](https://www.w3.org/TR/css-color-4/#named-colors),
44    /// or it can be a hexadecimal value in the form `#rgb`,
45    /// `#rrggbb`, `#rrrgggbbb` or `#rrrrggggbbbb` where `r`, `g`
46    /// and `b` are hex digits of the red, green, and blue components
47    /// of the color, respectively. (White in the four forms is
48    /// `#fff`, `#ffffff`, `#fffffffff` and `#ffffffffffff`.)
49    ///
50    /// Additionally, parse strings of the form `#rgba`, `#rrggbbaa`,
51    /// `#rrrrggggbbbbaaaa`, if @alpha is not [`None`], and set @alpha
52    /// to the value specified by the hex digits for `a`. If no alpha
53    /// component is found in @spec, @alpha is set to 0xffff (for a
54    /// solid color).
55    /// ## `spec`
56    /// a string specifying the new color
57    ///
58    /// # Returns
59    ///
60    /// [`true`] if parsing of the specifier succeeded,
61    ///   otherwise [`false`]
62    ///
63    /// ## `alpha`
64    /// return location for alpha
65    #[cfg(feature = "v1_46")]
66    #[cfg_attr(docsrs, doc(cfg(feature = "v1_46")))]
67    #[doc(alias = "pango_color_parse_with_alpha")]
68    pub fn parse_with_alpha(spec: &str) -> Result<(Self, u16), glib::BoolError> {
69        unsafe {
70            let mut color = Self::uninitialized();
71            let mut alpha = std::mem::MaybeUninit::uninit();
72            let is_success = ffi::pango_color_parse_with_alpha(
73                color.to_glib_none_mut().0,
74                alpha.as_mut_ptr(),
75                spec.to_glib_none().0,
76            );
77            if from_glib(is_success) {
78                Ok((color, alpha.assume_init()))
79            } else {
80                Err(glib::bool_error!("Failed to parse the color with alpha"))
81            }
82        }
83    }
84
85    pub fn red(&self) -> u16 {
86        unsafe { *self.to_glib_none().0 }.red
87    }
88
89    pub fn green(&self) -> u16 {
90        unsafe { *self.to_glib_none().0 }.green
91    }
92
93    pub fn blue(&self) -> u16 {
94        unsafe { *self.to_glib_none().0 }.blue
95    }
96}
97
98impl fmt::Debug for Color {
99    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100        f.debug_struct("Color")
101            .field("red", &self.red())
102            .field("green", &self.green())
103            .field("blue", &self.blue())
104            .finish()
105    }
106}
107
108impl std::str::FromStr for Color {
109    type Err = glib::BoolError;
110    fn from_str(s: &str) -> Result<Self, Self::Err> {
111        Color::parse(s)
112    }
113}