Add description to the previously committed patch
[debian/pino.git] / src / color_utils.vala
1 using Gtk;
2
3 namespace ColorUtils {
4         
5         public string color2rgba(Gdk.Color color, double alpha = 0.40) { //alpha 0.0-1.0
6                 
7                 string alpha_string = alpha.to_string();
8                 switch(alpha_string) {
9                         case "0":
10                                 alpha_string = "0.00";
11                                 break;
12                         case "1":
13                                 alpha_string = "1.00";
14                                 break;
15                         default:
16                                 alpha_string = alpha.to_string().substring(0, 4);
17                                 break;
18                 }
19                 
20                 return "rgba(%d, %d, %d, %s)".printf(color.red/256, color.green/256,
21                         color.blue/256, alpha_string);
22         }
23         
24         public Gdk.Color rgba2color(string rgba, out uint16 alpha) {
25                 Gdk.Color color = Gdk.Color();
26                 
27                 Regex re = new Regex("rgba\\(([0-9]+), ([0-9]+), ([0-9]+), ([0-9]\\.[0-9][0-9])\\)");
28                 color.red = (uint16)re.replace(rgba, -1, 0, "\\1").to_int() * 256;
29                 color.green = (uint16)re.replace(rgba, -1, 0, "\\2").to_int() * 256;
30                 color.blue = (uint16)re.replace(rgba, -1, 0, "\\3").to_int() * 256;
31                 alpha = (uint16)(re.replace(rgba, -1, 0, "\\4").to_double() * 256.0 * 256.0);
32                 //warning("%d", alpha);
33                 return color;
34         }
35         
36         public string rgb_to_hex(Gdk.Color color) {
37                 string s = "%X%X%X".printf(
38                         (int)Math.trunc(color.red / 256.00),
39                         (int)Math.trunc(color.green / 256.00),
40                         (int)Math.trunc(color.blue / 256.00));
41                 return "#" + s;
42         }
43         
44         public uint16 lighter(uint16 color, uint16 delta) {
45                 if(color + delta > 255*256)
46                         return 255*256;
47                 else
48                         return color + delta; 
49         }
50         
51         public uint16 darker(uint16 color, uint16 delta) {
52                 if(color - delta < 0)
53                         return 0;
54                 else
55                         return color - delta; 
56         }
57 }