Fixed gradients to actually use both end colors
[monky] / src / colours.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 #include "conky.h"
31 #include "logging.h"
32 #ifdef X11
33 #include "x11.h"
34 #endif
35
36 /* precalculated: 31/255, and 63/255 */
37 #define CONST_8_TO_5_BITS 0.12156862745098
38 #define CONST_8_TO_6_BITS 0.247058823529412
39
40 static short colour_depth = 0;
41 static long redmask, greenmask, bluemask;
42
43 static void set_up_gradient(void)
44 {
45         int i;
46 #ifdef X11
47         if (output_methods & TO_X) {
48                 colour_depth = DisplayPlanes(display, screen);
49         } else
50 #endif /* X11 */
51         {
52                 colour_depth = 16;
53         }
54         if (colour_depth != 24 && colour_depth != 16) {
55                 NORM_ERR("using non-standard colour depth, gradients may look like a "
56                         "lolly-pop");
57         }
58
59         redmask = 0;
60         greenmask = 0;
61         bluemask = 0;
62         for (i = (colour_depth / 3) - 1; i >= 0; i--) {
63                 redmask |= 1 << i;
64                 greenmask |= 1 << i;
65                 bluemask |= 1 << i;
66         }
67         if (colour_depth % 3 == 1) {
68                 greenmask |= 1 << (colour_depth / 3);
69         }
70         redmask = redmask << (2 * colour_depth / 3 + colour_depth % 3);
71         greenmask = greenmask << (colour_depth / 3);
72 }
73
74 /* adjust colour values depending on colour depth */
75 unsigned int adjust_colours(unsigned int colour)
76 {
77         double r, g, b;
78
79         if (colour_depth == 0) {
80                 set_up_gradient();
81         }
82         if (colour_depth == 16) {
83                 r = (colour & 0xff0000) >> 16;
84                 g = (colour & 0xff00) >> 8;
85                 b =  colour & 0xff;
86                 colour  = (int) (r * CONST_8_TO_5_BITS) << 11;
87                 colour |= (int) (g * CONST_8_TO_6_BITS) << 5;
88                 colour |= (int) (b * CONST_8_TO_5_BITS);
89         }
90         return colour;
91 }
92
93 /* this function returns the next colour between two colours for a gradient */
94 unsigned long *do_gradient(int width, unsigned long first_colour, unsigned long last_colour)
95 {
96         int red1, green1, blue1;                                // first colour
97         int red2, green2, blue2;                                // last colour
98         int reddiff, greendiff, bluediff;               // difference
99         short redshift = (2 * colour_depth / 3 + colour_depth % 3);
100         short greenshift = (colour_depth / 3);
101         unsigned long *colours = malloc(width * sizeof(unsigned long));
102         int i;
103
104         if (colour_depth == 0) {
105                 set_up_gradient();
106         }
107         red1 = (first_colour & redmask) >> redshift;
108         green1 = (first_colour & greenmask) >> greenshift;
109         blue1 = first_colour & bluemask;
110         red2 = (last_colour & redmask) >> redshift;
111         green2 = (last_colour & greenmask) >> greenshift;
112         blue2 = last_colour & bluemask;
113         reddiff = abs(red1 - red2);
114         greendiff = abs(green1 - green2);
115         bluediff = abs(blue1 - blue2);
116 #ifdef HAVE_OPENMP
117 #pragma omp parallel for schedule(dynamic,10) shared(colours)
118 #endif /* HAVE_OPENMP */
119         for (i = 0; i < width; i++) {
120                 int red3 = 0, green3 = 0, blue3 = 0;    // colour components
121
122                 float factor = ((float) i / (width - 1));
123
124                 /* the '+ 0.5' bit rounds our floats to ints properly */
125                 if (red1 >= red2) {
126                         red3 = -(factor * reddiff) - 0.5;
127                 } else if (red1 < red2) {
128                         red3 = factor * reddiff + 0.5;
129                 }
130                 if (green1 >= green2) {
131                         green3 = -(factor * greendiff) - 0.5;
132                 } else if (green1 < green2) {
133                         green3 = factor * greendiff + 0.5;
134                 }
135                 if (blue1 >= blue2) {
136                         blue3 = -(factor * bluediff) - 0.5;
137                 } else if (blue1 < blue2) {
138                         blue3 = factor * bluediff + 0.5;
139                 }
140                 red3 += red1;
141                 green3 += green1;
142                 blue3 += blue1;
143                 if (red3 < 0) {
144                         red3 = 0;
145                 }
146                 if (green3 < 0) {
147                         green3 = 0;
148                 }
149                 if (blue3 < 0) {
150                         blue3 = 0;
151                 }
152                 if (red3 > bluemask) {
153                         red3 = bluemask;
154                 }
155                 if (green3 > bluemask) {
156                         green3 = bluemask;
157                 }
158                 if (blue3 > bluemask) {
159                         blue3 = bluemask;
160                 }
161                 colours[i] = (red3 << redshift) | (green3 << greenshift) | blue3;
162         }
163         return colours;
164 }
165
166 #ifdef X11
167 long get_x11_color(const char *name)
168 {
169         XColor color;
170
171         color.pixel = 0;
172         if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
173                 /* lets check if it's a hex colour with the # missing in front
174                  * if yes, then do something about it */
175                 char newname[DEFAULT_TEXT_BUFFER_SIZE];
176
177                 newname[0] = '#';
178                 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
179                 /* now lets try again */
180                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
181                                         &color)) {
182                         NORM_ERR("can't parse X color '%s'", name);
183                         return 0xFF00FF;
184                 }
185         }
186         if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
187                 NORM_ERR("can't allocate X color '%s'", name);
188         }
189
190         return (long) color.pixel;
191 }
192 #endif