X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fcolours.c;h=f839f2f7dc0034dbf125da1d04cda6080c17bd7d;hb=f607145f13fab35193defb1c5fab308796a429ef;hp=a50f84071d74a7498d3db2f9428605816e019293;hpb=f05829ad9a32082fa80c43a921471716ce76d22b;p=monky diff --git a/src/colours.c b/src/colours.c index a50f840..f839f2f 100644 --- a/src/colours.c +++ b/src/colours.c @@ -1,4 +1,7 @@ -/* Conky, a system monitor, based on torsmo +/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- + * vim: ts=4 sw=4 noet ai cindent syntax=c + * + * Conky, a system monitor, based on torsmo * * Any original torsmo code is licensed under the BSD license * @@ -7,7 +10,7 @@ * Please see COPYING for details * * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen - * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al. + * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al. * (see AUTHORS) * All rights reserved. * @@ -49,7 +52,7 @@ static void set_up_gradient(void) colour_depth = 16; } if (colour_depth != 24 && colour_depth != 16) { - ERR("using non-standard colour depth, gradients may look like a " + NORM_ERR("using non-standard colour depth, gradients may look like a " "lolly-pop"); } @@ -68,8 +71,8 @@ static void set_up_gradient(void) greenmask = greenmask << (colour_depth / 3); } -/* adjust color values depending on color depth */ -unsigned int adjust_colors(unsigned int color) +/* adjust colour values depending on colour depth */ +unsigned int adjust_colours(unsigned int colour) { double r, g, b; @@ -77,109 +80,113 @@ unsigned int adjust_colors(unsigned int color) set_up_gradient(); } if (colour_depth == 16) { - r = (color & 0xff0000) >> 16; - g = (color & 0xff00) >> 8; - b = color & 0xff; - color = (int) (r * CONST_8_TO_5_BITS) << 11; - color |= (int) (g * CONST_8_TO_6_BITS) << 5; - color |= (int) (b * CONST_8_TO_5_BITS); - } - return color; + r = (colour & 0xff0000) >> 16; + g = (colour & 0xff00) >> 8; + b = colour & 0xff; + colour = (int) (r * CONST_8_TO_5_BITS) << 11; + colour |= (int) (g * CONST_8_TO_6_BITS) << 5; + colour |= (int) (b * CONST_8_TO_5_BITS); + } + return colour; } /* this function returns the next colour between two colours for a gradient */ -unsigned long do_gradient(unsigned long first_colour, - unsigned long last_colour) +unsigned long *do_gradient(int width, unsigned long first_colour, unsigned long last_colour) { - int tmp_color = 0; int red1, green1, blue1; // first colour - int red2, green2, blue2; // second colour - int red3 = 0, green3 = 0, blue3 = 0; // difference + int red2, green2, blue2; // last colour + int reddiff, greendiff, bluediff; // difference short redshift = (2 * colour_depth / 3 + colour_depth % 3); short greenshift = (colour_depth / 3); + unsigned long *colours = malloc(width * sizeof(unsigned long)); + int i; + if (colour_depth == 0) { + set_up_gradient(); + } red1 = (first_colour & redmask) >> redshift; green1 = (first_colour & greenmask) >> greenshift; blue1 = first_colour & bluemask; red2 = (last_colour & redmask) >> redshift; green2 = (last_colour & greenmask) >> greenshift; blue2 = last_colour & bluemask; - if (red1 > red2) { - red3 = -1; - } - if (red1 < red2) { - red3 = 1; - } - if (green1 > green2) { - green3 = -1; - } - if (green1 < green2) { - green3 = 1; - } - if (blue1 > blue2) { - blue3 = -1; - } - if (blue1 < blue2) { - blue3 = 1; - } - red1 += red3; - green1 += green3; - blue1 += blue3; - if (red1 < 0) { - red1 = 0; - } - if (green1 < 0) { - green1 = 0; - } - if (blue1 < 0) { - blue1 = 0; - } - if (red1 > bluemask) { - red1 = bluemask; - } - if (green1 > bluemask) { - green1 = bluemask; - } - if (blue1 > bluemask) { - blue1 = bluemask; - } - tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1; - return tmp_color; + reddiff = abs(red1 - red2); + greendiff = abs(green1 - green2); + bluediff = abs(blue1 - blue2); +#ifdef HAVE_OPENMP +#pragma omp parallel for schedule(dynamic,10) shared(colours) +#endif /* HAVE_OPENMP */ + for (i = 0; i < width; i++) { + int red3 = 0, green3 = 0, blue3 = 0; // colour components + + float factor = ((float) i / (width - 1)); + + /* the '+ 0.5' bit rounds our floats to ints properly */ + if (red1 >= red2) { + red3 = -(factor * reddiff) - 0.5; + } else if (red1 < red2) { + red3 = factor * reddiff + 0.5; + } + if (green1 >= green2) { + green3 = -(factor * greendiff) - 0.5; + } else if (green1 < green2) { + green3 = factor * greendiff + 0.5; + } + if (blue1 >= blue2) { + blue3 = -(factor * bluediff) - 0.5; + } else if (blue1 < blue2) { + blue3 = factor * bluediff + 0.5; + } + red3 += red1; + green3 += green1; + blue3 += blue1; + if (red3 < 0) { + red3 = 0; + } + if (green3 < 0) { + green3 = 0; + } + if (blue3 < 0) { + blue3 = 0; + } + if (red3 > bluemask) { + red3 = bluemask; + } + if (green3 > bluemask) { + green3 = bluemask; + } + if (blue3 > bluemask) { + blue3 = bluemask; + } + colours[i] = (red3 << redshift) | (green3 << greenshift) | blue3; + } + return colours; } -/* this function returns the max diff for a gradient */ -unsigned long gradient_max(unsigned long first_colour, - unsigned long last_colour) +#ifdef X11 +long get_x11_color(const char *name) { - int red1, green1, blue1; // first colour - int red2, green2, blue2; // second colour - int red3 = 0, green3 = 0, blue3 = 0; // difference - long redshift, greenshift; - int max; + XColor color; - if (colour_depth == 0) { - set_up_gradient(); - } - redshift = (2 * colour_depth / 3 + colour_depth % 3); - greenshift = (colour_depth / 3); - - red1 = (first_colour & redmask) >> redshift; - green1 = (first_colour & greenmask) >> greenshift; - blue1 = first_colour & bluemask; - red2 = (last_colour & redmask) >> redshift; - green2 = (last_colour & greenmask) >> greenshift; - blue2 = last_colour & bluemask; - red3 = abs(red1 - red2); - green3 = abs(green1 - green2); - blue3 = abs(blue1 - blue2); - max = red3; + color.pixel = 0; + if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) { + /* lets check if it's a hex colour with the # missing in front + * if yes, then do something about it */ + char newname[DEFAULT_TEXT_BUFFER_SIZE]; - if (green3 > max) { - max = green3; + newname[0] = '#'; + strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1); + /* now lets try again */ + if (!XParseColor(display, DefaultColormap(display, screen), &newname[0], + &color)) { + NORM_ERR("can't parse X color '%s'", name); + return 0xFF00FF; + } + } + if (!XAllocColor(display, DefaultColormap(display, screen), &color)) { + NORM_ERR("can't allocate X color '%s'", name); } - if (blue3 > max) { - max = blue3; - } - return max; -} + return (long) color.pixel; +} +#endif