From 87b416f06ed29b1ef94feb850ba35648952fe172 Mon Sep 17 00:00:00 2001 From: Juraj Skripsky Date: Tue, 10 May 2005 17:32:14 +0000 Subject: [PATCH] 2005-05-10 Juraj Skripsky * 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 --- .../System.Drawing/System.Drawing/ChangeLog | 5 + .../System.Drawing/System.Drawing/Color.cs | 105 +++++++----------- .../Test/System.Drawing/ChangeLog | 5 + .../Test/System.Drawing/TestColor.cs | 17 +++ 4 files changed, 68 insertions(+), 64 deletions(-) diff --git a/mcs/class/System.Drawing/System.Drawing/ChangeLog b/mcs/class/System.Drawing/System.Drawing/ChangeLog index d158eed7871..82980836812 100644 --- a/mcs/class/System.Drawing/System.Drawing/ChangeLog +++ b/mcs/class/System.Drawing/System.Drawing/ChangeLog @@ -1,3 +1,8 @@ +2005-05-10 Juraj Skripsky + + * TestColor.cs: Added tests for GetHue, GetBrightness + and GetSaturation. + 2005-05-09 Sebastien Pouliot * gdipFunctions.cs: Use PlatformID.Unix under NET_2_0. diff --git a/mcs/class/System.Drawing/System.Drawing/Color.cs b/mcs/class/System.Drawing/System.Drawing/Color.cs index 918b5f11c1e..ce0d9fe9093 100644 --- a/mcs/class/System.Drawing/System.Drawing/Color.cs +++ b/mcs/class/System.Drawing/System.Drawing/Color.cs @@ -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)); + byte maxval = Math.Max (r, Math.Max (g, b)); + + return (float)(maxval + minval) / 510; } - 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)); + byte maxval = Math.Max (r, Math.Max (g, b)); + + int sum = maxval + minval; + if (sum > 255) + sum = 510 - sum; - 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)); + 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; + + return hue; } // ----------------------- diff --git a/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog b/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog index 59e63fceca2..85d19efce19 100644 --- a/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog +++ b/mcs/class/System.Drawing/Test/System.Drawing/ChangeLog @@ -1,3 +1,8 @@ +2005-05-10 Juraj Skripsky + + * TestColor.cs: Added tests for GetHue, GetBrightness + and GetSaturation. + 2005-02-24 Jordi Mas i Hernandez * TestRegion.cs: added new IsVisible cases diff --git a/mcs/class/System.Drawing/Test/System.Drawing/TestColor.cs b/mcs/class/System.Drawing/Test/System.Drawing/TestColor.cs index c71fa40962c..d6d9f7eb930 100644 --- a/mcs/class/System.Drawing/Test/System.Drawing/TestColor.cs +++ b/mcs/class/System.Drawing/Test/System.Drawing/TestColor.cs @@ -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 ()); + } } } -- 2.25.1