]>
Dogcows Code - chaz/tint2/blob - src/util/common.c
1 /**************************************************************************
3 * Tint2 : common windows function
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
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.
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 **************************************************************************/
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
33 void copy_file(const char *pathSrc
, const char *pathDest
)
35 FILE *fileSrc
, *fileDest
;
39 fileSrc
= fopen(pathSrc
, "rb");
40 if (fileSrc
== NULL
) return;
42 fileDest
= fopen(pathDest
, "wb");
43 if (fileDest
== NULL
) return;
45 while ((nb
= fread(line
, 1, 100, fileSrc
)) > 0) fwrite(line
, 1, nb
, fileDest
);
52 int parse_line (const char *line
, char **key
, char **value
)
56 /* Skip useless lines */
57 if ((line
[0] == '#') || (line
[0] == '\n')) return 0;
58 if (!(a
= strchr (line
, '='))) return 0;
60 /* overwrite '=' with '\0' */
65 /* overwrite '\n' with '\0' if '\n' present */
66 if ((b
= strchr (a
, '\n'))) b
[0] = '\0';
76 int hex_char_to_int (char c
)
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;
89 int hex_to_rgb (char *hex
, int *r
, int *g
, int *b
)
93 if (hex
== NULL
|| hex
[0] != '#') return (0);
97 *r
= hex_char_to_int (hex
[1]);
98 *g
= hex_char_to_int (hex
[2]);
99 *b
= hex_char_to_int (hex
[3]);
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]);
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]);
117 void get_color (char *hex
, double *rgb
)
120 hex_to_rgb (hex
, &r
, &g
, &b
);
122 rgb
[0] = (r
/ 255.0);
123 rgb
[1] = (g
/ 255.0);
124 rgb
[2] = (b
/ 255.0);
128 void adjust_asb(DATA32
*data
, int w
, int h
, int alpha
, float satur
, float bright
)
131 unsigned int a
, r
, g
, b
, argb
;
134 float h2
, f
, p
, q
, t
;
135 float hue
, saturation
, brightness
;
136 float redc
, greenc
, bluec
;
138 for(y
= 0; y
< h
; y
++) {
139 for(id
= y
* w
, x
= 0; x
< w
; x
++, 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;
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
;
155 saturation
= ((float)(cmax
- cmin
)) / ((float)cmax
);
161 redc
= ((float)(cmax
- r
)) / ((float)(cmax
- cmin
));
162 greenc
= ((float)(cmax
- g
)) / ((float)(cmax
- cmin
));
163 bluec
= ((float)(cmax
- b
)) / ((float)(cmax
- cmin
));
165 hue
= bluec
- greenc
;
167 hue
= 2.0f
+ redc
- bluec
;
169 hue
= 4.0f
+ greenc
- redc
;
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;
185 // convert HSB to RGB
186 if (saturation
== 0) {
187 r
= g
= b
= (int)(brightness
* 255.0f
+ 0.5f
);
189 h2
= (hue
- (int)hue
) * 6.0f
;
191 p
= brightness
* (1.0f
- saturation
);
192 q
= brightness
* (1.0f
- saturation
* f
);
193 t
= brightness
* (1.0f
- (saturation
* (1.0f
- f
)));
196 r
= (int)(brightness
* 255.0f
+ 0.5f
);
197 g
= (int)(t
* 255.0f
+ 0.5f
);
198 b
= (int)(p
* 255.0f
+ 0.5f
);
201 r
= (int)(q
* 255.0f
+ 0.5f
);
202 g
= (int)(brightness
* 255.0f
+ 0.5f
);
203 b
= (int)(p
* 255.0f
+ 0.5f
);
206 r
= (int)(p
* 255.0f
+ 0.5f
);
207 g
= (int)(brightness
* 255.0f
+ 0.5f
);
208 b
= (int)(t
* 255.0f
+ 0.5f
);
211 r
= (int)(p
* 255.0f
+ 0.5f
);
212 g
= (int)(q
* 255.0f
+ 0.5f
);
213 b
= (int)(brightness
* 255.0f
+ 0.5f
);
216 r
= (int)(t
* 255.0f
+ 0.5f
);
217 g
= (int)(p
* 255.0f
+ 0.5f
);
218 b
= (int)(brightness
* 255.0f
+ 0.5f
);
221 r
= (int)(brightness
* 255.0f
+ 0.5f
);
222 g
= (int)(p
* 255.0f
+ 0.5f
);
223 b
= (int)(q
* 255.0f
+ 0.5f
);
229 argb
= (argb
<< 8) + r
;
230 argb
= (argb
<< 8) + g
;
231 argb
= (argb
<< 8) + b
;
This page took 0.041609 seconds and 4 git commands to generate.