* _build_ -> build
[libprolooks.git] / prolooks / Helpers.vala
blob2787adfe31249f9c92e646704a68324e82b57abd
1 using Gtk;
2 using Gdk;
4 namespace Prolooks {
5 public enum ButtonState {
6 NORMAL,
7 PRESSED;
10 public enum ButtonType {
11 PRESS_BUTTON,
12 TOGGLE_BUTTON;
15 public static Gdk.Color color_from_string (string webcolor) {
16 Gdk.Color color;
17 Gdk.Color.parse(webcolor, out color);
18 return 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);
37 return gradient;
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)
47 radius_x = w / 2;
48 if (radius_y > h - radius_y)
49 radius_y = h / 2;
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;
55 cr.new_path();
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);
65 cr.close_path ();
68 public static void background_gradient(Cairo.Context cr, double w, double h) {
69 // outside background
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);
80 cr.fill ();
83 public static double modula(double number, double divisor) {
84 return (((int)number % (int)divisor) + (number - (int)number));
87 public class HSB {
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() {
97 int i;
98 var hue_shift = new double[3];
99 var color_shift = new double[3];
100 double m1;
101 double m2;
102 double m3;
104 Gdk.Color color = Gdk.Color();
106 if (brightness <= 0.5)
107 m2 = brightness * (1 + saturation);
108 else
109 m2 = brightness + saturation - brightness * saturation;
111 m1 = 2 * brightness - m2;
113 hue_shift[0] = hue + 120;
114 hue_shift[1] = hue;
115 hue_shift[2] = hue - 120;
117 color_shift[0] = color_shift[1] = color_shift[2] = brightness;
119 i = (saturation == 0)?3:0;
121 for (; i < 3; i++)
123 m3 = hue_shift[i];
125 if (m3 > 360)
126 m3 = modula(m3, 360);
127 else if (m3 < 0)
128 m3 = 360 - modula(Math.fabs(m3), 360);
130 if (m3 < 60)
131 color_shift[i] = m1 + (m2 - m1) * m3 / 60;
132 else if (m3 < 180)
133 color_shift[i] = m2;
134 else if (m3 < 240)
135 color_shift[i] = m1 + (m2 - m1) * (240 - m3) / 60;
136 else
137 color_shift[i] = m1;
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);
144 return color;
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;
155 if (red > green) {
156 if (red > blue)
157 max = red;
158 else
159 max = blue;
161 if (green < blue)
162 min = green;
163 else
164 min = blue;
165 } else {
166 if (green > blue)
167 max = green;
168 else
169 max = blue;
171 if (red < blue)
172 min = red;
173 else
174 min = blue;
177 brightness = (max + min) / 2.0;
179 if (Math.fabs(max - min) < 0.0001)
181 hue = 0.0;
182 saturation = 0.0;
184 else
186 if (brightness <= 0.5)
187 saturation = (max - min) / (max + min);
188 else
189 saturation = (max - min) / (2.0 - max - min);
191 delta = max -min;
193 if (red == max)
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;
200 hue *= 60.0;
201 if (hue < 0.0)
202 hue += 360.0;
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 ();
219 return result;
221 } // namespace Prolooks