auesnthaeou
[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
32 #include "conky.h"
33 #include "core.h"
34 #include "logging.h"
35 #ifdef X11
36 #include "x11.h"
37 #endif
38
39 /* precalculated: 31/255, and 63/255 */
40 #define CONST_8_TO_5_BITS 0.12156862745098
41 #define CONST_8_TO_6_BITS 0.247058823529412
42
43 static void set_up_gradient(conky_context *ctx)
44 {
45         int i;
46 #ifdef X11
47         if (ctx->output_methods & TO_X) {
48                 ctx->colour_depth = DisplayPlanes(display, screen);
49         } else
50 #endif /* X11 */
51         {
52                 ctx->colour_depth = 16;
53         }
54         if (ctx->colour_depth != 24 && ctx->colour_depth != 16) {
55                 NORM_ERR("using non-standard colour depth, gradients may look like a "
56                         "lollipop");
57         }
58
59         ctx->redmask = 0;
60         ctx->greenmask = 0;
61         ctx->bluemask = 0;
62         for (i = (ctx->colour_depth / 3) - 1; i >= 0; i--) {
63                 ctx->redmask |= 1 << i;
64                 ctx->greenmask |= 1 << i;
65                 ctx->bluemask |= 1 << i;
66         }
67         if (ctx->colour_depth % 3 == 1) {
68                 ctx->greenmask |= 1 << (ctx->colour_depth / 3);
69         }
70         ctx->redmask = ctx->redmask << (2 * ctx->colour_depth / 3 + ctx->colour_depth % 3);
71         ctx->greenmask = ctx->greenmask << (ctx->colour_depth / 3);
72 }
73
74 /* adjust colour values depending on colour depth */
75 unsigned int adjust_colours(conky_context *ctx, unsigned int colour)
76 {
77         double r, g, b;
78
79         if (ctx->colour_depth == 0) {
80                 set_up_gradient(ctx);
81         }
82         if (ctx->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(conky_context *ctx, 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 * ctx->colour_depth / 3 + ctx->colour_depth % 3);
100         short greenshift = (ctx->colour_depth / 3);
101         unsigned long *colours = malloc(width * sizeof(unsigned long));
102         int i;
103
104         if (ctx->colour_depth == 0) {
105                 set_up_gradient(ctx);
106         }
107         red1 = (first_colour & ctx->redmask) >> redshift;
108         green1 = (first_colour & ctx->greenmask) >> greenshift;
109         blue1 = first_colour & ctx->bluemask;
110         red2 = (last_colour & ctx->redmask) >> redshift;
111         green2 = (last_colour & ctx->greenmask) >> greenshift;
112         blue2 = last_colour & ctx->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 + 1) / width);
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 > ctx->bluemask) {
153                         red3 = ctx->bluemask;
154                 }
155                 if (green3 > ctx->bluemask) {
156                         green3 = ctx->bluemask;
157                 }
158                 if (blue3 > ctx->bluemask) {
159                         blue3 = ctx->bluemask;
160                 }
161                 colours[i] = (red3 << redshift) | (green3 << greenshift) | blue3;
162         }
163         return colours;
164 }
165