[ease] [general] Add generic Color and Gradient classes



commit ad3d3fc6a98a397f4521cdfd9c468d81484c8dcd
Author: Nate Stedman <natesm gmail com>
Date:   Sat Jul 24 00:43:22 2010 -0400

    [general] Add generic Color and Gradient classes
    
    Ease.Color
    
    Color is a generic abstraction that supports Cairo, GDK, and
    Clutter. all "color" usage in Ease should switch to this class
    unless it is a "simple" usage - setting the background of a
    Clutter stage, for example.
    
    Ease.Gradient & Ease.GradientMode
    
    Currently useless, will see support later in the form of slide
    backgrounds and shapes.

 Makefile.am            |    2 +
 src/ease-color.vala    |  246 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/ease-gradient.vala |  166 ++++++++++++++++++++++++++++++++
 src/ease-theme.vala    |    5 +
 4 files changed, 419 insertions(+), 0 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 639a7f5..52ab9ee 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,6 +17,7 @@ ease_SOURCES = \
 	src/ease-actor.vala \
 	src/ease-animated-zoom-slider.vala \
 	src/ease-close-confirm-dialog.vala \
+	src/ease-color.vala \
 	src/ease-document.vala \
 	src/ease-editor-embed.vala \
 	src/ease-editor-window.vala \
@@ -24,6 +25,7 @@ ease_SOURCES = \
 	src/ease-element-map-value.vala \
 	src/ease-element.vala \
 	src/ease-enums.vala \
+	src/ease-gradient.vala \
 	src/ease-handle.vala \
 	src/ease-html-exporter.vala \
 	src/ease-image-actor.vala \
