2005-05-10 Juraj Skripsky <juraj@hotfeet.ch>
authorJuraj Skripsky <js@hotfeet.ch>
Tue, 10 May 2005 17:32:14 +0000 (17:32 -0000)
committerJuraj Skripsky <js@hotfeet.ch>
Tue, 10 May 2005 17:32:14 +0000 (17:32 -0000)
        * Color.cs: New, correct implementations for GetHue(),
        GetBrightness() and GetSaturation().

        * TestColor.cs: Added tests for GetHue, GetBrightness
        and GetSaturation.

svn path=/trunk/mcs/; revision=44338

mcs/class/System.Drawing/System.Drawing/ChangeLog
mcs/class/System.Drawing/System.Drawing/Color.cs
mcs/class/System.Drawing/Test/System.Drawing/ChangeLog
mcs/class/System.Drawing/Test/System.Drawing/TestColor.cs

index d158eed78712b38ec788d335a2cb61449cdfe6b1..82980836812f12567bfffa788041801f4f7427f6 100644 (file)
@@ -1,3 +1,8 @@
+2005-05-10 Juraj Skripsky <juraj@hotfeet.ch>
+
+       * TestColor.cs: Added tests for GetHue, GetBrightness
+       and GetSaturation.
+
 2005-05-09  Sebastien Pouliot  <sebastien@ximian.com>
 
        * gdipFunctions.cs: Use PlatformID.Unix under NET_2_0. 
index 918b5f11c1e0a62536c7f6beb19464af5e119357..ce0d9fe9093d2a1c4d6498ffb0a654d64d775a87 100644 (file)
@@ -5,9 +5,11 @@
 //     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
 
@@ -292,75 +294,50 @@ namespace System.Drawing
                        (colorA.myname != colorB.myname));
                }
 
-               // This gives the right results, but the floats don't exactly match MS
-               // Should we cache those? Getting all three numbers will have us do a few calcs 3 times
-               public float GetBrightness (){
-                       float   cMax;
-                       float   cMin;
-
-                       cMax = Math.Max(Math.Max(r, g), b);
-                       cMin = Math.Min(Math.Min(r, g), b);
-
-                       return (((cMax+cMin)*HLSMax)+RGBMax)/(2*RGBMax) / HLSMax;
+               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 (){
-                       float   cMax;
-                       float   cMin;
-                       float   l;
-
-                       cMax = Math.Max(Math.Max(r, g), b);
-                       cMin = Math.Min(Math.Min(r, g), b);
-
-                       if (cMax==cMin) {               // Achromatic
-                               return 0;
-                       }
-
-                       l = (((cMax+cMin)*HLSMax)+RGBMax)/(2*RGBMax);
+               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
 
-                       if (l<=(HLSMax/2)) {
-                               return (((cMax-cMin)*HLSMax)+((cMax+cMin)/2))/(cMax+cMin) / HLSMax;
-                       } else {
-                               return (((cMax-cMin)*HLSMax)+((2*RGBMax-cMax-cMin)/2))/(2*RGBMax-cMax-cMin) / HLSMax;
-                       }
+                       return (float)(maxval - minval) / sum;
                }
 
-               public float GetHue (){
-                       float   cMax;
-                       float   cMin;
-                       float   rDelta;
-                       float   gDelta;
-                       float   bDelta;
-                       float   h;
-
-                       cMax = Math.Max(Math.Max(r, g), b);
-                       cMin = Math.Min(Math.Min(r, g), b);
-
-                       if (cMax==cMin) {               // Achromatic
-                               return 0;
-                       }
-
-                       rDelta=(((cMax-r)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
-                       gDelta=(((cMax-g)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
-                       bDelta=(((cMax-b)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
-
-                       if (r == cMax) {
-                               h=bDelta - gDelta;
-                       } else if (g == cMax) {
-                               h=(HLSMax/3) + rDelta - bDelta;
-                       } else { /* B == cMax */
-                               h=((2*HLSMax)/3) + gDelta - rDelta;
-                       }
-
-                       if (h<0) {
-                               h+=HLSMax;
-                       }
-
-                       if (h>HLSMax) {
-                               h-=HLSMax;
-                       }
-
-                       return h * 360 / HLSMax;
+               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
                }
                
                // -----------------------
index 59e63fceca2b5369b07e93e47a6c2afe75d3bfaf..85d19efce191b21ff792efd4cb19650fe9386216 100644 (file)
@@ -1,3 +1,8 @@
+2005-05-10 Juraj Skripsky <juraj@hotfeet.ch>
+
+       * TestColor.cs: Added tests for GetHue, GetBrightness
+       and GetSaturation.
+
 2005-02-24 Jordi Mas i Hernandez <jordi@ximan.com>
        * TestRegion.cs: added new IsVisible cases
 
index c71fa40962ce7a2c771bfb712be9a90d161b36ec..d6d9f7eb930df14a9c51d820585646f1f8785bac 100644 (file)
@@ -897,6 +897,23 @@ namespace MonoTests.System.Drawing
                        AssertEquals ("#YellowGreen.G", 205, color.G);
                        AssertEquals ("#YellowGreen.B", 50, color.B);
                }
+               
+               [Test]
+               public void TestHBSValues ()
+               {
+                       AssertEquals ("BrightnessBlack", 0.0f, Color.Black.GetBrightness ());
+                       AssertEquals ("BrightnessWhite", 1.0f, Color.White.GetBrightness ());
+               
+                       Color c1 = Color.FromArgb (0, 13, 45, 7); //just some random color
+                       AssertEquals ("Hue1",        110.5263f, c1.GetHue ());
+                       AssertEquals ("Brightness1", 0.1019608f, c1.GetBrightness ());
+                       AssertEquals ("Saturation1", 0.7307692f, c1.GetSaturation ());
+       
+                       Color c2 = Color.FromArgb (0, 112, 75, 29); //another random color
+                       AssertEquals ("Hue2",        33.25302f, c2.GetHue ());
+                       AssertEquals ("Brightness2", 0.2764706f, c2.GetBrightness ());
+                       AssertEquals ("Saturation2", 0.5886525f, c2.GetSaturation ());
+               }
        }
 }