3
// Copyright (C) 2008 GNOME Do
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.
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.
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/>.
27
namespace Do.Interface.CairoUtils
30
public static class CairoUtils
33
/// Sets a rounded rectangle path of the context
36
/// A <see cref="Context"/>
39
/// A <see cref="System.Double"/>
42
/// A <see cref="System.Double"/>
44
/// <param name="width">
45
/// A <see cref="System.Double"/>
47
/// <param name="height">
48
/// A <see cref="System.Double"/>
50
/// <param name="radius">
51
/// A <see cref="System.Double"/>
53
public static void SetRoundedRectanglePath (this Context cr, double x, double y,
54
double width, double height, double radius)
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);
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.
69
/// A <see cref="Context"/>
71
/// <param name="region">
72
/// A <see cref="Gdk.Rectangle"/>
74
/// <param name="radius">
75
/// A <see cref="System.Double"/>
77
/// <param name="stroke">
78
/// A <see cref="System.Boolean"/>
80
public static void SetRoundedRectanglePath (this Context cr, Gdk.Rectangle region, double radius, bool stroke)
83
SetRoundedRectanglePath (cr, (double)region.X+.5, (double)region.Y+.5, (double)region.Width-1,
84
(double)region.Height-1, radius);
86
SetRoundedRectanglePath (cr, region.X, region.Y, region.Width, region.Height, radius);
89
public static void AlphaFill (this Context cr)
92
cr.Color = new Cairo.Color (0, 0, 0, 0);
93
cr.Operator = Operator.Source;
99
/// Convert a Gdk.Color to Cairo.Colo
101
/// <param name="color">
102
/// A <see cref="Gdk.Color"/>
104
/// <param name="alpha">
105
/// A <see cref="System.Double"/>
108
/// A <see cref="Cairo.Color"/>
110
public static Cairo.Color ConvertToCairo (this Gdk.Color color, double alpha)
112
return new Cairo.Color ((double) color.Red/ushort.MaxValue,
113
(double) color.Green/ushort.MaxValue,
114
(double) color.Blue/ushort.MaxValue,
119
/// Convert a Cairo.Color to a Gdk.Color
121
/// <param name="color">
122
/// A <see cref="Cairo.Color"/>
125
/// A <see cref="Gdk.Color"/>
127
public static Gdk.Color ConvertToGdk (this Cairo.Color color)
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));
135
/// Adjust the brightness of a Color
137
/// <param name="color">
138
/// A <see cref="Cairo.Color"/>
140
/// <param name="brightness">
141
/// A <see cref="System.Double"/>
144
/// A <see cref="Cairo.Color"/>
146
public static Cairo.Color ShadeColor (this Cairo.Color color, double brightness)
148
Gdk.Color gdk_color = ConvertToGdk (color);
153
r = (byte) ((gdk_color.Red) >> 8);
154
g = (byte) ((gdk_color.Green) >> 8);
155
b = (byte) ((gdk_color.Blue) >> 8);
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);
161
return new Cairo.Color ((double) r/byte.MaxValue,
162
(double) g/byte.MaxValue,
163
(double) b/byte.MaxValue,
168
/// Adjust the saturation of a color
170
/// <param name="color">
171
/// A <see cref="Cairo.Color"/>
173
/// <param name="saturation">
174
/// A <see cref="System.Double"/>
177
/// A <see cref="Cairo.Color"/>
179
public static Cairo.Color SaturateColor (this Cairo.Color color, double saturation)
181
Gdk.Color gdk_color = ConvertToGdk (color);
186
r = (byte) ((gdk_color.Red) >> 8);
187
g = (byte) ((gdk_color.Green) >> 8);
188
b = (byte) ((gdk_color.Blue) >> 8);
190
Util.Appearance.RGBToHSV (r, g, b, out h, out s, out v);
192
Util.Appearance.HSVToRGB (h, s, v, out r, out g, out b);
194
return new Cairo.Color ((double) r/byte.MaxValue,
195
(double) g/byte.MaxValue,
196
(double) b/byte.MaxValue,
201
/// Adjust the Hue of a color
203
/// <param name="color">
204
/// A <see cref="Cairo.Color"/>
206
/// <param name="hue">
207
/// A <see cref="System.Double"/>
210
/// A <see cref="Cairo.Color"/>
212
public static Cairo.Color SetHue (this Cairo.Color color, double hue)
214
if (hue <= 0 || hue > 360)
217
Gdk.Color gdk_color = ConvertToGdk (color);
222
r = (byte) ((gdk_color.Red) >> 8);
223
g = (byte) ((gdk_color.Green) >> 8);
224
b = (byte) ((gdk_color.Blue) >> 8);
226
Util.Appearance.RGBToHSV (r, g, b, out h, out s, out v);
228
Util.Appearance.HSVToRGB (h, s, v, out r, out g, out b);
230
return new Cairo.Color ((double) r/byte.MaxValue,
231
(double) g/byte.MaxValue,
232
(double) b/byte.MaxValue,
237
/// Colorize grey colors to allow for better theme matching. Colors with saturation will
238
/// have nothing done to them
240
/// <param name="color">
241
/// A <see cref="Cairo.Color"/>
243
/// <param name="hue">
244
/// A <see cref="System.Double"/>
247
/// A <see cref="Cairo.Color"/>
249
public static Cairo.Color ColorizeColor (this Cairo.Color color, Cairo.Color reference_color)
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;
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);
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);
269
Util.Appearance.HSVToRGB (h0, s0, v0, out r0, out g0, out b0);
271
return new Cairo.Color ((double) r0/byte.MaxValue,
272
(double) g0/byte.MaxValue,
273
(double) b0/byte.MaxValue,
275
} else { //color is already saturated in some manner, do nothing
281
/// Set a color to use a maximum value
283
/// <param name="gdk_color">
284
/// A <see cref="Gdk.Color"/>
286
/// <param name="max_value">
287
/// A <see cref="System.Double"/>
290
/// A <see cref="Gdk.Color"/>
292
public static Gdk.Color SetMaximumValue (this Gdk.Color gdk_color, double max_value)
297
r = (byte) ((gdk_color.Red) >> 8);
298
g = (byte) ((gdk_color.Green) >> 8);
299
b = (byte) ((gdk_color.Blue) >> 8);
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);
305
return new Gdk.Color (r, g, b);
309
/// Set a color to use a maximum value
311
/// <param name="gdk_color">
312
/// A <see cref="Gdk.Color"/>
314
/// <param name="max_value">
315
/// A <see cref="System.Double"/>
318
/// A <see cref="Gdk.Color"/>
320
public static Gdk.Color SetMinimumValue (this Gdk.Color gdk_color, double min_value)
325
r = (byte) ((gdk_color.Red) >> 8);
326
g = (byte) ((gdk_color.Green) >> 8);
327
b = (byte) ((gdk_color.Blue) >> 8);
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);
333
return new Gdk.Color (r, g, b);
337
/// Convert a Gdk.color to a hex string
339
/// <param name="gdk_color">
340
/// A <see cref="Gdk.Color"/>
343
/// A <see cref="System.String"/>
345
public static string ColorToHexString (this Gdk.Color gdk_color)
348
r = (byte) ((gdk_color.Red) >> 8);
349
g = (byte) ((gdk_color.Green) >> 8);
350
b = (byte) ((gdk_color.Blue) >> 8);
352
return string.Format ("{0:X2}{1:X2}{2:X2}", r, g, b);