1use gdk_sys;
6use glib;
7use glib::translate::*;
8use glib_sys::gconstpointer;
9use gobject_sys;
10use std::fmt;
11use std::hash::{Hash, Hasher};
12use std::mem;
13use std::str::FromStr;
14
15#[derive(Debug)]
16pub struct RgbaParseError;
17
18#[derive(Clone, Copy, Debug)]
19#[repr(C)]
20pub struct RGBA {
21 pub red: f64,
22 pub green: f64,
23 pub blue: f64,
24 pub alpha: f64,
25}
26
27impl RGBA {
28 pub fn black() -> RGBA {
29 skip_assert_initialized!();
30 RGBA {
31 red: 0f64,
32 green: 0f64,
33 blue: 0f64,
34 alpha: 1f64,
35 }
36 }
37
38 pub fn blue() -> RGBA {
39 skip_assert_initialized!();
40 RGBA {
41 red: 0f64,
42 green: 0f64,
43 blue: 1f64,
44 alpha: 1f64,
45 }
46 }
47
48 pub fn green() -> RGBA {
49 skip_assert_initialized!();
50 RGBA {
51 red: 0f64,
52 green: 1f64,
53 blue: 0f64,
54 alpha: 1f64,
55 }
56 }
57
58 pub fn red() -> RGBA {
59 skip_assert_initialized!();
60 RGBA {
61 red: 1f64,
62 green: 0f64,
63 blue: 0f64,
64 alpha: 1f64,
65 }
66 }
67
68 pub fn white() -> RGBA {
69 skip_assert_initialized!();
70 RGBA {
71 red: 1f64,
72 green: 1f64,
73 blue: 1f64,
74 alpha: 1f64,
75 }
76 }
77}
78
79impl fmt::Display for RGBA {
80 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81 let string: glib::GString =
82 unsafe { from_glib_full(gdk_sys::gdk_rgba_to_string(self.to_glib_none().0)) };
83 f.write_str(&string)
84 }
85}
86
87impl FromStr for RGBA {
88 type Err = RgbaParseError;
89 fn from_str(s: &str) -> Result<Self, Self::Err> {
90 skip_assert_initialized!();
91 unsafe {
92 let mut rgba = RGBA::uninitialized();
93 if from_glib(gdk_sys::gdk_rgba_parse(
94 rgba.to_glib_none_mut().0,
95 s.to_glib_none().0,
96 )) {
97 Ok(rgba)
98 } else {
99 Err(RgbaParseError)
100 }
101 }
102 }
103}
104
105impl Hash for RGBA {
106 fn hash<H: Hasher>(&self, state: &mut H) {
107 let hash = unsafe { gdk_sys::gdk_rgba_hash(self.to_glib_none().0 as gconstpointer) };
108 state.write_u32(hash);
109 }
110}
111
112impl PartialEq for RGBA {
113 fn eq(&self, other: &RGBA) -> bool {
114 unsafe {
115 from_glib(gdk_sys::gdk_rgba_equal(
116 self.to_glib_none().0 as gconstpointer,
117 other.to_glib_none().0 as gconstpointer,
118 ))
119 }
120 }
121}
122
123impl Eq for RGBA {}
124
125#[doc(hidden)]
126impl Uninitialized for RGBA {
127 #[inline]
128 unsafe fn uninitialized() -> Self {
129 mem::uninitialized()
130 }
131}
132
133#[doc(hidden)]
134impl<'a> ToGlibPtr<'a, *const gdk_sys::GdkRGBA> for RGBA {
135 type Storage = &'a Self;
136
137 #[inline]
138 fn to_glib_none(&'a self) -> Stash<'a, *const gdk_sys::GdkRGBA, Self> {
139 let ptr: *const RGBA = &*self;
140 Stash(ptr as *const gdk_sys::GdkRGBA, self)
141 }
142}
143
144#[doc(hidden)]
145impl<'a> ToGlibPtrMut<'a, *mut gdk_sys::GdkRGBA> for RGBA {
146 type Storage = &'a mut Self;
147
148 #[inline]
149 fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut gdk_sys::GdkRGBA, Self> {
150 let ptr: *mut RGBA = &mut *self;
151 StashMut(ptr as *mut gdk_sys::GdkRGBA, self)
152 }
153}
154
155#[doc(hidden)]
156impl FromGlibPtrNone<*const gdk_sys::GdkRGBA> for RGBA {
157 unsafe fn from_glib_none(ptr: *const gdk_sys::GdkRGBA) -> Self {
158 *(ptr as *const RGBA)
159 }
160}
161
162#[doc(hidden)]
163impl FromGlibPtrNone<*mut gdk_sys::GdkRGBA> for RGBA {
164 unsafe fn from_glib_none(ptr: *mut gdk_sys::GdkRGBA) -> Self {
165 *(ptr as *mut RGBA)
166 }
167}
168
169#[doc(hidden)]
170impl FromGlibPtrBorrow<*const gdk_sys::GdkRGBA> for RGBA {
171 unsafe fn from_glib_borrow(ptr: *const gdk_sys::GdkRGBA) -> Self {
172 *(ptr as *const RGBA)
173 }
174}
175
176#[doc(hidden)]
177impl FromGlibPtrBorrow<*mut gdk_sys::GdkRGBA> for RGBA {
178 unsafe fn from_glib_borrow(ptr: *mut gdk_sys::GdkRGBA) -> Self {
179 *(ptr as *mut RGBA)
180 }
181}
182
183#[doc(hidden)]
184impl FromGlibPtrFull<*mut gdk_sys::GdkRGBA> for RGBA {
185 #[inline]
186 unsafe fn from_glib_full(ptr: *mut gdk_sys::GdkRGBA) -> Self {
187 let rgba = *(ptr as *mut RGBA);
188 gdk_sys::gdk_rgba_free(ptr);
189 rgba
190 }
191}
192
193impl glib::StaticType for RGBA {
194 fn static_type() -> glib::types::Type {
195 skip_assert_initialized!();
196 unsafe { from_glib(gdk_sys::gdk_rgba_get_type()) }
197 }
198}
199
200impl<'a> glib::value::FromValueOptional<'a> for RGBA {
201 unsafe fn from_value_optional(value: &'a glib::Value) -> Option<Self> {
202 from_glib_full(
203 gobject_sys::g_value_dup_boxed(value.to_glib_none().0) as *mut gdk_sys::GdkRGBA
204 )
205 }
206}
207
208impl glib::value::SetValue for RGBA {
209 unsafe fn set_value(value: &mut glib::Value, this: &Self) {
210 gobject_sys::g_value_set_boxed(
211 value.to_glib_none_mut().0,
212 this.to_glib_none().0 as gconstpointer,
213 )
214 }
215}
216
217impl glib::value::SetValueOptional for RGBA {
218 unsafe fn set_value_optional(value: &mut glib::Value, this: Option<&Self>) {
219 gobject_sys::g_value_set_boxed(
220 value.to_glib_none_mut().0,
221 this.to_glib_none().0 as gconstpointer,
222 )
223 }
224}