[Mono-list] new color.cs for inclusion in CVS

Dennis Hayes DENNISH@Raytek.com
Wed, 6 Mar 2002 15:54:25 -0800


Here is a newer version of the color.cs file including changes recommended
by Ben Houston.


//
// System.Drawing.Color.cs
//
// Author:
//   Dennis Hayes (dennish@raytek.com)
//
// (C) 2002 Dennis Hayes
// 
/// Some notes on what the class needs to impliment.
/// 140 predefined and named colors
/// 141 is Transparent
/// Empty is transparent black
/// Color.Empty
/// Color.Equals
/// static
/// Color.FromArgb(int r,int g,int b)
/// Color.FromArgb(int a,int r,int g,int b)
/// Color.FromArgb(int a, Color color)
/// Color.FromArgb(int argpacked)
/// Color.FromKnownColor
/// Color.FromName
/// Color.ReferenceEquals
/* Rough list of named colors, non implmented, possibly shared with
KnownColor structure?
AliceBlue
AntiqueWhite
Aqua
Aquamarine
Azure
Beige
Bisque
Black
BlanchedAlmond
Blue
BlueViolet
Brown
BurlyWood
CadetBlue
Chartreuse
Chocolate
Coral
CornflowerBlue
Cornsilk
Crimson
Cyan
DarkBlue
DarkCyan
DarkGoldenrod
DarkGray
DarkGreen
DarkKhaki
DarkMagenta
DarkOliveGreen
DarkOrange
DarkOrchid
DarkRed
DarkSalmon
DarkSeaGreen
DarkSlateBlue
DarkSlateGray
DarkTurquoise
DarkViolet
DeepPink
DeepSkyBlue
Desktop
DimGray
DodgerBlue
Firebrick
FloralWhite
ForestGreen
Fuchsia
Gainsboro
GhostWhite
Gold
Goldenrod
Gray
GrayText
Green
GreenYellow
Honeydew
HotPink
HotTrack
IndianRed
Indigo
Info
Ivory
Khaki
Lavender
LavenderBlush
LawnGreen
LemonChiffon
LightBlue
LightCoral
LightCyan
LightGoldenrodYellow
LightGray
LightGreen
LightPink
LightSalmon
LightSeaGreen
LightSkyBlue
LightSlateGray
LightSteelBlue
LightYellow
Lime
LimeGreen
Linen
Magenta
Maroon
MediumAquamarine
MediumBlue
MediumOrchid
MediumPurple
MediumSeaGreen
MediumSlateBlue
MediumSpringGreen
MediumTurquoise
MediumVioletRed
MidnightBlue
MintCream
MistyRose
Moccasin
NavajoWhite
Navy
OldLace
Olive
OliveDrab
Orange
OrangeRed
Orchid
PaleGoldenrod
PaleGreen
PaleTurquoise
PaleVioletRed
PapayaWhip
PeachPuff
Peru
Pink
Plum
PowderBlue
Purple
Red
RosyBrown
RoyalBlue
SaddleBrown
Salmon
SandyBrown
ScrollBar
SeaGreen
SeaShell
Sienna
Silver
SkyBlue
SlateBlue
SlateGray
Snow
SpringGreen
SteelBlue
Tan
Teal
Thistle
Tomato
Turquoise
Violet
Wheat
White
WhiteSmoke
Yellow
YellowGreen
*/

using System;
namespace System.Drawing 
{
	public struct Color
	{ 
		// Private transparancy (A) and R,G,B fields.
		int a;
		int r;
		int g;
		int b;
		public static Color FromArgb (int r, int g, int b)
		{
			//TODO: Is 255 the correct value to return for A?
			Color color;
			color.a = 255;
			color.r = r;
			color.g = g;
			color.b = b;
			return color;
		}
		
		public static Color FromArgb (int a,int r, int g, int b)
		{
			Color color;
			color.a = a;
			color.r = r;
			color.g = g;
			color.b = b;
			return color;
		}
		
		public static Color FromArgb (int a,Color incolor)
		{
			Color color;
			color.a = incolor.a;
			color.r = incolor.r;
			color.g = incolor.g;
			color.b = incolor.b;
			return color;
		}

		public static Color FromArgb (int argpacked)
		{
			throw new Exception("Not implemented yet");
			//TODO: Is 255 the correct value to return for A?
			//TODO: do bit manupulations on packed arg
			//return new Color (255, argpacked, argpacked,
argpacked );
		}

		Color FromName(string ColorName)
		{
			throw new Exception("Not implemented yet");
			//return KnownColor(ColorName);
		}

		// -----------------------
		// Public Shared Members
		// -----------------------

		/// <summary>
		///	Empty Shared Field
		/// </summary>
		///
		/// <remarks>
		///	An uninitialized Color Structure
		/// </remarks>
		
		public static readonly Color Empty;
		
		/// <summary>
		///	Equality Operator
		/// </summary>
		///
		/// <remarks>
		///	Compares two Color objects. The return value is
		///	based on the equivalence of the A,R,G,B properties 
		///	of the two Colors.
		/// </remarks>

		public static bool operator == (Color colorA, Color colorB)
		{
			return ((colorA.A == colorB.A) && (colorA.R ==
colorB.R)
			&& (colorA.G == colorB.G) && (colorA.B ==
colorB.B));
		}
		
