~ubuntu-branches/ubuntu/trusty/mono-addins/trusty-proposed

« back to all changes in this revision

Viewing changes to Mono.Addins.Gui/Mono.Addins.Gui/HslColor.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-04-25 11:11:33 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110425111133-t05u5p7o5fxx70fu
Tags: 0.6-2
Upload to Unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// HslColor.cs
 
3
//  
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@novell.com>
 
6
// 
 
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using Gdk;
 
29
 
 
30
namespace Mono.Addins.Gui
 
31
{
 
32
        struct HslColor
 
33
        {
 
34
                public double H {
 
35
                        get;
 
36
                        set;
 
37
                }
 
38
                
 
39
                public double S {
 
40
                        get;
 
41
                        set;
 
42
                }
 
43
                
 
44
                public double L {
 
45
                        get;
 
46
                        set;
 
47
                }
 
48
                
 
49
                static Gdk.Color black = new  Gdk.Color (0, 0, 0);
 
50
                public static implicit operator Color (HslColor hsl)
 
51
                {
 
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;
 
58
                        
 
59
                        double r = 0, g = 0, b = 0;
 
60
                        
 
61
                        if (hsl.L == 0)
 
62
                                return black;
 
63
                        
 
64
                        if (hsl.S == 0) {
 
65
                                r = g = b = hsl.L;
 
66
                        } else {
 
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;
 
69
                                
 
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++) {
 
73
                                        if (t3[i] < 0)
 
74
                                                t3[i] += 1.0;
 
75
                                        if (t3[i] > 1)
 
76
                                                t3[i]-=1.0;
 
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)
 
80
                                                clr[i] = temp2;
 
81
                                        else if (3.0 * t3[i] < 2.0)
 
82
                                                clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0);
 
83
                                        else
 
84
                                                clr[i] = temp1;
 
85
                                }
 
86
                                
 
87
                                r = clr[0];
 
88
                                g = clr[1];
 
89
                                b = clr[2];
 
90
                        }
 
91
                        return new Color ((byte)(255 * r), 
 
92
                                          (byte)(255 * g), 
 
93
                                          (byte)(255 * b));
 
94
                }
 
95
                
 
96
                public static Cairo.Color ToCairoColor (Gdk.Color color)
 
97
                {
 
98
                        return new Cairo.Color ((double)color.Red / ushort.MaxValue,
 
99
                                                (double)color.Green / ushort.MaxValue,
 
100
                                                (double)color.Blue / ushort.MaxValue);
 
101
                }
 
102
                
 
103
                public static implicit operator Cairo.Color (HslColor hsl)
 
104
                {
 
105
                        return ToCairoColor ((Gdk.Color)hsl);
 
106
                }
 
107
                
 
108
                public static implicit operator HslColor (Color color)
 
109
                {
 
110
                        return new HslColor (color);
 
111
                }
 
112
                
 
113
                public HslColor (Color color) : this ()
 
114
                {
 
115
                        double r = color.Red   / (double)ushort.MaxValue;
 
116
                        double g = color.Green / (double)ushort.MaxValue;
 
117
                        double b = color.Blue  / (double)ushort.MaxValue;
 
118
 
 
119
                        double v = System.Math.Max (r, g);
 
120
                        v = System.Math.Max (v, b);
 
121
 
 
122
                        double m = System.Math.Min (r, g);
 
123
                        m = System.Math.Min (m, b);
 
124
                        
 
125
                        this.L = (m + v) / 2.0;
 
126
                        if (this.L <= 0.0)
 
127
                                return;
 
128
                        double vm = v - m;
 
129
                        this.S = vm;
 
130
                        
 
131
                        if (this.S > 0.0) {
 
132
                                this.S /= (this.L <= 0.5) ? (v + m) : (2.0 - v - m);
 
133
                        } else {
 
134
                                return;
 
135
                        }
 
136
                        
 
137
                        double r2 = (v - r) / vm;
 
138
                        double g2 = (v - g) / vm;
 
139
                        double b2 = (v - b) / vm;
 
140
                        
 
141
                        if (r == v) {
 
142
                                this.H = (g == m ? 5.0 + b2 : 1.0 - g2);
 
143
                        } else if (g == v) {
 
144
                                this.H = (b == m ? 1.0 + r2 : 3.0 - b2);
 
145
                        } else {
 
146
                                this.H = (r == m ? 3.0 + g2 : 5.0 - r2);
 
147
                        }
 
148
                        this.H /= 6.0;
 
149
                }
 
150
                
 
151
                public static double Brightness (Gdk.Color c)
 
152
                {
 
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);
 
157
                }
 
158
                
 
159
                public override string ToString ()
 
160
                {
 
161
                        return string.Format ("[HslColor: H={0}, S={1}, L={2}]", H, S, L);
 
162
                }
 
163
        }
 
164
}