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