Blame mono/Assistance/Drawing.cs

b82ef4
using System;
b82ef4
b82ef4
namespace Assistance {
b82ef4
	namespace Drawing {
b82ef4
		public class Helper {
b82ef4
			public static Rectangle getBounds(Cairo.Context context) {
b82ef4
				double w = 1.0;
b82ef4
				double h = 1.0;
b82ef4
				if (context.GetTarget() is Cairo.ImageSurface) {
b82ef4
					Cairo.ImageSurface surface = (Cairo.ImageSurface)context.GetTarget();
b82ef4
					w = surface.Width;
b82ef4
					h = surface.Height;
b82ef4
				}
b82ef4
				
b82ef4
				Point[] corners = new Point[] {
b82ef4
					new Point(0.0, 0.0),
b82ef4
					new Point(  w, 0.0),
b82ef4
					new Point(  w,   h),
b82ef4
					new Point(0.0,   h) };
b82ef4
b82ef4
				Rectangle bounds = new Rectangle();
b82ef4
				for(int i = 0; i < corners.Length; ++i) {
b82ef4
					double x = corners[i].x;
b82ef4
					double y = corners[i].y;
b82ef4
					context.DeviceToUser(ref x, ref y);
b82ef4
					if (i == 0)
b82ef4
						bounds = new Rectangle(x, y);
b82ef4
					else
b82ef4
						bounds = bounds.expand(new Point(x, y));
b82ef4
				}
b82ef4
				
b82ef4
				return bounds;
b82ef4
			}
b82ef4
		}
b82ef4
		
b82ef4
	
b82ef4
		public struct Color {
b82ef4
			public double r, g, b, a;
b82ef4
			public Color(string name, double alpha = 1.0) {
b82ef4
				Gdk.Color c = new Gdk.Color();
b82ef4
				if (!Gdk.Color.Parse(name, ref c))
b82ef4
					Console.Error.WriteLine("Color [" + name + "] not found");
b82ef4
				this.r = (double)c.Red/65535.0;
b82ef4
				this.g = (double)c.Green/65535.0;
b82ef4
				this.b = (double)c.Blue/65535.0;
b82ef4
				this.a = alpha;
b82ef4
			}
b82ef4
			public Color(double r, double g, double b, double a = 1.0) {
b82ef4
				this.r = r;
b82ef4
				this.g = g;
b82ef4
				this.b = b;
b82ef4
				this.a = a;
b82ef4
			}
b82ef4
			public void apply(Cairo.Context context) {
b82ef4
				context.SetSourceRGBA(r, g, b, a);
b82ef4
			}
b82ef4
		}
b82ef4
	
b82ef4
	
b82ef4
		public struct Pen {
b82ef4
			public Color color;
b82ef4
			public double width;
b82ef4
			public Pen(string color, double width = 1.0, double alpha = 1.0) {
b82ef4
				this.color = new Color(color, alpha);
b82ef4
				this.width = width;
b82ef4
			}
b82ef4
			public void apply(Cairo.Context context) {
b82ef4
				color.apply(context);
b82ef4
				context.LineWidth = width;
b82ef4
			}
b82ef4
		}
b82ef4
b82ef4
b82ef4
		public struct Brush {
b82ef4
			public Color color;
b82ef4
			public Brush(string color, double alpha = 1.0) {
b82ef4
				this.color = new Color(color, alpha);
b82ef4
			}
b82ef4
			public void apply(Cairo.Context context) {
b82ef4
				color.apply(context);
b82ef4
			}
b82ef4
		}
b82ef4
	}
b82ef4
}
b82ef4