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 Mono.Addins.Gui
49
static Gdk.Color black = new Gdk.Color (0, 0, 0);
50
public static implicit operator Color (HslColor hsl)
52
if (hsl.L > 1) hsl.L = 1;
53
if (hsl.L < 0) hsl.L = 0;
54
if (hsl.H > 1) hsl.H = 1;
55
if (hsl.H < 0) hsl.H = 0;
56
if (hsl.S > 1) hsl.S = 1;
57
if (hsl.S < 0) hsl.S = 0;
59
double r = 0, g = 0, b = 0;
67
double temp2 = hsl.L <= 0.5 ? hsl.L * (1.0 + hsl.S) : hsl.L + hsl.S -(hsl.L * hsl.S);
68
double temp1 = 2.0 * hsl.L - temp2;
70
double[] t3 = new double[] { hsl.H + 1.0 / 3.0, hsl.H, hsl.H - 1.0 / 3.0};
71
double[] clr= new double[] { 0, 0, 0};
72
for (int i = 0; i < 3; i++) {
77
if (6.0 * t3[i] < 1.0)
78
clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0;
79
else if (2.0 * t3[i] < 1.0)
81
else if (3.0 * t3[i] < 2.0)
82
clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0);
91
return new Color ((byte)(255 * r),
96
public static Cairo.Color ToCairoColor (Gdk.Color color)
98
return new Cairo.Color ((double)color.Red / ushort.MaxValue,
99
(double)color.Green / ushort.MaxValue,
100
(double)color.Blue / ushort.MaxValue);
103
public static implicit operator Cairo.Color (HslColor hsl)
105
return ToCairoColor ((Gdk.Color)hsl);
108
public static implicit operator HslColor (Color color)
110
return new HslColor (color);
113
public HslColor (Color color) : this ()
115
double r = color.Red / (double)ushort.MaxValue;
116
double g = color.Green / (double)ushort.MaxValue;
117
double b = color.Blue / (double)ushort.MaxValue;
119
double v = System.Math.Max (r, g);
120
v = System.Math.Max (v, b);
122
double m = System.Math.Min (r, g);
123
m = System.Math.Min (m, b);
125
this.L = (m + v) / 2.0;
132
this.S /= (this.L <= 0.5) ? (v + m) : (2.0 - v - m);
137
double r2 = (v - r) / vm;
138
double g2 = (v - g) / vm;
139
double b2 = (v - b) / vm;
142
this.H = (g == m ? 5.0 + b2 : 1.0 - g2);
144
this.H = (b == m ? 1.0 + r2 : 3.0 - b2);
146
this.H = (r == m ? 3.0 + g2 : 5.0 - r2);
151
public static double Brightness (Gdk.Color c)
153
double r = c.Red / (double)ushort.MaxValue;
154
double g = c.Green / (double)ushort.MaxValue;
155
double b = c.Blue / (double)ushort.MaxValue;
156
return System.Math.Sqrt (r * .241 + g * .691 + b * .068);
159
public override string ToString ()
161
return string.Format ("[HslColor: H={0}, S={1}, L={2}]", H, S, L);