		/// <summary>
		///	Inequality Operator
		/// </summary>
		///
		/// <remarks>
		///	Compares two Color objects. The return value is
		///	based on the equivalence of the A,R,G,B properties 
		///	of the two colors.
		/// </remarks>

		public static bool operator != (Color colorA, Color colorB)
		{
			return ((colorA.A != colorB.A) || (colorA.R !=
colorB.R)
			|| (colorA.G != colorB.G) || (colorA.B !=
colorB.B));
		}
		
		// -----------------------
		// Public Constructors
		// -----------------------
		public Color(int Transparancy, int Red, int Green, int Blue)
		{
			a = Transparancy;
			r = Red;
			g = Green;
			b = Blue;
		}

		// If I add this as a parameterless constructor, I get this
error:
		// Structs cannot contain explicit parameterless
constructors
		// Also, default parameters are not permitted.
		//		public Color()
		//		{
		//			a = 0;
		//			r = 0;
		//			g = 0;
		//			b = 0;
		//		}

		// -----------------------
		// Public Instance Members
		// -----------------------

		/// <summary>
		///	IsEmpty Property
		/// </summary>
		///
		/// <remarks>
		///	Indicates transparent black. R,G,B = 0; A=0?
		/// </remarks>
		
		public bool IsEmpty 
		{
			get 
			{
				return ((A == 0) && (R == 0) && (G == 0) &&
(B == 0));
			}
		}

		/// <summary>
		///	A Property
		/// </summary>
		///
		/// <remarks>
		///	The transparancy of the Color.
		/// </remarks>
		
		public int A
		{
			get 
			{
				return a;
			}
			set 
			{
				a = value;
			}
		}

		/// <summary>
		///	R Property
		/// </summary>
		///
		/// <remarks>
		///	The red value of the Color.
		/// </remarks>
		
		public int R
		{
			get 
			{
				return r;
			}
			set 
			{
				r = value;
			}
		}

		/// <summary>
		///	G Property
		/// </summary>
		///
		/// <remarks>
		///	The green value of the Color.
		/// </remarks>
		
		public int G
		{
			get 
			{
				return g;
			}
			set 
			{
				g = value;
			}
		}

		/// <summary>
		///	B Property
		/// </summary>
		///
		/// <remarks>
		///	The blue value of the Color.
		/// </remarks>
		
		public int B
		{
			get 
			{
				return b;
			}
			set 
			{
				b = value;
			}
		}

		/// <summary>
		///	Equals Method
		/// </summary>
		///
		/// <remarks>
		///	Checks equivalence of this Color and another object.
		/// </remarks>
		
		public override bool Equals (object o)
		{
			if (!(o is Color))
				return false;

			return (this == (Color) o);
		}

		/// <summary>
		///	GetHashCode Method
		/// </summary>
		///
		/// <remarks>
		///	Calculates a hashing value.
		/// </remarks>
		
		public override int GetHashCode ()
		{
			return (((A^R)^G)^B);
		}

		/// <summary>
		///	ToString Method
		/// </summary>
		///
		/// <remarks>
		///	Formats the Color as a string in ARGB notation.
		/// </remarks>
		
		public override string ToString ()
		{
			return String.Format ("[{0},{1},{2},{3}]", A, R, G,
B);
		}

	}
}
-----Original Message-----
From: Ben Houston [mailto:ben@exocortex.org]
Sent: Wednesday, March 06, 2002 3:31 PM
To: mono-list@ximian.com
Cc: 'Dennis Hayes'
Subject: RE: [Mono-list] new color.cs for inclusion in CVS


Hi Dennis,

Many of your constructors look like the following:
> 		public static Color FromArgb (int a,int r, int g, int b)
> 		{
> 			return new Color (a,r,g,b);
> 		}

The above may be really slow since it could require a box/unbox
operation to the struct when using the new operator (i.e. remember that
we are not using classes).  Although, I find that I am sometimes wrong
about this stuff so maybe someone should check my facts. ;-)
 
I would have written it like this instead:
> 		public static Color FromArgb (int a,int r, int g, int b)
> 		{
>			Color color;
>			color.a = a;
>			color.r = r;
>			color.g = g;
>			color.b = b;
>			return	color;
> 		}

Cheers,
-ben houston
4th Year Cognitive Science/Neuroscience
Carleton University, Ottawa, Canada
( ben@exocortex.org / 613-266-0637 )
  


> -----Original Message-----
> From: mono-list-admin@ximian.com [mailto:mono-list-admin@ximian.com]
On
> Behalf Of Dennis Hayes
> Sent: Wednesday, March 06, 2002 3:50 PM
> To: 'mono-list@ximian.com'
> Subject: [Mono-list] new color.cs for inclusion in CVS
> 
> Here is a decent start on the Color structure for
System.Drawing.Color.
> I plan to finish this and the System.Drawing.KnownColor structure in
the
> next few days.
> Could someone (mkestner?) put this in CVS for me?
> This compiles using Visual Studio, and passes some simple test.
> Will also try to add tests to nunit in next few days as well.
> Thanks.
> Dennis