1use std::fmt;
4
5use glib::translate::*;
6
7use crate::{ffi, Color};
8
9impl Color {
10 #[doc(alias = "pango_color_parse")]
11 pub fn parse(spec: &str) -> Result<Self, glib::BoolError> {
12 unsafe {
13 let mut color = Self::uninitialized();
14 let is_success =
15 ffi::pango_color_parse(color.to_glib_none_mut().0, spec.to_glib_none().0);
16 if from_glib(is_success) {
17 Ok(color)
18 } else {
19 Err(glib::bool_error!("Failed to parse the color"))
20 }
21 }
22 }
23
24 #[cfg(feature = "v1_46")]
25 #[cfg_attr(docsrs, doc(cfg(feature = "v1_46")))]
26 #[doc(alias = "pango_color_parse_with_alpha")]
27 pub fn parse_with_alpha(spec: &str) -> Result<(Self, u16), glib::BoolError> {
28 unsafe {
29 let mut color = Self::uninitialized();
30 let mut alpha = std::mem::MaybeUninit::uninit();
31 let is_success = ffi::pango_color_parse_with_alpha(
32 color.to_glib_none_mut().0,
33 alpha.as_mut_ptr(),
34 spec.to_glib_none().0,
35 );
36 if from_glib(is_success) {
37 Ok((color, alpha.assume_init()))
38 } else {
39 Err(glib::bool_error!("Failed to parse the color with alpha"))
40 }
41 }
42 }
43
44 pub fn red(&self) -> u16 {
45 unsafe { *self.to_glib_none().0 }.red
46 }
47
48 pub fn green(&self) -> u16 {
49 unsafe { *self.to_glib_none().0 }.green
50 }
51
52 pub fn blue(&self) -> u16 {
53 unsafe { *self.to_glib_none().0 }.blue
54 }
55}
56
57impl fmt::Debug for Color {
58 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59 f.debug_struct("Color")
60 .field("red", &self.red())
61 .field("green", &self.green())
62 .field("blue", &self.blue())
63 .finish()
64 }
65}
66
67impl std::str::FromStr for Color {
68 type Err = glib::BoolError;
69 fn from_str(s: &str) -> Result<Self, Self::Err> {
70 Color::parse(s)
71 }
72}