[ease] [general] Add alpha support to backgrounds.



commit c69b8dc07d94c95cdf62231c7ccf077b937f92ac
Author: Nate Stedman <natesm gmail com>
Date:   Wed Aug 25 13:14:52 2010 -0400

    [general] Add alpha support to backgrounds.
    
    Backgrounds support now alpha values for colors, including those
    in gradients. This works towards fixing bug 627806.
    
    BackgroundWidget gained a "use_alpha" property which turns alpha
    support on and off. By default, it is enabled, except for Slides,
    which do not support transparent backgrounds (because it would
    make no sense).
    
    - Renamed Color.red255, etc. to Color.red8, etc.
    - Added Color.red16, etc. with type uint16.
    - Fixed bugs related to Color.red8, etc.

 ease-core/ease-background-widget.vala |   62 +++++++++++----
 ease-core/ease-color.vala             |  136 ++++++++++++++++++++++++++-------
 ease-core/ease-text-element.vala      |    2 +-
 3 files changed, 157 insertions(+), 43 deletions(-)
---
diff --git a/ease-core/ease-background-widget.vala b/ease-core/ease-background-widget.vala
index c7ed03f..41e4794 100644
--- a/ease-core/ease-background-widget.vala
+++ b/ease-core/ease-background-widget.vala
@@ -44,12 +44,34 @@ public class Ease.BackgroundWidget : Gtk.Alignment
 	private Slide slide;
 	private Element element;
 	
+	/**
+	 * Whether or not the background widget's color selection dialogs display
+	 * an opacity slider. By default, they will display the slider (except with 
+	 * the { link BackgroundWidget.for_slide} constructor.
+	 *
+	 * This property controls visibility of the opacity control for both solid
+	 * color and gradient backgrounds. There is no way to disable or enable it
+	 * independently. 
+	 */
+	public bool use_alpha
+	{
+		get { return bg_color.use_alpha; }
+		set
+		{
+			bg_color.use_alpha = value;
+			grad_color1.use_alpha = value;
+			grad_color2.use_alpha = value;
+		}
+	}
+	
 	public BackgroundWidget(Background bg, Element e)
 	{
 		background = bg;
 		document = e.parent.parent;
 		element = e;
 		init();
+		
+		use_alpha = true;
 	}
 	
 	public BackgroundWidget.for_slide(Slide s)
@@ -58,6 +80,8 @@ public class Ease.BackgroundWidget : Gtk.Alignment
 		background = s.background;
 		slide = s;
 		init();
+		
+		use_alpha = false;
 	}
 	
 	private void init()
@@ -319,27 +343,15 @@ public class Ease.BackgroundWidget : Gtk.Alignment
 		UndoAction action = null;
 		if (sender == bg_color)
 		{
-			action = background.color.undo_action();
-			background.color.gdk = sender.color;
-			action.applied.connect(() => {
-				sender.set_color(background.gradient.end.gdk);
-			});
+			action = set_color(bg_color, background.color);
 		}
 		else if (sender == grad_color1)
 		{
-			action = background.gradient.start.undo_action();
-			background.gradient.start.gdk = sender.color;
-			action.applied.connect(() => {
-				sender.set_color(background.gradient.start.gdk);
-			});
+			action = set_color(grad_color1, background.gradient.start);
 		}
 		else if (sender == grad_color2)
 		{
-			action = background.gradient.end.undo_action();
-			background.gradient.end.gdk = sender.color;
-			action.applied.connect(() => {
-				sender.set_color(background.gradient.end.gdk);
-			});
+			action = set_color(grad_color2, background.gradient.end);
 		}
 		if (action != null) emit_undo(action);
 	}
@@ -479,5 +491,25 @@ public class Ease.BackgroundWidget : Gtk.Alignment
 				break;
 		}
 	}
+	
+	private UndoAction set_color(Gtk.ColorButton? sender, Color color)
+	{
+		// get an undo action
+		var action = color.undo_action();
+		
+		// set the color
+		color.gdk = sender.color;
+		
+		// set the alpha
+		if (use_alpha) color.alpha16 = (uint16)sender.alpha;
+		
+		// reset UI elements when the action is applied
+		action.applied.connect(() => {
+			sender.set_color(color.gdk);
+			sender.alpha = color.alpha16;
+		});
+		
+		return action;
+	}
 }
 
