5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2009 Xamarin, Inc (http://www.xamarin.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
52
public HslColor (double h, double s, double l)
59
public static implicit operator Color (HslColor hsl)
61
if (hsl.L > 1) hsl.L = 1;
62
if (hsl.L < 0) hsl.L = 0;
63
if (hsl.H > 1) hsl.H = 1;
64
if (hsl.H < 0) hsl.H = 0;
65
if (hsl.S > 1) hsl.S = 1;
66
if (hsl.S < 0) hsl.S = 0;
68
double r = 0, g = 0, b = 0;
71
return new Color (0f, 0f, 0f);
76
double temp2 = hsl.L <= 0.5 ? hsl.L * (1.0 + hsl.S) : hsl.L + hsl.S -(hsl.L * hsl.S);
77
double temp1 = 2.0 * hsl.L - temp2;
79
double[] t3 = new double[] { hsl.H + 1.0 / 3.0, hsl.H, hsl.H - 1.0 / 3.0};
80
double[] clr= new double[] { 0, 0, 0};
81
for (int i = 0; i < 3; i++) {
86
if (6.0 * t3[i] < 1.0)
87
clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0;
88
else if (2.0 * t3[i] < 1.0)
90
else if (3.0 * t3[i] < 2.0)
91
clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0);
100
return new Color (r, g, b);
103
public static implicit operator HslColor (Color color)
105
return new HslColor (color);
108
public HslColor (Color color)
110
double r = color.Red;
111
double g = color.Green;
112
double b = color.Blue;
114
double v = System.Math.Max (r, g);
115
v = System.Math.Max (v, b);
117
double m = System.Math.Min (r, g);
118
m = System.Math.Min (m, b);
120
this.L = (m + v) / 2.0;
127
this.S /= (this.L <= 0.5) ? (v + m) : (2.0 - v - m);
132
double r2 = (v - r) / vm;
133
double g2 = (v - g) / vm;
134
double b2 = (v - b) / vm;
137
this.H = (g == m ? 5.0 + b2 : 1.0 - g2);
139
this.H = (b == m ? 1.0 + r2 : 3.0 - b2);
141
this.H = (r == m ? 3.0 + g2 : 5.0 - r2);
146
public static double Brightness (Color c)
148
double r = c.Red / (double)ushort.MaxValue;
149
double g = c.Green / (double)ushort.MaxValue;
150
double b = c.Blue / (double)ushort.MaxValue;
151
return System.Math.Sqrt (r * .241 + g * .691 + b * .068);
154
public override string ToString ()
156
return string.Format ("[HslColor: H={0}, S={1}, L={2}]", H, S, L);