[california/wip/725768-colors] Added color chooser



commit 1ddb49d3351028b55eab78e666d085d6904ea6ae
Author: Jim Nelson <jim yorba org>
Date:   Thu Mar 20 12:49:05 2014 -0700

    Added color chooser

 src/Makefile.am                             |    5 +++
 src/backing/backing-source.vala             |    7 ++++
 src/host/host-color-chooser-popup.vala      |   36 ++++++++++++++++++++
 src/manager/manager-calendar-list-item.vala |   11 ++++++
 src/util/util-gfx.vala                      |   48 ++++++++++++++++++++++----
 5 files changed, 99 insertions(+), 8 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 8f76128..f523b5a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -62,6 +62,7 @@ california_VALASOURCES = \
        \
        host/host.vala \
        host/host-calendar-popup.vala \
+       host/host-color-chooser-popup.vala \
        host/host-create-update-event.vala \
        host/host-interaction.vala \
        host/host-main-window.vala \
@@ -124,6 +125,10 @@ california_CFLAGS = \
        -DPREFIX=\"$(prefix)\" \
        $(NULL)
 
+LIBS = \
+       -lm \
+       $(NULL)
+
 california-resources.c: $(california_RC) california-resources.xml
        $(GLIB_COMPILE_RESOURCES) --target="$@" --generate-source california-resources.xml
 
diff --git a/src/backing/backing-source.vala b/src/backing/backing-source.vala
index 55ebadc..3a1d411 100644
--- a/src/backing/backing-source.vala
+++ b/src/backing/backing-source.vala
@@ -91,6 +91,13 @@ public abstract class Source : BaseObject {
             Gdk.RGBA() { red = 0.0, green = 0.0, blue = 0.0, alpha = 1.0 },  null);
     }
     
+    public void set_color_to_rgba(Gdk.RGBA rgba) {
+        Gdk.Color rgb = Gfx.rgba_to_rgb(rgba);
+        
+        color = rgb.to_string();
+        debug("set-color: %s", color);
+    }
+    
     public override string to_string() {
         return title;
     }
diff --git a/src/host/host-color-chooser-popup.vala b/src/host/host-color-chooser-popup.vala
new file mode 100644
index 0000000..dc76f85
--- /dev/null
+++ b/src/host/host-color-chooser-popup.vala
@@ -0,0 +1,36 @@
+/* Copyright 2014 Yorba Foundation
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later).  See the COPYING file in this distribution.
+ */
+
+namespace California.Host {
+
+/**
+ * A simple { link Popup} window featuring only a GtkColorChooser.
+ */
+
+public class ColorChooserPopup : Popup {
+    private Gtk.ColorChooserWidget color_chooser = new Gtk.ColorChooserWidget();
+    
+    public signal void selected(Gdk.RGBA rgba);
+    
+    public ColorChooserPopup(Gtk.Widget relative_to, Gdk.RGBA initial_rgba) {
+        base (relative_to);
+        
+        color_chooser.rgba = initial_rgba;
+        color_chooser.use_alpha = false;
+        color_chooser.show_editor = false;
+        color_chooser.margin = 8;
+        
+        color_chooser.color_activated.connect((rgba) => {
+            selected(rgba);
+            dismissed();
+        });
+        
+        add(color_chooser);
+    }
+}
+
+}
+
diff --git a/src/manager/manager-calendar-list-item.vala b/src/manager/manager-calendar-list-item.vala
index cae9e95..11a3bb2 100644
--- a/src/manager/manager-calendar-list-item.vala
+++ b/src/manager/manager-calendar-list-item.vala
@@ -46,6 +46,17 @@ public class CalendarListItem : Gtk.Grid {
     
     [GtkCallback]
     private void on_color_button_clicked() {
+        Host.ColorChooserPopup popup = new Host.ColorChooserPopup(color_button, source.color_as_rgba());
+        
+        popup.selected.connect((rgba) => {
+            source.set_color_to_rgba(rgba);
+        });
+        
+        popup.dismissed.connect(() => {
+            popup.destroy();
+        });
+        
+        popup.show_all();
     }
 }
 
