~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Components/MonoDevelop.Components/HslColor.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-02-02 11:39:59 UTC
  • mfrom: (1.4.4 upstream)
  • mto: (1.5.1 sid)
  • mto: This revision was merged to the branch mainline in revision 47.
  • Revision ID: james.westby@ubuntu.com-20100202113959-n3u848nfj35yyd03
* New upstream release
* debian/control:
  + Standards version 3.8.4 (no changes needed)
* debian/patches/remove_support_for_non_debian_functionality.patch,
  debian/patches/remove_support_for_soft_debugger.patch,
  debian/patches/remove_support_for_moonlight.patch,
  debian/rules:
  + Split patch into two pieces, to make it easier to enable either
    SDB or Moonlight support with a rebuild
* debian/monodevelop-moonlight.install,
  debian/monodevelop-debugger-sdb.install,
  debian/control:
  + Create packaging data for the Soft Debugger addin and Moonlight addin -
    and comment them out of debian/control as we can't provide them on
    Debian for now

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 MonoDevelop.Components
 
31
{
 
32
        public 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
                        double r = 0, g = 0, b = 0;
 
53
                        
 
54
                        if (hsl.L == 0)
 
55
                                return black;
 
56
                        
 
57
                        if (hsl.S == 0) {
 
58
                                r = g = b = hsl.L;
 
59
                        } else {
 
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;
 
62
                                
 
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++) {
 
66
                                        if (t3[i] < 0)
 
67
                                                t3[i] += 1.0;
 
68
                                        if (t3[i] > 1)
 
69
                                                t3[i]-=1.0;
 
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)
 
73
                                                clr[i] = temp2;
 
74
                                        else if (3.0 * t3[i] < 2.0)
 
75
                                                clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0);
 
76
                                        else
 
77
                                                clr[i] = temp1;
 
78
                                }
 
79
                                
 
80
                                r = clr[0];
 
81
                                g = clr[1];
 
82
                                b = clr[2];
 
83
                        }
 
84
                        return new Color ((byte)(255 * r), 
 
85
                                          (byte)(255 * g), 
 
86
                                          (byte)(255 * b));
 
87
                }
 
88
                
 
89
                public static implicit operator HslColor (Color color)
 
90
                {
 
91
                        return new HslColor (color);
 
92
                }
 
93
                
 
94
                public HslColor (Color color) : this ()
 
95
                {
 
96
                        double r = color.Red   / (double)ushort.MaxValue;
 
97
                        double g = color.Green / (double)ushort.MaxValue;
 
98
                        double b = color.Blue  / (double)ushort.MaxValue;
 
99
 
 
100
                        double v = System.Math.Max (r, g);
 
101
                        v = System.Math.Max (v, b);
 
102
 
 
103
                        double m = System.Math.Min (r, g);
 
104
                        m = System.Math.Min (m, b);
 
105
                        
 
106
                        this.L = (m + v) / 2.0;
 
107
                        if (this.L <= 0.0)
 
108
                                return;
 
109
                        double vm = v - m;
 
110
                        this.S = vm;
 
111
                        
 
112
                        if (this.S > 0.0) {
 
113
                                this.S /= (this.L <= 0.5) ? (v + m) : (2.0 - v - m);
 
114
                        } else {
 
115
                                return;
 
116
                        }
 
117
                        
 
118
                        double r2 = (v - r) / vm;
 
119
                        double g2 = (v - g) / vm;
 
120
                        double b2 = (v - b) / vm;
 
121
                        
 
122
                        if (r == v) {
 
123
                                this.H = (g == m ? 5.0 + b2 : 1.0 - g2);
 
124
                        } else if (g == v) {
 
125
                                this.H = (b == m ? 1.0 + r2 : 3.0 - b2);
 
126
                        } else {
 
127
                                this.H = (r == m ? 3.0 + g2 : 5.0 - r2);
 
128
                        }
 
129
                        this.H /= 6.0;
 
130
                }
 
131
                
 
132
                public static double Brightness (Gdk.Color c)
 
133
                {
 
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);
 
138
                }
 
139
                
 
140
                public override string ToString ()
 
141
                {
 
142
                        return string.Format ("[HslColor: H={0}, S={1}, L={2}]", H, S, L);
 
143
                }
 
144
        }
 
145
}