// 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
(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
}
// -----------------------
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 ());
+ }
}
}