]> Dogcows Code - chaz/tint2/blob - src/util/common.c
*fix* more systray modifications for nice looking icons in real transparency mode
[chaz/tint2] / src / util / common.c
1 /**************************************************************************
2 *
3 * Tint2 : common windows function
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
20
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28
29 #include "common.h"
30
31
32
33 void copy_file(const char *pathSrc, const char *pathDest)
34 {
35 FILE *fileSrc, *fileDest;
36 char line[100];
37 int nb;
38
39 fileSrc = fopen(pathSrc, "rb");
40 if (fileSrc == NULL) return;
41
42 fileDest = fopen(pathDest, "wb");
43 if (fileDest == NULL) return;
44
45 while ((nb = fread(line, 1, 100, fileSrc)) > 0) fwrite(line, 1, nb, fileDest);
46
47 fclose (fileDest);
48 fclose (fileSrc);
49 }
50
51
52 int parse_line (const char *line, char **key, char **value)
53 {
54 char *a, *b;
55
56 /* Skip useless lines */
57 if ((line[0] == '#') || (line[0] == '\n')) return 0;
58 if (!(a = strchr (line, '='))) return 0;
59
60 /* overwrite '=' with '\0' */
61 a[0] = '\0';
62 *key = strdup (line);
63 a++;
64
65 /* overwrite '\n' with '\0' if '\n' present */
66 if ((b = strchr (a, '\n'))) b[0] = '\0';
67
68 *value = strdup (a);
69
70 g_strstrip(*key);
71 g_strstrip(*value);
72 return 1;
73 }
74
75
76 int hex_char_to_int (char c)
77 {
78 int r;
79
80 if (c >= '0' && c <= '9') r = c - '0';
81 else if (c >= 'a' && c <= 'f') r = c - 'a' + 10;
82 else if (c >= 'A' && c <= 'F') r = c - 'A' + 10;
83 else r = 0;
84
85 return r;
86 }
87
88
89 int hex_to_rgb (char *hex, int *r, int *g, int *b)
90 {
91 int len;
92
93 if (hex == NULL || hex[0] != '#') return (0);
94
95 len = strlen (hex);
96 if (len == 3 + 1) {
97 *r = hex_char_to_int (hex[1]);
98 *g = hex_char_to_int (hex[2]);
99 *b = hex_char_to_int (hex[3]);
100 }
101 else if (len == 6 + 1) {
102 *r = hex_char_to_int (hex[1]) * 16 + hex_char_to_int (hex[2]);
103 *g = hex_char_to_int (hex[3]) * 16 + hex_char_to_int (hex[4]);
104 *b = hex_char_to_int (hex[5]) * 16 + hex_char_to_int (hex[6]);
105 }
106 else if (len == 12 + 1) {
107 *r = hex_char_to_int (hex[1]) * 16 + hex_char_to_int (hex[2]);
108 *g = hex_char_to_int (hex[5]) * 16 + hex_char_to_int (hex[6]);
109 *b = hex_char_to_int (hex[9]) * 16 + hex_char_to_int (hex[10]);
110 }
111 else return 0;
112
113 return 1;
114 }
115
116
117 void get_color (char *hex, double *rgb)
118 {
119 int r, g, b;
120 hex_to_rgb (hex, &r, &g, &b);
121
122 rgb[0] = (r / 255.0);
123 rgb[1] = (g / 255.0);
124 rgb[2] = (b / 255.0);
125 }
126
127
128 void adjust_asb(DATA32 *data, int w, int h, int alpha, float satur, float bright)
129 {
130 unsigned int x, y;
131 unsigned int a, r, g, b, argb;
132 unsigned long id;
133 int cmax, cmin;
134 float h2, f, p, q, t;
135 float hue, saturation, brightness;
136 float redc, greenc, bluec;
137
138 for(y = 0; y < h; y++) {
139 for(id = y * w, x = 0; x < w; x++, id++) {
140 argb = data[id];
141 a = (argb >> 24) & 0xff;
142 // transparent => nothing to do.
143 if (a == 0) continue;
144 r = (argb >> 16) & 0xff;
145 g = (argb >> 8) & 0xff;
146 b = (argb) & 0xff;
147
148 // convert RGB to HSB
149 cmax = (r > g) ? r : g;
150 if (b > cmax) cmax = b;
151 cmin = (r < g) ? r : g;
152 if (b < cmin) cmin = b;
153 brightness = ((float)cmax) / 255.0f;
154 if (cmax != 0)
155 saturation = ((float)(cmax - cmin)) / ((float)cmax);
156 else
157 saturation = 0;
158 if (saturation == 0)
159 hue = 0;
160 else {
161 redc = ((float)(cmax - r)) / ((float)(cmax - cmin));
162 greenc = ((float)(cmax - g)) / ((float)(cmax - cmin));
163 bluec = ((float)(cmax - b)) / ((float)(cmax - cmin));
164 if (r == cmax)
165 hue = bluec - greenc;
166 else if (g == cmax)
167 hue = 2.0f + redc - bluec;
168 else
169 hue = 4.0f + greenc - redc;
170 hue = hue / 6.0f;
171 if (hue < 0)
172 hue = hue + 1.0f;
173 }
174
175 // adjust
176 saturation += satur;
177 if (saturation < 0.0) saturation = 0.0;
178 if (saturation > 1.0) saturation = 1.0;
179 brightness += bright;
180 if (brightness < 0.0) brightness = 0.0;
181 if (brightness > 1.0) brightness = 1.0;
182 if (alpha != 100)
183 a = (a * alpha)/100;
184
185 // convert HSB to RGB
186 if (saturation == 0) {
187 r = g = b = (int)(brightness * 255.0f + 0.5f);
188 } else {
189 h2 = (hue - (int)hue) * 6.0f;
190 f = h2 - (int)(h2);
191 p = brightness * (1.0f - saturation);
192 q = brightness * (1.0f - saturation * f);
193 t = brightness * (1.0f - (saturation * (1.0f - f)));
194 switch ((int) h2) {
195 case 0:
196 r = (int)(brightness * 255.0f + 0.5f);
197 g = (int)(t * 255.0f + 0.5f);
198 b = (int)(p * 255.0f + 0.5f);
199 break;
200 case 1:
201 r = (int)(q * 255.0f + 0.5f);
202 g = (int)(brightness * 255.0f + 0.5f);
203 b = (int)(p * 255.0f + 0.5f);
204 break;
205 case 2:
206 r = (int)(p * 255.0f + 0.5f);
207 g = (int)(brightness * 255.0f + 0.5f);
208 b = (int)(t * 255.0f + 0.5f);
209 break;
210 case 3:
211 r = (int)(p * 255.0f + 0.5f);
212 g = (int)(q * 255.0f + 0.5f);
213 b = (int)(brightness * 255.0f + 0.5f);
214 break;
215 case 4:
216 r = (int)(t * 255.0f + 0.5f);
217 g = (int)(p * 255.0f + 0.5f);
218 b = (int)(brightness * 255.0f + 0.5f);
219 break;
220 case 5:
221 r = (int)(brightness * 255.0f + 0.5f);
222 g = (int)(p * 255.0f + 0.5f);
223 b = (int)(q * 255.0f + 0.5f);
224 break;
225 }
226 }
227
228 argb = a;
229 argb = (argb << 8) + r;
230 argb = (argb << 8) + g;
231 argb = (argb << 8) + b;
232 data[id] = argb;
233 }
234 }
235 }
236
237
238 void createHeuristicMask(DATA32* data, int w, int h)
239 {
240 unsigned char* udata = (unsigned char*)data;
241 int b = udata[0];
242 int g = udata[1];
243 int r = udata[2];
244 int i;
245 for (i=0; i<h*w; ++i) {
246 if ( abs(b-*udata)<5 && abs(g-*(udata+1))<5 && abs(r-*(udata+2))<5 ) {
247 *(udata+3) = 0;
248 }
249 udata += 4;
250 }
251 }
This page took 0.050745 seconds and 5 git commands to generate.