5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.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
30
namespace MonoDevelop.Components
32
public struct HslColor
49
static Gdk.Color black = new Gdk.Color (0, 0, 0);
50
public static implicit operator Color (HslColor hsl)
52
double r = 0, g = 0, b = 0;
60
double temp2 = hsl.L <= 0.5 ? hsl.L * (1.0 + hsl.S) : hsl.L + hsl.S -(hsl.L * hsl.S);
61
double temp1 = 2.0 * hsl.L - temp2;
63
double[] t3 = new double[] { hsl.H + 1.0 / 3.0, hsl.H, hsl.H - 1.0 / 3.0};
64
double[] clr= new double[] { 0, 0, 0};
65
for (int i = 0; i < 3; i++) {
70
if (6.0 * t3[i] < 1.0)
71
clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0;
72
else if (2.0 * t3[i] < 1.0)
74
else if (3.0 * t3[i] < 2.0)
75
clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0);
84
return new Color ((byte)(255 * r),
89
public static implicit operator HslColor (Color color)
91
return new HslColor (color);
94
public HslColor (Color color) : this ()
96
double r = color.Red / (double)ushort.MaxValue;
97
double g = color.Green / (double)ushort.MaxValue;
98
double b = color.Blue / (double)ushort.MaxValue;
100
double v = System.Math.Max (r, g);
101
v = System.Math.Max (v, b);
103
double m = System.Math.Min (r, g);
104
m = System.Math.Min (m, b);
106
this.L = (m + v) / 2.0;
113
this.S /= (this.L <= 0.5) ? (v + m) : (2.0 - v - m);
118
double r2 = (v - r) / vm;
119
double g2 = (v - g) / vm;
120
double b2 = (v - b) / vm;
123
this.H = (g == m ? 5.0 + b2 : 1.0 - g2);
125
this.H = (b == m ? 1.0 + r2 : 3.0 - b2);
127
this.H = (r == m ? 3.0 + g2 : 5.0 - r2);
132
public static double Brightness (Gdk.Color c)
134
double r = c.Red / (double)ushort.MaxValue;
135
double g = c.Green / (double)ushort.MaxValue;
136
double b = c.Blue / (double)ushort.MaxValue;
137
return System.Math.Sqrt (r * .241 + g * .691 + b * .068);
140
public override string ToString ()
142
return string.Format ("[HslColor: H={0}, S={1}, L={2}]", H, S, L);