1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
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.
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/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
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
43 static void set_up_gradient(conky_context *ctx)
47 if (ctx->output_methods & TO_X) {
48 ctx->colour_depth = DisplayPlanes(display, screen);
52 ctx->colour_depth = 16;
54 if (ctx->colour_depth != 24 && ctx->colour_depth != 16) {
55 NORM_ERR("using non-standard colour depth, gradients may look like a "
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;
67 if (ctx->colour_depth % 3 == 1) {
68 ctx->greenmask |= 1 << (ctx->colour_depth / 3);
70 ctx->redmask = ctx->redmask << (2 * ctx->colour_depth / 3 + ctx->colour_depth % 3);
71 ctx->greenmask = ctx->greenmask << (ctx->colour_depth / 3);
74 /* adjust colour values depending on colour depth */
75 unsigned int adjust_colours(conky_context *ctx, unsigned int colour)
79 if (ctx->colour_depth == 0) {
82 if (ctx->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(conky_context *ctx, 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 * ctx->colour_depth / 3 + ctx->colour_depth % 3);
100 short greenshift = (ctx->colour_depth / 3);
101 unsigned long *colours = malloc(width * sizeof(unsigned long));
104 if (ctx->colour_depth == 0) {
105 set_up_gradient(ctx);
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);
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 + 1) / width);
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 > ctx->bluemask) {
153 red3 = ctx->bluemask;
155 if (green3 > ctx->bluemask) {
156 green3 = ctx->bluemask;
158 if (blue3 > ctx->bluemask) {
159 blue3 = ctx->bluemask;
161 colours[i] = (red3 << redshift) | (green3 << greenshift) | blue3;