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