1 /*
2  * asmail is an e-mail checker for X
3  *
4  * Copyright (c) 2002  Albert Dorofeev <albert@tigr.net>
5  *
6  * For the updates see http://www.tigr.net/afterstep/
7  *
8  * This software is distributed under GPL. For details see LICENSE file.
9  */
10 
11 #include <stdio.h>
12 
13 #include <X11/Xlib.h>
14 #include <X11/xpm.h>
15 
16 
17 /*
18  * Note: this function was originally taken out of ascd.
19  *
20  * It takes the given color, parses it in the context
21  * of the given window and returns a pixel of that color.
22  */
GetColor(char * ColorName,Display * disp,Window win)23 Pixel GetColor(char *ColorName, Display * disp, Window win)
24 {
25 	XColor Color;
26 	XWindowAttributes Attributes;
27 
28 	XGetWindowAttributes(disp, win, &Attributes);
29 	Color.pixel = 0;
30 
31 	if (!XParseColor(disp, Attributes.colormap, ColorName, &Color))
32 		printf("asmail: can't parse %s\n", ColorName);
33 	else if (!XAllocColor(disp, Attributes.colormap, &Color))
34 		printf("asmail: can't allocate %s\n", ColorName);
35 
36 	return Color.pixel;
37 }
38 
39 /*
40  * Performs the same actions as GetColor but
41  * returns the complete XColor structure
42  */
ParseColor(char * ColorName,Display * disp,Window win)43 XColor ParseColor(char *ColorName, Display * disp, Window win)
44 {
45 	XColor Color;
46 	XWindowAttributes Attributes;
47 
48 	XGetWindowAttributes(disp, win, &Attributes);
49 	Color.pixel = 0;
50 
51 	if (!XParseColor(disp, Attributes.colormap, ColorName, &Color))
52 		printf("asmail: can't parse %s\n", ColorName);
53 	else if (!XAllocColor(disp, Attributes.colormap, &Color))
54 		printf("asmail: can't allocate %s\n", ColorName);
55 
56 	return Color;
57 }
58 
59 static char tmp_char[50];
60 
61 /*
62  * darkens the given color using the supplied rate
63  */
DarkenCharColor(char * ColorName,float rate,Display * disp,Window win)64 char *DarkenCharColor(char *ColorName, float rate, Display * disp, Window win)
65 {
66 	XColor tmp_color;
67 #ifdef DEBUG
68 	printf("darkening %s ->", ColorName);
69 #endif
70 	tmp_color = ParseColor(ColorName, disp, win);
71 #ifdef DEBUG
72 	printf(" #%x %x %x ", tmp_color.red, tmp_color.green, tmp_color.blue);
73 #endif
74 	tmp_color.red = tmp_color.red / 257 / rate;
75 	tmp_color.green = tmp_color.green / 257 / rate;
76 	tmp_color.blue = tmp_color.blue / 257 / rate;
77 	sprintf(tmp_char, "#%.2x%.2x%.2x",
78 		(int) tmp_color.red,
79 		(int) tmp_color.green,
80 		(int) tmp_color.blue);
81 #ifdef DEBUG
82 	printf("-> %s\n", tmp_char);
83 #endif
84 	return tmp_char;
85 }
86 
87 /*
88  * darkens the given color using the supplied rate
89  */
DarkenColor(char * ColorName,float rate,Display * disp,Window win)90 Pixel DarkenColor(char *ColorName, float rate, Display * disp, Window win)
91 {
92 	return GetColor( DarkenCharColor(ColorName, rate, disp, win),
93 			       disp, win);
94 }
95 
96 /*
97  * brightens the given color using the supplied rate
98  */
LightenCharColor(char * ColorName,float rate,Display * disp,Window win)99 char *LightenCharColor(char *ColorName, float rate, Display * disp, Window win)
100 {
101 	XColor tmp_color;
102 #ifdef DEBUG
103 	printf("lightening %s ->", ColorName);
104 #endif
105 	tmp_color = ParseColor(ColorName, disp, win);
106 #ifdef DEBUG
107 	printf(" #%x %x %x ", tmp_color.red, tmp_color.green, tmp_color.blue);
108 #endif
109 	tmp_color.red = tmp_color.red / 257 * rate;
110 	tmp_color.green = tmp_color.green / 257 * rate;
111 	tmp_color.blue = tmp_color.blue / 257 * rate;
112 	if (tmp_color.red > 255)
113 		tmp_color.red = 255;
114 	if (tmp_color.green > 255)
115 		tmp_color.green = 255;
116 	if (tmp_color.blue > 255)
117 		tmp_color.blue = 255;
118 	sprintf(tmp_char, "#%.2x%.2x%.2x",
119 		(int) tmp_color.red,
120 		(int) tmp_color.green,
121 		(int) tmp_color.blue);
122 #ifdef DEBUG
123 	printf("-> %s\n", tmp_char);
124 #endif
125 	return tmp_char;
126 }
127 
128 /*
129  * brightens the given color using the supplied rate
130  */
LightenColor(char * ColorName,float rate,Display * disp,Window win)131 Pixel LightenColor(char *ColorName, float rate, Display * disp, Window win)
132 {
133 	return GetColor( LightenCharColor(ColorName, rate, disp, win),
134 			       disp, win);
135 }
136