// Dennis Hayes (dennish@raytek.com)
// Ben Houston (ben@exocortex.org)
// Gonzalo Paniagua (gonzalo@ximian.com)
+// Juraj Skripsky (juraj@hotfeet.ch)
//
// (C) 2002 Dennis Hayes
// (c) 2002 Ximian, Inc. (http://www.ximiam.com)
+// (C) 2005 HotFeet GmbH (http://www.hotfeet.ch)
//
// TODO: Are the static/non static functions declared correctly
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
using System;
using System.Collections;
using System.Runtime.InteropServices;
[Serializable]
public struct Color
{
+ private const float RGBMax = 255;
+ private const float HLSMax = 240;
private static Hashtable namedColors;
private static Hashtable systemColors;
static Color [] knownColors;
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));
+ && (colorA.g == colorB.g) && (colorA.b == colorB.b) &&
+ (colorA.myname == colorB.myname));
}
/// <summary>
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));
+ || (colorA.g != colorB.g) || (colorA.b != colorB.b) ||
+ (colorA.myname != colorB.myname));
}
-
- public float GetBrightness (){
- // Intensity is the normalized sum of the three RGB values.;
- return ((float)(r + g + b))/(255*3);
+
+ public float GetBrightness ()
+ {
+ byte minval = Math.Min (r, Math.Min (g, b));\r
+ byte maxval = Math.Max (r, Math.Max (g, b));\r
+ \r
+ return (float)(maxval + minval) / 510;\r
}
- public float GetSaturation (){
- // S = 1 - I * Min(r,g,b)
- return (255 -
- (((float)(r + g +b))/3)*Math.Min(r,Math.Min(g,b))
- )/255;
+
+ public float GetSaturation ()
+ {
+ byte minval = Math.Min (r, Math.Min (g, b));\r
+ byte maxval = Math.Max (r, Math.Max (g, b));
+
+ int sum = maxval + minval;
+ if (sum > 255)
+ sum = 510 - sum;\r
+
+ return (float)(maxval - minval) / sum;
}
- public float GetHue (){
- float top = ((float)(2*r-g-b))/(2*255);
- float bottom = (float)Math.Sqrt(((r-g)*(r-g) + (r-b)*(g-b))/255);
- return (float)Math.Acos(top/bottom);
+ public float GetHue ()
+ {
+ byte minval = Math.Min (r, Math.Min (g, b));\r
+ byte maxval = Math.Max (r, Math.Max (g, b));
+
+ if (maxval == minval)\r
+ return 0.0f;\r
+
+ float diff = (float)(maxval - minval);
+ float rnorm = (maxval - r) / diff;\r
+ float gnorm = (maxval - g) / diff;\r
+ float bnorm = (maxval - b) / diff;\r
+
+ float hue = 0.0f;\r
+ if (r == maxval) \r
+ hue = 60.0f * (6.0f + bnorm - gnorm);\r
+ if (g == maxval) \r
+ hue = 60.0f * (2.0f + rnorm - bnorm);\r
+ if (b == maxval) \r
+ hue = 60.0f * (4.0f + gnorm - rnorm);\r
+ if (hue > 360.0f) \r
+ hue = hue - 360.0f;
+
+ return hue;\r
}
// -----------------------
static public Color Green
{
get {
+ // LAMESPEC: MS uses A=255, R=0, G=128, B=0 for Green Color,
+ // which is seems to be wrong. G must be 255.
return Color.FromArgbNamed (255, 0, 128, 0, "Green", KnownColor.Green);
}
}