5 public enum ButtonState
{
10 public enum ButtonType
{
15 public static Gdk
.Color
color_from_string (string webcolor
) {
17 Gdk
.Color
.parse(webcolor
, out color
);
21 public static void set_source_from_color (Cairo
.Context cr
, Gdk
.Color color
, double alpha
= 1.0) {
22 cr
.set_source_rgba((double)color
.red
/ (double)uint16.MAX
, (double)color
.green
/ (double)uint16.MAX
, (double)color
.blue
/ (double)uint16.MAX
, alpha
);
25 public static void set_source_from_color_string (Cairo
.Context cr
, string color
, double alpha
= 1.0) {
26 set_source_from_color (cr
, color_from_string (color
), alpha
);
29 public static void add_color_stop (Cairo
.Pattern p
, double offset
, Gdk
.Color color
, double alpha
= 1.0) {
30 p
.add_color_stop_rgba(offset
, (double)color
.red
/ (double)uint16.MAX
, (double)color
.green
/ (double)uint16.MAX
, (double)color
.blue
/ (double)uint16.MAX
, alpha
);
33 public static Cairo
.Pattern
create_gradient (double x1
, double y1
, double x2
, double y2
, Gdk
.Color start
, Gdk
.Color stop
, double alpha_start
= 1.0, double alpha_stop
= 1.0) {
34 Cairo
.Pattern gradient
= new Cairo
.Pattern
.linear(x1
, y1
, x2
, y2
);
35 add_color_stop(gradient
, 0, start
, alpha_start
);
36 add_color_stop(gradient
, 1, stop
, alpha_stop
);
40 public static void rounded_rect (Cairo
.Context cr
, double x
, double y
, double w
, double h
, double radius_x
=5, double radius_y
=5) {
41 // from mono moonlight aka mono silverlight
42 // test limits (without using multiplications)
43 // http://graphics.stanford.edu/courses/cs248-98-fall/Final/q1.html
44 double ARC_TO_BEZIER
= 0.55228475;
46 if (radius_x
> w
- radius_x
)
48 if (radius_y
> h
- radius_y
)
51 // approximate (quite close) the arc using a bezier curve
52 var c1
= ARC_TO_BEZIER
* radius_x
;
53 var c2
= ARC_TO_BEZIER
* radius_y
;
56 cr
.move_to ( x
+ radius_x
, y
);
57 cr
.rel_line_to ( w
- 2 * radius_x
, 0.0);
58 cr
.rel_curve_to ( c1
, 0.0, radius_x
, c2
, radius_x
, radius_y
);
59 cr
.rel_line_to ( 0, h
- 2 * radius_y
);
60 cr
.rel_curve_to ( 0.0, c2
, c1
- radius_x
, radius_y
, -radius_x
, radius_y
);
61 cr
.rel_line_to ( -w
+ 2 * radius_x
, 0);
62 cr
.rel_curve_to ( -c1
, 0, -radius_x
, -c2
, -radius_x
, -radius_y
);
63 cr
.rel_line_to (0, -h
+ 2 * radius_y
);
64 cr
.rel_curve_to (0.0, -c2
, radius_x
- c1
, -radius_y
, radius_x
, -radius_y
);
68 public static void background_gradient(Cairo
.Context cr
, double w
, double h
) {
70 Color background_gradient_start
;
71 Color background_gradient_stop
;
72 Color
.parse("#bebdc2", out background_gradient_start
);
73 Color
.parse("#b1b4b9", out background_gradient_stop
);
75 cr
.rectangle (0, 0, w
, h
);
76 Cairo
.Pattern background_gradient
= new Cairo
.Pattern
.linear(0, 0, 0, h
);
77 add_color_stop(background_gradient
, 0, background_gradient_start
);
78 add_color_stop(background_gradient
, 1, background_gradient_stop
);
79 cr
.set_source (background_gradient
);
83 public static double modula(double number
, double divisor
) {
84 return (((int)number
% (int)divisor
) + (number
- (int)number
));
88 public double hue
{get; set;}
89 public double saturation
{get; set;}
90 public double brightness
{get; set;}
92 public string to_string() {
93 return "HSB (%f, %f, %f)".printf (hue
, saturation
, brightness
);
96 public Gdk
.Color
to_gdk_color() {
98 var hue_shift
= new
double[3];
99 var color_shift
= new
double[3];
104 Gdk
.Color color
= Gdk
.Color();
106 if (brightness
<= 0.5)
107 m2
= brightness
* (1 + saturation
);
109 m2
= brightness
+ saturation
- brightness
* saturation
;
111 m1
= 2 * brightness
- m2
;
113 hue_shift
[0] = hue
+ 120;
115 hue_shift
[2] = hue
- 120;
117 color_shift
[0] = color_shift
[1] = color_shift
[2] = brightness
;
119 i
= (saturation
== 0)?
3:0;
126 m3
= modula(m3
, 360);
128 m3
= 360 - modula(Math
.fabs(m3
), 360);
131 color_shift
[i
] = m1
+ (m2
- m1
) * m3
/ 60;
135 color_shift
[i
] = m1
+ (m2
- m1
) * (240 - m3
) / 60;
140 color
.red
= (uint16) (color_shift
[0] * (double)uint16.MAX
);
141 color
.green
= (uint16) (color_shift
[1] * (double)uint16.MAX
);
142 color
.blue
= (uint16) (color_shift
[2] * (double)uint16.MAX
);
147 public void from_gdk_color(Gdk
.Color color
) {
148 double min
, max
, delta
;
149 double red
, green
, blue
;
151 red
= (double)color
.red
/ (double)uint16.MAX
;
152 green
= (double)color
.green
/ (double)uint16.MAX
;
153 blue
= (double)color
.blue
/ (double)uint16.MAX
;
177 brightness
= (max
+ min
) / 2.0;
179 if (Math
.fabs(max
- min
) < 0.0001)
186 if (brightness
<= 0.5)
187 saturation
= (max
- min
) / (max
+ min
);
189 saturation
= (max
- min
) / (2.0 - max
- min
);
194 hue
= (green
- blue
) / delta
;
195 else if (green
== max
)
196 hue
= 2.0 + (blue
- red
) / delta
;
197 else if (blue
== max
)
198 hue
= 4.0 + (red
- green
) / delta
;
207 public static Gdk
.Color
shade_color(Gdk
.Color orig
, double shade_ratio
) {
208 HSB hsb
= new
HSB ();
209 hsb
.from_gdk_color (orig
);
211 hsb
.brightness
= Math
.fmin (hsb
.brightness
* shade_ratio
, 1.0);
212 hsb
.brightness
= Math
.fmax (hsb
.brightness
, 0.0);
214 hsb
.saturation
= Math
.fmin (hsb
.saturation
* shade_ratio
, 1.0);
215 hsb
.saturation
= Math
.fmax (hsb
.saturation
, 0.0);
217 Gdk
.Color result
= hsb
.to_gdk_color ();
221 } // namespace Prolooks