diff --git a/src/ease-color.vala b/src/ease-color.vala
new file mode 100644
index 0000000..ddf5303
--- /dev/null
+++ b/src/ease-color.vala
@@ -0,0 +1,246 @@
+/*  Ease, a GTK presentation application
+    Copyright (C) 2010 Nate Stedman
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * Color abstraction, supporting Clutter, GDK, and Cairo colors.
+ */
+public class Ease.Color : GLib.Object
+{
+	/**
+	 * The format string for converting Colors to strings.
+	 */
+	private const string STR = "%f%s%f%s%f%s%f";
+	
+	/**
+	 * The string placed between each channel in a string representation.
+	 */
+	private const string SPLIT = ", ";
+
+	/**
+	 * The red value of this color.
+	 */
+	public double red
+	{
+		get { return red_priv; }
+		set
+		{
+			if (value < 0)
+			{
+				warning("red value must be >= 0, %f is not", value);
+				red_priv = 0;
+			}
+			else if (value > 1)
+			{
+				warning("red value must be <= 0, %f is not", value);
+				red_priv = 1;
+			}
+			else red_priv = value;
+		}
+	}
+	private double red_priv;
+	
+	/**
+	 * The green value of this color.
+	 */
+	public double green
+	{
+		get { return green_priv; }
+		set
+		{
+			if (value < 0)
+			{
+				warning("green value must be >= 0, %f is not", value);
+				green_priv = 0;
+			}
+			else if (value > 1)
+			{
+				warning("green value must be <= 0, %f is not", value);
+				green_priv = 1;
+			}
+			else green_priv = value;
+		}
+	}
+	private double green_priv;
+	
+	/**
+	 * The blue value of this color.
+	 */
+	public double blue
+	{
+		get { return blue_priv; }
+		set
+		{
+			if (value < 0)
+			{
+				warning("blue value must be >= 0, %f is not", value);
+				blue_priv = 0;
+			}
+			else if (value > 1)
+			{
+				warning("blue value must be <= 0, %f is not", value);
+				blue_priv = 1;
+			}
+			else blue_priv = value;
+		}
+	}
+	private double blue_priv;
+	
+	/**
+	 * The alpha (transparency) of this color.
+	 */
+	public double alpha
+	{
+		get { return alpha_priv; }
+		set
+		{
+			if (value < 0)
+			{
+				warning("alpha value must be >= 0, %f is not", value);
+				alpha_priv = 0;
+			}
+			else if (value > 1)
+			{
+				warning("alpha value must be <= 0, %f is not", value);
+				alpha_priv = 1;
+			}
+			else alpha_priv = value;
+		}
+	}
+	private double alpha_priv;
+	
+	/**
+	 * A Clutter.Color representation of this color. Changes made to the
+	 * the returned color are not reflected in this color.
+	 */
+	public Clutter.Color clutter
+	{
+		get
+		{
+			return { (uchar)(255 * red),
+			         (uchar)(255 * green),
+			         (uchar)(255 * blue),
+			         (uchar)(255 * alpha) };
+		}
+		set
+		{
+			red = value.red / 255f;
+			green = value.green / 255f;
+			blue = value.blue / 255f;
+			alpha = value.alpha / 255f;
+		}
+	}
+	
+	/**
+	 * A Gdk.Color representation of this color. Changes made to the returned
+	 * color are not reflected in this color. Note that GDK colors do not
+	 * support an alpha value. When being set, the alpha will be set to full,
+	 * when retrieved, the alpha value will be ignored.
+	 */
+	public Gdk.Color gdk
+	{
+		get
+		{
+			return { 0,
+			         (uint16)(255 * red),
+			         (uint16)(65535 * green),
+			         (uint16)(65535 * blue) };
+		}
+		set
+		{
+			red = value.red / 65535f;
+			green = value.green / 65535f;
+			blue = value.blue / 65535f;
+			alpha = 1;
+		}
+	}
+	
+	/**
+	 * Creates an opaque color.
+	 */
+	public Color.rgb(double r, double g, double b)
+	{
+		rgba(r, g, b, 1);
+	}
+	
+	/**
+	 * Creates a color.
+	 *
+	 * @param r The red value.
+	 * @param g The green value.
+	 * @param b The blue value.
+	 * @param a The alpha value.
+	 */
+	public Color.rgba(double r, double g, double b, double a)
+	{
+		red = r;
+		green = g;
+		blue = b;
+		alpha = a;
+	}
+	
+	/**
+	 * Creates a Color from a Clutter.Color. See also: { link clutter}.
+	 *
+	 * @param color The Clutter color to use.
+	 */
+	public Color.from_clutter(Clutter.Color color)
+	{
+		clutter = color;
+	}
+	
+	/**
+	 * Creates a Color from a Gdk.Color. See also: { link gdk}.
+	 *
+	 * @param color The Clutter color to use.
+	 */
+	public Color.from_gdk(Gdk.Color color)
+	{
+		gdk = color;
+	}
+	
+	/**
+	 * Creates a Color from a string representation created by to_string().
+	 *
+	 * @param str The string to create a color from.
+	 */
+	public Color.from_string(string str)
+	{
+		var split = str.split(SPLIT);
+		red = split[0].to_double();
+		green = split[1].to_double();
+		blue = split[2].to_double();
+		alpha = split[3].to_double();
+	}
+	
+	/**
+	 * Creates a string representation of a color.
+	 */
+	public string to_string()
+	{
+		return STR.printf(red, SPLIT, blue, SPLIT, green, SPLIT, alpha, SPLIT);
+	}
+	
+	/**
+	 * Sets the given Cairo context's color to this color.
+	 *
+	 * @param cr The Cairo Context to set the color for.
+	 */
+	public void cairo_set(Cairo.Context cr)
+	{
+		cr.set_source_rgba(red, green, blue, alpha);
+	}
+}
diff --git a/src/ease-gradient.vala b/src/ease-gradient.vala
new file mode 100644
index 0000000..0f3f960
--- /dev/null
+++ b/src/ease-gradient.vala
@@ -0,0 +1,166 @@
+/*  Ease, a GTK presentation application
+    Copyright (C) 2010 Nate Stedman
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * Gradient representation, using { link Color}.
+ */
+public class Ease.Gradient : GLib.Object
+{
+	/**
+	 * The format string for converting gradients to strings.
+	 */
+	private const string STR = "%s%s%s%s%s%s";
+	
+	/**
+	 * The string placed between each color in a string representation.
+	 */
+	private const string SPLIT = "|";	
+	
+	/**
+	 * The starting { link Color} of the gradient.
+	 */
+	public Color start { get; set; }
+	
+	/**
+	 * The ending { link Color} of the gradient.
+	 */
+	public Color end { get; set; }
+	
+	/**
+	 * The { link GradientMode} of the gradient.
+	 */
+	public GradientMode mode { get; set; }
+	
+	/**
+	 * The angle, in radians, of the gradient, if it is linear.
+	 */
+	public double angle { get; set; }
+	
+	/**
+	 * Creates a new linear gradient, with the specified colors.
+	 */
+	public Gradient(Color start_color, Color end_color)
+	{
+		start = start_color;
+		end = end_color;
+		mode = GradientMode.LINEAR;
+	}
+	
+	/**
+	 * Creates a new mirrored linear gradient, with the specified colors.
+	 */
+	public Gradient.mirrored(Color start_color, Color end_color)
+	{
+		this(start_color, end_color);
+		mode = GradientMode.LINEAR_MIRRORED;
+	}
+	
+	/**
+	 * Creates a new linear gradient, with the specified colors.
+	 */
+	public Gradient.radial(Color start_color, Color end_color)
+	{
+		this(start_color, end_color);
+		mode = GradientMode.RADIAL;
+	}
+	
+	/**
+	 * Creates a Gradient from a string representation.
+	 */
+	public Gradient.from_string(string str)
+	{
+		var split = str.split(SPLIT);
+		start = new Color.from_string(split[0]);
+		end = new Color.from_string(split[1]);
+		mode = GradientMode.from_string(split[2]);
+		angle = split[3].to_double();
+	}
+	
+	/**
+	 * Returns a string representation of this Gradient.
+	 */
+	public string to_string()
+	{
+		return STR.printf(start.to_string(), SPLIT,
+		                  end.to_string(), SPLIT,
+		                  mode.to_string(), SPLIT,
+		                  angle.to_string());
+	}
+	
+	/**
+	 * Reverses the Gradient.
+	 */
+	public void flip()
+	{
+		var temp = end;
+		end = start;
+		start = temp;
+	}
+}
+
+/**
+ * The { link Gradient} types provided by Ease.
+ */
+public enum Ease.GradientMode
+{
+	/**
+	 * A linear gradient, from top to bottom.
+	 */
+	LINEAR,
+	
+	/**
+	 * A mirrored linear gradient, with the "end" color in the middle and the
+	 * "start" color on both ends.
+	 */
+	LINEAR_MIRRORED,
+	
+	/**
+	 * A radial gradient, with the "start" color in the center and the "end"
+	 * color on the outsides.
+	 */
+	RADIAL;
+	
+	/**
+	 * Returns a string representation of this GradientMode.
+	 */
+	public string to_string()
+	{
+		switch (this)
+		{
+			case LINEAR: return Theme.GRAD_LINEAR;
+			case LINEAR_MIRRORED: return Theme.GRAD_LINEAR_MIRRORED;
+			case RADIAL: return Theme.GRAD_RADIAL;
+		}
+		return "undefined";
+	}
+	
+	/**
+	 * Creates a GradientMode from a string representation.
+	 */
+	public static GradientMode from_string(string str)
+	{
+		switch (str)
+		{
+			case Theme.GRAD_LINEAR: return LINEAR;
+			case Theme.GRAD_LINEAR_MIRRORED: return LINEAR_MIRRORED;
+			case Theme.GRAD_RADIAL: return RADIAL;
+		}
+		
+		warning("%s is not a gradient type", str);
+		return LINEAR;
+	}
+}
diff --git a/src/ease-theme.vala b/src/ease-theme.vala
index 61b10b5..aea1ea3 100644
--- a/src/ease-theme.vala
+++ b/src/ease-theme.vala
@@ -74,6 +74,11 @@ public class Ease.Theme : GLib.Object
 	public const string TEXT_ALIGN = "text-align";
 	public const string TEXT_COLOR = "text-color";
 	
+	// gradient types
+	public const string GRAD_LINEAR = "linear";
+	public const string GRAD_LINEAR_MIRRORED = "linear-mirrored";
+	public const string GRAD_RADIAL = "radial";
+	
 	// generic element properties
 	public const string E_IDENTIFIER = "element-identifier";
 	



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