~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt.Drawing/HslColor.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

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 Xamarin, Inc (http://www.xamarin.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
 
 
29
namespace Xwt.Drawing
 
30
{
 
31
        class HslColor
 
32
        {
 
33
                public double H {
 
34
                        get;
 
35
                        set;
 
36
                }
 
37
                
 
38
                public double S {
 
39
                        get;
 
40
                        set;
 
41
                }
 
42
                
 
43
                public double L {
 
44
                        get;
 
45
                        set;
 
46
                }
 
47
                
 
48
                public HslColor ()
 
49
                {
 
50
                }
 
51
                
 
52
                public HslColor (double h, double s, double l)
 
53
                {
 
54
                        H = h;
 
55
                        S = s;
 
56
                        L = l;
 
57
                }
 
58
                
 
59
                public static implicit operator Color (HslColor hsl)
 
60
                {
 
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;
 
67
                        
 
68
                        double r = 0, g = 0, b = 0;
 
69
                        
 
70
                        if (hsl.L == 0)
 
71
                                return new Color (0f, 0f, 0f);
 
72
                        
 
73
                        if (hsl.S == 0) {
 
74
                                r = g = b = hsl.L;
 
75
                        } else {
 
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;
 
78
                                
 
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++) {
 
82
                                        if (t3[i] < 0)
 
83
                                                t3[i] += 1.0;
 
84
                                        if (t3[i] > 1)
 
85
                                                t3[i]-=1.0;
 
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)
 
89
                                                clr[i] = temp2;
 
90
                                        else if (3.0 * t3[i] < 2.0)
 
91
                                                clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0);
 
92
                                        else
 
93
                                                clr[i] = temp1;
 
94
                                }
 
95
                                
 
96
                                r = clr[0];
 
97
                                g = clr[1];
 
98
                                b = clr[2];
 
99
                        }
 
100
                        return new Color (r, g, b);
 
101
                }
 
102
                
 
103
                public static implicit operator HslColor (Color color)
 
104
                {
 
105
                        return new HslColor (color);
 
106
                }
 
107
                
 
108
                public HslColor (Color color)
 
109
                {
 
110
                        double r = color.Red;
 
111
                        double g = color.Green;
 
112
                        double b = color.Blue;
 
113
 
 
114
                        double v = System.Math.Max (r, g);
 
115
                        v = System.Math.Max (v, b);
 
116
 
 
117
                        double m = System.Math.Min (r, g);
 
118
                        m = System.Math.Min (m, b);
 
119
                        
 
120
                        this.L = (m + v) / 2.0;
 
121
                        if (this.L <= 0.0)
 
122
                                return;
 
123
                        double vm = v - m;
 
124
                        this.S = vm;
 
125
                        
 
126
                        if (this.S > 0.0) {
 
127
                                this.S /= (this.L <= 0.5) ? (v + m) : (2.0 - v - m);
 
128
                        } else {
 
129
                                return;
 
130
                        }
 
131
                        
 
132
                        double r2 = (v - r) / vm;
 
133
                        double g2 = (v - g) / vm;
 
134
                        double b2 = (v - b) / vm;
 
135
                        
 
136
                        if (r == v) {
 
137
                                this.H = (g == m ? 5.0 + b2 : 1.0 - g2);
 
138
                        } else if (g == v) {
 
139
                                this.H = (b == m ? 1.0 + r2 : 3.0 - b2);
 
140
                        } else {
 
141
                                this.H = (r == m ? 3.0 + g2 : 5.0 - r2);
 
142
                        }
 
143
                        this.H /= 6.0;
 
144
                }
 
145
                
 
146
                public static double Brightness (Color c)
 
147
                {
 
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);
 
152
                }
 
153
                
 
154
                public override string ToString ()
 
155
                {
 
156
                        return string.Format ("[HslColor: H={0}, S={1}, L={2}]", H, S, L);
 
157
                }
 
158
        }
 
159
}