summary refs log tree commit diff
path: root/src/x/color.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/x/color.rs')
-rw-r--r--src/x/color.rs45
1 files changed, 43 insertions, 2 deletions
diff --git a/src/x/color.rs b/src/x/color.rs
index 10ad651..d770379 100644
--- a/src/x/color.rs
+++ b/src/x/color.rs
@@ -7,9 +7,48 @@
 use crate::x::connection::*;
 use crate::x::display::*;
 
+use x11::xrender::XRenderColor;
+use x11::xft::XftColor;
 use xcb::x::{ AllocColor, Visualtype };
 
 
+pub struct Color {
+  /*   We need to store both the original 16-bit components and the pixel
+   * value. Additionally, there are situations in which we need to return
+   * an XftColor, which contains both of those. So, we might as well use it
+   * as the authoritative format.
+   *
+   *   We treat the contents as opaque, to allow us to pretend it's
+   * something cleaner. :)
+   */
+  font_color: XftColor,
+}
+
+
+impl Color {
+  /* Please notice that the constructor is private. */
+  fn new(pixel: u32, red: u16, green: u16, blue: u16) -> Self {
+    Color {
+      font_color: XftColor {
+	pixel: pixel as u64,
+	color: XRenderColor {
+	  red, green, blue,
+	  alpha: 65535,
+	}
+      }
+    }
+  }
+
+  pub fn as_pixel(&self) -> u32 {
+    self.font_color.pixel as u32
+  }
+
+  pub fn as_xft_color(&self) -> &XftColor {
+    &self.font_color
+  }
+}
+
+
 pub trait DisplayColorExt: DisplayExt {
   /*   This is sufficiently obscure knowledge that it's worth keeping here
    * even though it's unused.
@@ -30,7 +69,7 @@ pub trait DisplayColorExt: DisplayExt {
   }
 
   fn allocate_color(&self, red: u16, green: u16, blue: u16)
-    -> xcb::Result<u32>
+    -> xcb::Result<Color>
   {
     let connection = self.connection();
 
@@ -41,7 +80,9 @@ pub trait DisplayColorExt: DisplayExt {
       red, green, blue,
     });
 
-    Ok(connection.wait_for_reply(cookie)?.pixel())
+    let pixel = connection.wait_for_reply(cookie)?.pixel();
+
+    Ok(Color::new(pixel, red, green, blue))
   }
 }