Author: juraj
Date: 2005-05-10 13:32:14 -0400 (Tue, 10 May 2005)
New Revision: 44338

Modified:
   trunk/mcs/class/System.Drawing/System.Drawing/ChangeLog
   trunk/mcs/class/System.Drawing/System.Drawing/Color.cs
   trunk/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog
   trunk/mcs/class/System.Drawing/Test/System.Drawing/TestColor.cs
Log:
2005-05-10 Juraj Skripsky <[EMAIL PROTECTED]>

        * Color.cs: New, correct implementations for GetHue(),
        GetBrightness() and GetSaturation().

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



Modified: trunk/mcs/class/System.Drawing/System.Drawing/ChangeLog
===================================================================
--- trunk/mcs/class/System.Drawing/System.Drawing/ChangeLog     2005-05-10 
16:59:04 UTC (rev 44337)
+++ trunk/mcs/class/System.Drawing/System.Drawing/ChangeLog     2005-05-10 
17:32:14 UTC (rev 44338)
@@ -1,3 +1,8 @@
+2005-05-10 Juraj Skripsky <[EMAIL PROTECTED]>
+
+       * TestColor.cs: Added tests for GetHue, GetBrightness
+       and GetSaturation.
+
 2005-05-09  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * gdipFunctions.cs: Use PlatformID.Unix under NET_2_0. 

Modified: trunk/mcs/class/System.Drawing/System.Drawing/Color.cs
===================================================================
--- trunk/mcs/class/System.Drawing/System.Drawing/Color.cs      2005-05-10 
16:59:04 UTC (rev 44337)
+++ trunk/mcs/class/System.Drawing/System.Drawing/Color.cs      2005-05-10 
17:32:14 UTC (rev 44338)
@@ -5,9 +5,11 @@
 //     Dennis Hayes ([EMAIL PROTECTED])
 //     Ben Houston  ([EMAIL PROTECTED])
 //     Gonzalo Paniagua ([EMAIL PROTECTED])
+//     Juraj Skripsky ([EMAIL PROTECTED])
 //
 // (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 @@
                        (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));
+                       byte maxval = Math.Max (r, Math.Max (g, b));
+       
+                       return (float)(maxval + minval) / 510;
                }
 
-               public float GetSaturation (){
-                       float   cMax;
-                       float   cMin;
-                       float   l;
+               public float GetSaturation ()
+               {
+                       byte minval = Math.Min (r, Math.Min (g, b));
+                       byte maxval = Math.Max (r, Math.Max (g, b));
+                       
+                       int sum = maxval + minval;
+                       if (sum > 255)
+                               sum = 510 - sum;
 
-                       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);
-
-                       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;
+               public float GetHue ()
+               {
+                       byte minval = Math.Min (r, Math.Min (g, b));
+                       byte maxval = Math.Max (r, Math.Max (g, b));
+                       
+                       if (maxval == minval)
+                                       return 0.0f;
+                       
+                       float diff = (float)(maxval - minval);
+                       float rnorm = (maxval - r) / diff;
+                       float gnorm = (maxval - g) / diff;
+                       float bnorm = (maxval - b) / diff;
+       
+                       float hue = 0.0f;
+                       if (r == maxval) 
+                               hue = 60.0f * (6.0f + bnorm - gnorm);
+                       if (g == maxval) 
+                               hue = 60.0f * (2.0f + rnorm - bnorm);
+                       if (b  == maxval) 
+                               hue = 60.0f * (4.0f + gnorm - rnorm);
+                       if (hue > 360.0f) 
+                               hue = hue - 360.0f;
 
-                       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;
+                       return hue;
                }
                
                // -----------------------

Modified: trunk/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog
===================================================================
--- trunk/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog        
2005-05-10 16:59:04 UTC (rev 44337)
+++ trunk/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog        
2005-05-10 17:32:14 UTC (rev 44338)
@@ -1,3 +1,8 @@
+2005-05-10 Juraj Skripsky <[EMAIL PROTECTED]>
+
+       * TestColor.cs: Added tests for GetHue, GetBrightness
+       and GetSaturation.
+
 2005-02-24 Jordi Mas i Hernandez <[EMAIL PROTECTED]>
        * TestRegion.cs: added new IsVisible cases
 

Modified: trunk/mcs/class/System.Drawing/Test/System.Drawing/TestColor.cs
===================================================================
--- trunk/mcs/class/System.Drawing/Test/System.Drawing/TestColor.cs     
2005-05-10 16:59:04 UTC (rev 44337)
+++ trunk/mcs/class/System.Drawing/Test/System.Drawing/TestColor.cs     
2005-05-10 17:32:14 UTC (rev 44338)
@@ -897,6 +897,23 @@
                        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 ());
+               }
        }
 }
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to