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