~ubuntu-branches/ubuntu/lucid/docky/lucid-proposed

« back to all changes in this revision

Viewing changes to Docky.CairoHelper/Docky.CairoHelper/Util.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2010-02-17 15:10:07 UTC
  • Revision ID: james.westby@ubuntu.com-20100217151007-msxpd0lsj300ndde
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  
 
2
//  Copyright (C) 2009 Jason Smith, Robert Dyer
 
3
// 
 
4
//  This program is free software: you can redistribute it and/or modify
 
5
//  it under the terms of the GNU General Public License as published by
 
6
//  the Free Software Foundation, either version 3 of the License, or
 
7
//  (at your option) any later version.
 
8
// 
 
9
//  This program is distributed in the hope that it will be useful,
 
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
//  GNU General Public License for more details.
 
13
// 
 
14
//  You should have received a copy of the GNU General Public License
 
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
// 
 
17
 
 
18
using System;
 
19
 
 
20
using Gdk;
 
21
 
 
22
namespace Docky.CairoHelper
 
23
{
 
24
        public static class Util
 
25
        {
 
26
                /// <summary>
 
27
                /// Set a color to use a maximum value
 
28
                /// </summary>
 
29
                /// <param name="gdk_color">
 
30
                /// A <see cref="Gdk.Color"/>
 
31
                /// </param>
 
32
                /// <param name="max_value">
 
33
                /// A <see cref="System.Double"/>
 
34
                /// </param>
 
35
                /// <returns>
 
36
                /// A <see cref="Gdk.Color"/>
 
37
                /// </returns>
 
38
                public static Gdk.Color SetMinimumValue (this Gdk.Color gdk_color, double min_value)
 
39
                {
 
40
                        byte r, g, b; 
 
41
                        double h, s, v;
 
42
                        
 
43
                        r = (byte) ((gdk_color.Red)   >> 8);
 
44
                        g = (byte) ((gdk_color.Green) >> 8);
 
45
                        b = (byte) ((gdk_color.Blue)  >> 8);
 
46
                        
 
47
                        RGBToHSV (r, g, b, out h, out s, out v);
 
48
                        v = Math.Max (v, min_value);
 
49
                        HSVToRGB (h, s, v, out r, out g, out b);
 
50
                        
 
51
                        return new Gdk.Color (r, g, b);
 
52
                }
 
53
                
 
54
                public static void RGBToHSV (byte r, byte g, byte b, 
 
55
                                                                         out double hue, out double sat, out double val)
 
56
                {
 
57
                        // Ported from Murrine Engine.
 
58
                        double red, green, blue;
 
59
                        double max, min;
 
60
                        double delta;
 
61
                        
 
62
                        red = (double) r;
 
63
                        green = (double) g;
 
64
                        blue = (double) b;
 
65
                        
 
66
                        hue = 0;
 
67
                        
 
68
                        max = Math.Max (red, Math.Max (blue, green));
 
69
                        min = Math.Min (red, Math.Min (blue, green));
 
70
                        delta = max - min;
 
71
                        val = max / 255.0 * 100.0;
 
72
                        
 
73
                        if (Math.Abs (delta) < 0.0001) {
 
74
                                sat = 0;
 
75
                        } else {
 
76
                                sat = (delta / max) * 100;
 
77
                                
 
78
                                if (red == max)   hue = (green - blue) / delta;
 
79
                                if (green == max) hue = 2 + (blue - red) / delta;
 
80
                                if (blue == max)  hue = 4 + (red - green) / delta;
 
81
                                
 
82
                                hue *= 60;
 
83
                                if (hue < 0) hue += 360;
 
84
                        }
 
85
                }
 
86
                
 
87
                public static void HSVToRGB (double hue, double sat, double val,
 
88
                                                                         out byte red, out byte green, out byte blue)
 
89
                {
 
90
                        double h, s, v;
 
91
                        double r = 0, g = 0, b = 0;
 
92
 
 
93
                        h = hue;
 
94
                        s = sat / 100;
 
95
                        v = val / 100;
 
96
 
 
97
                        if (s == 0) {
 
98
                                r = v;
 
99
                                g = v;
 
100
                                b = v;
 
101
                        } else {
 
102
                                int secNum;
 
103
                                double fracSec;
 
104
                                double p, q, t;
 
105
                                
 
106
                                secNum = (int) Math.Floor(h / 60);
 
107
                                fracSec = h/60 - secNum;
 
108
 
 
109
                                p = v * (1 - s);
 
110
                                q = v * (1 - s*fracSec);
 
111
                                t = v * (1 - s*(1 - fracSec));
 
112
                                
 
113
                                switch (secNum) {
 
114
                                        case 0:
 
115
                                                r = v;
 
116
                                                g = t;
 
117
                                                b = p;
 
118
                                                break;
 
119
                                        case 1:
 
120
                                                r = q;
 
121
                                                g = v;
 
122
                                                b = p;
 
123
                                                break;
 
124
                                        case 2:
 
125
                                                r = p;
 
126
                                                g = v;
 
127
                                                b = t;
 
128
                                                break;
 
129
                                        case 3:
 
130
                                                r = p;
 
131
                                                g = q;
 
132
                                                b = v;
 
133
                                                break;
 
134
                                        case 4:
 
135
                                                r = t;
 
136
                                                g = p;
 
137
                                                b = v;
 
138
                                                break;
 
139
                                        case 5:
 
140
                                                r = v;
 
141
                                                g = p;
 
142
                                                b = q;
 
143
                                                break;
 
144
                                }
 
145
                        }
 
146
                        red   = Convert.ToByte(r*255);
 
147
                        green = Convert.ToByte(g*255);
 
148
                        blue  = Convert.ToByte(b*255);
 
149
                }
 
150
        }
 
151
}