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
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
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.
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/>.
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
40 static short colour_depth = 0;
41 static long redmask, greenmask, bluemask;
43 static void set_up_gradient(void)
47 if (output_methods & TO_X) {
48 colour_depth = DisplayPlanes(display, screen);
54 if (colour_depth != 24 && colour_depth != 16) {
55 NORM_ERR("using non-standard colour depth, gradients may look like a "
62 for (i = (colour_depth / 3) - 1; i >= 0; i--) {
67 if (colour_depth % 3 == 1) {
68 greenmask |= 1 << (colour_depth / 3);
70 redmask = redmask << (2 * colour_depth / 3 + colour_depth % 3);
71 greenmask = greenmask << (colour_depth / 3);
74 /* adjust colour values depending on colour depth */
75 unsigned int adjust_colours(unsigned int colour)
79 if (colour_depth == 0) {
82 if (colour_depth == 16) {
83 r = (colour & 0xff0000) >> 16;
84 g = (colour & 0xff00) >> 8;
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);
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)
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));
104 if (colour_depth == 0) {
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);
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
122 float factor = ((float) i / (width - 1));
124 /* the '+ 0.5' bit rounds our floats to ints properly */
126 red3 = -(factor * reddiff) - 0.5;
127 } else if (red1 < red2) {
128 red3 = factor * reddiff + 0.5;
130 if (green1 >= green2) {
131 green3 = -(factor * greendiff) - 0.5;
132 } else if (green1 < green2) {
133 green3 = factor * greendiff + 0.5;
135 if (blue1 >= blue2) {
136 blue3 = -(factor * bluediff) - 0.5;
137 } else if (blue1 < blue2) {
138 blue3 = factor * bluediff + 0.5;
152 if (red3 > bluemask) {
155 if (green3 > bluemask) {
158 if (blue3 > bluemask) {
161 colours[i] = (red3 << redshift) | (green3 << greenshift) | blue3;
167 long get_x11_color(const char *name)
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];
178 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
179 /* now lets try again */
180 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
182 NORM_ERR("can't parse X color '%s'", name);
186 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
187 NORM_ERR("can't allocate X color '%s'", name);
190 return (long) color.pixel;