[Mono-list] Best way to convert Cairo.Color from/to Gdk.Color???

Jonathan Pobst monkey at jpobst.com
Sun Nov 21 11:33:25 EST 2010


Here are the extension methods I use to convert back and forth.  Note 
that the range of a Cairo color value is 0.0-1.0 and the range for Gdk 
is 0-255.

public static Cairo.Color ToCairoColor (this Gdk.Color color)
{
	return new Cairo.Color ((double)color.Red / ushort.MaxValue, 
(double)color.Green / ushort.MaxValue, (double)color.Blue / 
ushort.MaxValue);
}


public static Gdk.Color ToGdkColor (this Cairo.Color color)
{
	Gdk.Color c = new Gdk.Color ();
	c.Blue = (ushort)(color.B * ushort.MaxValue);
	c.Red = (ushort)(color.R * ushort.MaxValue);
	c.Green = (ushort)(color.G * ushort.MaxValue);

	return c;
}


Jonathan

On 11/21/2010 6:40 AM, Francisco M. Marzoa wrote:
> Hello,
>
> I'd like to know what's the best way to converting Cairo.Color into
> Gdk.Color and viceversa, mainly for using Gtk.ColorSelection widget.
> Currently I'm doing something like:
>
> Gdk.Color gcl;
> Cairo.Color ccl;
>
> ...
>
> gcl = Gdk.Color ( (byte)ccl.R, (byte)ccl.G, (byte),cc.B);
>
> Although it seems to work, it does not looks neither clean nor robust,
> as Cairo.Color uses a double for each color component, data may be lost
> on the conversion though. I wonder if there's a better method for doing
> this, and also for converting from Gdk.Color to Cairo.Color.
>
> Thanks a lot in advance.
>
> Best regards,
>
>
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
>



More information about the Mono-list mailing list