2005-05-10 Juraj Skripsky <juraj@hotfeet.ch>
[mono.git] / mcs / class / System.Drawing / System.Drawing / Color.cs
index 34851a9df97f063aac390bf900b9a7208e4003ab..ce0d9fe9093d2a1c4d6498ffb0a654d64d775a87 100644 (file)
@@ -5,12 +5,37 @@
 //     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;
@@ -25,6 +50,8 @@ namespace System.Drawing
        [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;
@@ -246,7 +273,8 @@ namespace System.Drawing
                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>
@@ -262,24 +290,54 @@ namespace System.Drawing
                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
                }
                
                // -----------------------
@@ -855,6 +913,8 @@ namespace System.Drawing
                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);
                        }
                }