~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do.Interface.Windows/src/Do.Interface/Do.Interface.CairoUtils/CairoUtils.cs

  • Committer: Hardeep S
  • Date: 2009-06-23 03:53:12 UTC
  • Revision ID: ootz0rz@gmail.com-20090623035312-it8tb5wkha6nf31p
Added Do.Interface.Windows, and a bunch of cleanup with old MonoDevelop files elsewhere

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// CairoUtils.cs
 
2
// 
 
3
// Copyright (C) 2008 GNOME Do
 
4
//
 
5
// This program is free software: you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation, either version 3 of the License, or
 
8
// (at your option) any later version.
 
9
//
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
// GNU General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
//
 
18
 
 
19
using System;
 
20
 
 
21
using Cairo;
 
22
using Gdk;
 
23
 
 
24
using Do.Interface;
 
25
using Do.Universe;
 
26
 
 
27
namespace Do.Interface.CairoUtils
 
28
{
 
29
        
 
30
        public static class CairoUtils
 
31
        {
 
32
                /// <summary>
 
33
                /// Sets a rounded rectangle path of the context
 
34
                /// </summary>
 
35
                /// <param name="cr">
 
36
                /// A <see cref="Context"/>
 
37
                /// </param>
 
38
                /// <param name="x">
 
39
                /// A <see cref="System.Double"/>
 
40
                /// </param>
 
41
                /// <param name="y">
 
42
                /// A <see cref="System.Double"/>
 
43
                /// </param>
 
44
                /// <param name="width">
 
45
                /// A <see cref="System.Double"/>
 
46
                /// </param>
 
47
                /// <param name="height">
 
48
                /// A <see cref="System.Double"/>
 
49
                /// </param>
 
50
                /// <param name="radius">
 
51
                /// A <see cref="System.Double"/>
 
52
                /// </param>
 
53
                public static void SetRoundedRectanglePath (this Context cr, double x, double y, 
 
54
                                                            double width, double height, double radius)
 
55
                {
 
56
                        cr.MoveTo (x+radius, y);
 
57
                        cr.Arc (x+width-radius, y+radius, radius, Math.PI*1.5, Math.PI*2);
 
58
                        cr.Arc (x+width-radius, y+height-radius, radius, 0, Math.PI*.5);
 
59
                        cr.Arc (x+radius, y+height-radius, radius, Math.PI*.5, Math.PI);
 
60
                        cr.Arc (x+radius, y+radius, radius, Math.PI, Math.PI*1.5);
 
61
                }
 
62
                
 
63
                
 
64
                /// <summary>
 
65
                /// Set a rounded rectangle path from a Gdk.Rectangle.  If stroke is set to true, the path will be
 
66
                /// adjusted to allow for stroking with a single line width.
 
67
                /// </summary>
 
68
                /// <param name="cr">
 
69
                /// A <see cref="Context"/>
 
70
                /// </param>
 
71
                /// <param name="region">
 
72
                /// A <see cref="Gdk.Rectangle"/>
 
73
                /// </param>
 
74
                /// <param name="radius">
 
75
                /// A <see cref="System.Double"/>
 
76
                /// </param>
 
77
                /// <param name="stroke">
 
78
                /// A <see cref="System.Boolean"/>
 
79
                /// </param>
 
80
                public static void SetRoundedRectanglePath (this Context cr, Gdk.Rectangle region, double radius, bool stroke)
 
81
                {
 
82
                        if (stroke)
 
83
                                SetRoundedRectanglePath (cr, (double)region.X+.5, (double)region.Y+.5, (double)region.Width-1,
 
84
                                                         (double)region.Height-1, radius);
 
85
                        else
 
86
                                SetRoundedRectanglePath (cr, region.X, region.Y, region.Width, region.Height, radius);
 
87
                }
 
88
                
 
89
                public static void AlphaFill (this Context cr)
 
90
                {
 
91
                        cr.Save ();
 
92
                        cr.Color = new Cairo.Color (0, 0, 0, 0);
 
93
                        cr.Operator = Operator.Source;
 
94
                        cr.Paint ();
 
95
                        cr.Restore ();
 
96
                }
 
97
                
 
98
                /// <summary>
 
99
                /// Convert a Gdk.Color to Cairo.Colo
 
100
                /// </summary>
 
101
                /// <param name="color">
 
102
                /// A <see cref="Gdk.Color"/>
 
103
                /// </param>
 
104
                /// <param name="alpha">
 
105
                /// A <see cref="System.Double"/>
 
106
                /// </param>
 
107
                /// <returns>
 
108
                /// A <see cref="Cairo.Color"/>
 
109
                /// </returns>
 
110
                public static Cairo.Color ConvertToCairo (this Gdk.Color color, double alpha)
 
111
                {
 
112
                        return new Cairo.Color ((double) color.Red/ushort.MaxValue,
 
113
                                                (double) color.Green/ushort.MaxValue,
 
114
                                                (double) color.Blue/ushort.MaxValue,
 
115
                                                alpha);
 
116
                }
 
117
                
 
118
                /// <summary>
 
119
                /// Convert a Cairo.Color to a Gdk.Color
 
120
                /// </summary>
 
121
                /// <param name="color">
 
122
                /// A <see cref="Cairo.Color"/>
 
123
                /// </param>
 
124
                /// <returns>
 
125
                /// A <see cref="Gdk.Color"/>
 
126
                /// </returns>
 
127
                public static Gdk.Color ConvertToGdk (this Cairo.Color color)
 
128
                {
 
129
                        return new Gdk.Color (Convert.ToByte (color.R*byte.MaxValue),
 
130
                                              Convert.ToByte (color.G*byte.MaxValue),
 
131
                                              Convert.ToByte (color.B*byte.MaxValue));
 
132
                }
 
133
                
 
134
                /// <summary>
 
135
                /// Adjust the brightness of a Color
 
136
                /// </summary>
 
137
                /// <param name="color">
 
138
                /// A <see cref="Cairo.Color"/>
 
139
                /// </param>
 
140
                /// <param name="brightness">
 
141
                /// A <see cref="System.Double"/>
 
142
                /// </param>
 
143
                /// <returns>
 
144
                /// A <see cref="Cairo.Color"/>
 
145
                /// </returns>
 
146
                public static Cairo.Color ShadeColor (this Cairo.Color color, double brightness)
 
147
                {
 
148
                        Gdk.Color gdk_color = ConvertToGdk (color);
 
149
                        
 
150
                        byte r, g, b; 
 
151
                        double h, s, v;
 
152
                        
 
153
                        r = (byte) ((gdk_color.Red)   >> 8);
 
154
                        g = (byte) ((gdk_color.Green) >> 8);
 
155
                        b = (byte) ((gdk_color.Blue)  >> 8);
 
156
                        
 
157
                        Util.Appearance.RGBToHSV (r, g, b, out h, out s, out v);
 
158
                        v = Math.Min (100, v * brightness);
 
159
                        Util.Appearance.HSVToRGB (h, s, v, out r, out g, out b);
 
160
                        
 
161
                        return new Cairo.Color ((double) r/byte.MaxValue,
 
162
                                                (double) g/byte.MaxValue,
 
163
                                                (double) b/byte.MaxValue,
 
164
                                                color.A);
 
165
                }
 
166
                
 
167
                /// <summary>
 
168
                /// Adjust the saturation of a color
 
169
                /// </summary>
 
170
                /// <param name="color">
 
171
                /// A <see cref="Cairo.Color"/>
 
172
                /// </param>
 
173
                /// <param name="saturation">
 
174
                /// A <see cref="System.Double"/>
 
175
                /// </param>
 
176
                /// <returns>
 
177
                /// A <see cref="Cairo.Color"/>
 
178
                /// </returns>
 
179
                public static Cairo.Color SaturateColor (this Cairo.Color color, double saturation)
 
180
                {
 
181
                        Gdk.Color gdk_color = ConvertToGdk (color);
 
182
                        
 
183
                        byte r, g, b; 
 
184
                        double h, s, v;
 
185
                        
 
186
                        r = (byte) ((gdk_color.Red)   >> 8);
 
187
                        g = (byte) ((gdk_color.Green) >> 8);
 
188
                        b = (byte) ((gdk_color.Blue)  >> 8);
 
189
                        
 
190
                        Util.Appearance.RGBToHSV (r, g, b, out h, out s, out v);
 
191
                        s *= saturation;
 
192
                        Util.Appearance.HSVToRGB (h, s, v, out r, out g, out b);
 
193
                        
 
194
                        return new Cairo.Color ((double) r/byte.MaxValue,
 
195
                                                (double) g/byte.MaxValue,
 
196
                                                (double) b/byte.MaxValue,
 
197
                                                color.A);
 
198
                }
 
199
                
 
200
                /// <summary>
 
201
                /// Adjust the Hue of a color
 
202
                /// </summary>
 
203
                /// <param name="color">
 
204
                /// A <see cref="Cairo.Color"/>
 
205
                /// </param>
 
206
                /// <param name="hue">
 
207
                /// A <see cref="System.Double"/>
 
208
                /// </param>
 
209
                /// <returns>
 
210
                /// A <see cref="Cairo.Color"/>
 
211
                /// </returns>
 
212
                public static Cairo.Color SetHue (this Cairo.Color color, double hue)
 
213
                {
 
214
                        if (hue <= 0 || hue > 360)
 
215
                                return color;
 
216
                        
 
217
                        Gdk.Color gdk_color = ConvertToGdk (color);
 
218
                        
 
219
                        byte r, g, b; 
 
220
                        double h, s, v;
 
221
                        
 
222
                        r = (byte) ((gdk_color.Red)   >> 8);
 
223
                        g = (byte) ((gdk_color.Green) >> 8);
 
224
                        b = (byte) ((gdk_color.Blue)  >> 8);
 
225
                        
 
226
                        Util.Appearance.RGBToHSV (r, g, b, out h, out s, out v);
 
227
                        h = hue;
 
228
                        Util.Appearance.HSVToRGB (h, s, v, out r, out g, out b);
 
229
                        
 
230
                        return new Cairo.Color ((double) r/byte.MaxValue,
 
231
                                                (double) g/byte.MaxValue,
 
232
                                                (double) b/byte.MaxValue,
 
233
                                                color.A);
 
234
                }
 
235
                
 
236
                /// <summary>
 
237
                /// Colorize grey colors to allow for better theme matching.  Colors with saturation will
 
238
                /// have nothing done to them
 
239
                /// </summary>
 
240
                /// <param name="color">
 
241
                /// A <see cref="Cairo.Color"/>
 
242
                /// </param>
 
243
                /// <param name="hue">
 
244
                /// A <see cref="System.Double"/>
 
245
                /// </param>
 
246
                /// <returns>
 
247
                /// A <see cref="Cairo.Color"/>
 
248
                /// </returns>
 
249
                public static Cairo.Color ColorizeColor (this Cairo.Color color, Cairo.Color reference_color)
 
250
                {
 
251
                        //Color has no saturation to it, we need to give it some
 
252
                        if (color.B == color.G && color.B == color.R) {
 
253
                                Gdk.Color gdk_color0 = ConvertToGdk (color);
 
254
                                Gdk.Color gdk_color1 = ConvertToGdk (reference_color);
 
255
                                byte r0, g0, b0, r1, g1, b1; 
 
256
                                double h0, s0, v0, h1, s1, v1;
 
257
                                
 
258
                                r0 = (byte) ((gdk_color0.Red)   >> 8);
 
259
                                g0 = (byte) ((gdk_color0.Green) >> 8);
 
260
                                b0 = (byte) ((gdk_color0.Blue)  >> 8);
 
261
                                r1 = (byte) ((gdk_color1.Red)   >> 8);
 
262
                                g1 = (byte) ((gdk_color1.Green) >> 8);
 
263
                                b1 = (byte) ((gdk_color1.Blue)  >> 8);
 
264
                                
 
265
                                Util.Appearance.RGBToHSV (r0, g0, b0, out h0, out s0, out v0);
 
266
                                Util.Appearance.RGBToHSV (r1, g1, b1, out h1, out s1, out v1);
 
267
                                h0 = h1;
 
268
                                s0 = (s0+s1)/2;
 
269
                                Util.Appearance.HSVToRGB (h0, s0, v0, out r0, out g0, out b0);
 
270
                                
 
271
                                return new Cairo.Color ((double) r0/byte.MaxValue,
 
272
                                                        (double) g0/byte.MaxValue,
 
273
                                                        (double) b0/byte.MaxValue,
 
274
                                                        color.A);
 
275
                        } else { //color is already saturated in some manner, do nothing
 
276
                                return color;
 
277
                        }
 
278
                }
 
279
                
 
280
                /// <summary>
 
281
                /// Set a color to use a maximum value
 
282
                /// </summary>
 
283
                /// <param name="gdk_color">
 
284
                /// A <see cref="Gdk.Color"/>
 
285
                /// </param>
 
286
                /// <param name="max_value">
 
287
                /// A <see cref="System.Double"/>
 
288
                /// </param>
 
289
                /// <returns>
 
290
                /// A <see cref="Gdk.Color"/>
 
291
                /// </returns>
 
292
                public static Gdk.Color SetMaximumValue (this Gdk.Color gdk_color, double max_value)
 
293
                {
 
294
                        byte r, g, b; 
 
295
                        double h, s, v;
 
296
                        
 
297
                        r = (byte) ((gdk_color.Red)   >> 8);
 
298
                        g = (byte) ((gdk_color.Green) >> 8);
 
299
                        b = (byte) ((gdk_color.Blue)  >> 8);
 
300
                        
 
301
                        Util.Appearance.RGBToHSV (r, g, b, out h, out s, out v);
 
302
                        v = Math.Min (v, max_value);
 
303
                        Util.Appearance.HSVToRGB (h, s, v, out r, out g, out b);
 
304
                        
 
305
                        return new Gdk.Color (r, g, b);
 
306
                }
 
307
                
 
308
                /// <summary>
 
309
                /// Set a color to use a maximum value
 
310
                /// </summary>
 
311
                /// <param name="gdk_color">
 
312
                /// A <see cref="Gdk.Color"/>
 
313
                /// </param>
 
314
                /// <param name="max_value">
 
315
                /// A <see cref="System.Double"/>
 
316
                /// </param>
 
317
                /// <returns>
 
318
                /// A <see cref="Gdk.Color"/>
 
319
                /// </returns>
 
320
                public static Gdk.Color SetMinimumValue (this Gdk.Color gdk_color, double min_value)
 
321
                {
 
322
                        byte r, g, b; 
 
323
                        double h, s, v;
 
324
                        
 
325
                        r = (byte) ((gdk_color.Red)   >> 8);
 
326
                        g = (byte) ((gdk_color.Green) >> 8);
 
327
                        b = (byte) ((gdk_color.Blue)  >> 8);
 
328
                        
 
329
                        Util.Appearance.RGBToHSV (r, g, b, out h, out s, out v);
 
330
                        v = Math.Max (v, min_value);
 
331
                        Util.Appearance.HSVToRGB (h, s, v, out r, out g, out b);
 
332
                        
 
333
                        return new Gdk.Color (r, g, b);
 
334
                }
 
335
                
 
336
                /// <summary>
 
337
                /// Convert a Gdk.color to a hex string
 
338
                /// </summary>
 
339
                /// <param name="gdk_color">
 
340
                /// A <see cref="Gdk.Color"/>
 
341
                /// </param>
 
342
                /// <returns>
 
343
                /// A <see cref="System.String"/>
 
344
                /// </returns>
 
345
                public static string ColorToHexString (this Gdk.Color gdk_color)
 
346
                {
 
347
                        byte r, g, b;
 
348
                        r = (byte) ((gdk_color.Red)   >> 8);
 
349
                        g = (byte) ((gdk_color.Green) >> 8);
 
350
                        b = (byte) ((gdk_color.Blue)  >> 8);
 
351
                        
 
352
                        return string.Format ("{0:X2}{1:X2}{2:X2}", r, g, b);
 
353
                }
 
354
        }
 
355
}