diff --git a/src/util/util-gfx.vala b/src/util/util-gfx.vala
index 07b8ec4..1246529 100644
--- a/src/util/util-gfx.vala
+++ b/src/util/util-gfx.vala
@@ -6,6 +6,12 @@
 
 namespace California.Gfx {
 
+/**
+ * Convert an RGB string into an RGB structure.
+ *
+ * The string can be in any of the forms that Gdk.Color.parse accepts.  If unable to parse the
+ * string, the { link default_rgb} is returned and { link used_default} is set to true.
+ */
 public Gdk.Color rgb_string_to_rgb(string rgb_string, Gdk.Color default_rgb, out bool used_default) {
     Gdk.Color rgb;
     if (!Gdk.Color.parse(rgb_string, out rgb)) {
@@ -21,6 +27,12 @@ public Gdk.Color rgb_string_to_rgb(string rgb_string, Gdk.Color default_rgb, out
     return rgb;
 }
 
+/**
+ * Convert an RGB string into an RGBA structure.
+ *
+ * The string can be in any of the forms that Gdk.Color.parse accepts.  If unable to parse the
+ * string, the { link default_rgba} is returned and { link used_default} is set to true.
+ */
 public Gdk.RGBA rgb_string_to_rgba(string rgb_string, Gdk.RGBA default_rgba, out bool used_default) {
     Gdk.Color rgb;
     if (!Gdk.Color.parse(rgb_string, out rgb)) {
@@ -31,12 +43,11 @@ public Gdk.RGBA rgb_string_to_rgba(string rgb_string, Gdk.RGBA default_rgba, out
         return default_rgba;
     }
     
-    Gdk.RGBA rgba = {
-        red: uint16_to_fp(rgb.red),
-        green: uint16_to_fp(rgb.green),
-        blue: uint16_to_fp(rgb.blue),
-        alpha: 1.0
-    };
+    Gdk.RGBA rgba = Gdk.RGBA();
+    rgba.red = uint16_to_fp(rgb.red);
+    rgba.green = uint16_to_fp(rgb.green);
+    rgba.blue = uint16_to_fp(rgb.blue);
+    rgba.alpha = 1.0;
     
     used_default = false;
     
@@ -45,9 +56,12 @@ public Gdk.RGBA rgb_string_to_rgba(string rgb_string, Gdk.RGBA default_rgba, out
 
 // compiler error if this calculation is done inline when initializing struct
 private inline double uint16_to_fp(uint16 value) {
-    return (double) value / (double) uint16.MAX;
+    return (double) value / (double) uint8.MAX;
 }
 
+/**
+ * Converts the Gdk.RGBA into a 32-bit pixel representation.
+ */
 public uint32 rgba_to_pixel(Gdk.RGBA rgba) {
     return (uint32) fp_to_uint8(rgba.red) << 24
         | (uint32) fp_to_uint8(rgba.green) << 16
@@ -56,7 +70,25 @@ public uint32 rgba_to_pixel(Gdk.RGBA rgba) {
 }
 
 private inline uint8 fp_to_uint8(double value) {
-    return (uint8) (value * 255.0);
+    return (uint8) Math.round(value * (double) uint8.MAX);
+}
+
+/**
+ * Converts a Gdk.RGBA structure into an RGB (Gdk.Color) structure.
+ *
+ * The alpha channel is necessarily stripped in this conversion.
+ */
+public Gdk.Color rgba_to_rgb(Gdk.RGBA rgba) {
+    Gdk.Color rgb = Gdk.Color();
+    rgb.red = fp_to_uint16(rgba.red);
+    rgb.green = fp_to_uint16(rgba.green);
+    rgb.blue = fp_to_uint16(rgba.blue);
+    
+    return rgb;
+}
+
+private inline uint16 fp_to_uint16(double value) {
+    return (uint16) Math.round(value * (double) uint8.MAX);
 }
 
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]