diff --git a/ease-core/ease-color.vala b/ease-core/ease-color.vala
index 347f7b2..2d21cf7 100644
--- a/ease-core/ease-color.vala
+++ b/ease-core/ease-color.vala
@@ -47,7 +47,7 @@ public class Ease.Color : GLib.Object
 	}
 
 	/**
-	 * The red value of this color.
+	 * The red value of this color, as a double from 0 to 1.
 	 */
 	public double red
 	{
@@ -70,7 +70,7 @@ public class Ease.Color : GLib.Object
 	private double red_priv;
 	
 	/**
-	 * The green value of this color.
+	 * The green value of this color, as a double from 0 to 1..
 	 */
 	public double green
 	{
@@ -93,7 +93,7 @@ public class Ease.Color : GLib.Object
 	private double green_priv;
 	
 	/**
-	 * The blue value of this color.
+	 * The blue value of this color, as a double from 0 to 1..
 	 */
 	public double blue
 	{
@@ -116,7 +116,7 @@ public class Ease.Color : GLib.Object
 	private double blue_priv;
 	
 	/**
-	 * The alpha (transparency) of this color.
+	 * The alpha (transparency) of this color, as a double from 0 to 1..
 	 */
 	public double alpha
 	{
@@ -142,7 +142,7 @@ public class Ease.Color : GLib.Object
 	/**
 	 * The red value of this color, as an 8-bit unsigned integer.
 	 */
-	public uint8 red255
+	public uint8 red8
 	{
 		get { return (uint8)(255 * red_priv); }
 		set
@@ -152,7 +152,7 @@ public class Ease.Color : GLib.Object
 				warning("red value must be >= 0, %f is not", value);
 				red_priv = 0;
 			}
-			else if (value > 1)
+			else if (value > 255)
 			{
 				warning("red value must be <= 255, %f is not", value);
 				red_priv = 1;
@@ -164,7 +164,7 @@ public class Ease.Color : GLib.Object
 	/**
 	 * The green value of this color, as an 8-bit unsigned integer.
 	 */
-	public uint8 green255
+	public uint8 green8
 	{
 		get { return (uint8)(255 * green_priv); }
 		set
@@ -174,7 +174,7 @@ public class Ease.Color : GLib.Object
 				warning("green value must be >= 0, %f is not", value);
 				green_priv = 0;
 			}
-			else if (value > 1)
+			else if (value > 255)
 			{
 				warning("green value must be <= 255, %f is not", value);
 				green_priv = 1;
@@ -186,7 +186,7 @@ public class Ease.Color : GLib.Object
 	/**
 	 * The blue value of this color, as an 8-bit unsigned integer.
 	 */
-	public uint8 blue255
+	public uint8 blue8 
 	{
 		get { return (uint8)(255 * blue_priv); }
 		set
@@ -196,7 +196,7 @@ public class Ease.Color : GLib.Object
 				warning("blue value must be >= 0, %f is not", value);
 				blue_priv = 0;
 			}
-			else if (value > 1)
+			else if (value > 255)
 			{
 				warning("blue value must be <= 255, %f is not", value);
 				blue_priv = 1;
@@ -208,7 +208,7 @@ public class Ease.Color : GLib.Object
 	/**
 	 * The alpha (transparency) of this color, as an 8-bit unsigned integer.
 	 */
-	public uint8 alpha255
+	public uint8 alpha8
 	{
 		get { return (uint8)(255 * alpha_priv); }
 		set
@@ -218,7 +218,7 @@ public class Ease.Color : GLib.Object
 				warning("alpha value must be >= 0, %f is not", value);
 				alpha_priv = 0;
 			}
-			else if (value > 1)
+			else if (value > 255)
 			{
 				warning("alpha value must be <= 255, %f is not", value);
 				alpha_priv = 1;
@@ -228,6 +228,94 @@ public class Ease.Color : GLib.Object
 	}
 	
 	/**
+	 * The red value of this color, as an 16-bit unsigned integer.
+	 */
+	public uint16 red16
+	{
+		get { return (uint16)(65535 * red_priv); }
+		set
+		{
+			if (value < 0)
+			{
+				warning("red value must be >= 0, %f is not", value);
+				red_priv = 0;
+			}
+			else if (value > 65535)
+			{
+				warning("red value must be <= 65535, %f is not", value);
+				red_priv = 1;
+			}
+			else red_priv = value / 65535.0;
+		}
+	}
+	
+	/**
+	 * The green value of this color, as an 16-bit unsigned integer.
+	 */
+	public uint16 green16
+	{
+		get { return (uint16)(65535 * green_priv); }
+		set
+		{
+			if (value < 0)
+			{
+				warning("green value must be >= 0, %f is not", value);
+				green_priv = 0;
+			}
+			else if (value > 65535)
+			{
+				warning("green value must be <= 65535, %f is not", value);
+				green_priv = 1;
+			}
+			else green_priv = value / 65535.0;
+		}
+	}
+	
+	/**
+	 * The blue value of this color, as an 16-bit unsigned integer.
+	 */
+	public uint16 blue16 
+	{
+		get { return (uint16)(65535 * blue_priv); }
+		set
+		{
+			if (value < 0)
+			{
+				warning("blue value must be >= 0, %f is not", value);
+				blue_priv = 0;
+			}
+			else if (value > 65535)
+			{
+				warning("blue value must be <= 65535, %f is not", value);
+				blue_priv = 1;
+			}
+			else blue_priv = value / 65535.0;
+		}
+	}
+	
+	/**
+	 * The alpha (transparency) of this color, as an 16-bit unsigned integer.
+	 */
+	public uint16 alpha16
+	{
+		get { return (uint16)(65535 * alpha_priv); }
+		set
+		{
+			if (value < 0)
+			{
+				warning("alpha value must be >= 0, %f is not", value);
+				alpha_priv = 0;
+			}
+			else if (value > 65535)
+			{
+				warning("alpha value must be <= 65535, %f is not", value);
+				alpha_priv = 1;
+			}
+			else alpha_priv = value / 65535.0;
+		}
+	}
+	
+	/**
 	 * A Clutter.Color representation of this color. Changes made to the
 	 * the returned color are not reflected in this color.
 	 */
@@ -235,17 +323,14 @@ public class Ease.Color : GLib.Object
 	{
 		get
 		{
-			return { (uint8)(255 * red),
-			         (uint8)(255 * green),
-			         (uint8)(255 * blue),
-			         (uint8)(255 * alpha) };
+			return { red8, green8, blue8, alpha8 };
 		}
 		set
 		{
-			red = value.red / 255f;
-			green = value.green / 255f;
-			blue = value.blue / 255f;
-			alpha = value.alpha / 255f;
+			red8 = value.red;
+			green8 = value.green;
+			blue8 = value.blue;
+			alpha8 = value.alpha;
 		}
 	}
 	
@@ -259,16 +344,13 @@ public class Ease.Color : GLib.Object
 	{
 		get
 		{
-			return { 0,
-			         (uint16)(65535 * red),
-			         (uint16)(65535 * green),
-			         (uint16)(65535 * blue) };
+			return { 0, red16, green16, blue16 };
 		}
 		set
 		{
-			red = value.red / 65535f;
-			green = value.green / 65535f;
-			blue = value.blue / 65535f;
+			red16 = value.red;
+			green16 = value.green;
+			blue16 = value.blue;
 			alpha = 1;
 		}
 	}
diff --git a/ease-core/ease-text-element.vala b/ease-core/ease-text-element.vala
index 0f01663..57f9492 100644
--- a/ease-core/ease-text-element.vala
+++ b/ease-core/ease-text-element.vala
@@ -212,7 +212,7 @@ public class Ease.TextElement : Element
 		
 		// set the text-specific properties of the element
 		html += " color:" + 
-		        @"rgb($(color.red255),$(color.green255),$(color.blue255));";
+		        @"rgb($(color.red8),$(color.green8),$(color.blue8));";
 		        
 		html += " font-family:'" + text_font + "', sans-serif;";
 		        



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