X-Git-Url: https://git.octo.it/?p=collection4.git;a=blobdiff_plain;f=src%2Fcommon.c;h=2a5bf45262a565943c5d6ec26b4686afee0e86c7;hp=d6047c43ca75f64578c9fc60c4c3c06654be3af1;hb=dada106cc5e266710e0238046245349e595201a3;hpb=04aa292008516f3add56b9bd87ce9c57b0366cc1 diff --git a/src/common.c b/src/common.c index d6047c4..2a5bf45 100644 --- a/src/common.c +++ b/src/common.c @@ -1,3 +1,26 @@ +/** + * collection4 - common.c + * Copyright (C) 2010 Florian octo Forster + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + #include #include #include @@ -16,6 +39,7 @@ #include "common.h" #include "graph_list.h" +#include "utils_cgi.h" #include #include @@ -159,6 +183,23 @@ static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */ | ((uint32_t) b)); } /* }}} uint32_t rgb_to_uint32 */ +static int uint32_to_rgb (uint32_t color, double *rgb) /* {{{ */ +{ + uint8_t r; + uint8_t g; + uint8_t b; + + r = (uint8_t) ((color >> 16) & 0x00ff); + g = (uint8_t) ((color >> 8) & 0x00ff); + b = (uint8_t) ((color >> 0) & 0x00ff); + + rgb[0] = ((double) r) / 255.0; + rgb[1] = ((double) g) / 255.0; + rgb[2] = ((double) b) / 255.0; + + return (0); +} /* }}} int uint32_to_rgb */ + uint32_t get_random_color (void) /* {{{ */ { double hsv[3] = { 0.0, 1.0, 1.0 }; @@ -171,6 +212,18 @@ uint32_t get_random_color (void) /* {{{ */ return (rgb_to_uint32 (rgb)); } /* }}} uint32_t get_random_color */ +uint32_t fade_color (uint32_t color) /* {{{ */ +{ + double rgb[3]; + + uint32_to_rgb (color, rgb); + rgb[0] = 1.0 - ((1.0 - rgb[0]) * 0.1); + rgb[1] = 1.0 - ((1.0 - rgb[1]) * 0.1); + rgb[2] = 1.0 - ((1.0 - rgb[2]) * 0.1); + + return (rgb_to_uint32 (rgb)); +} /* }}} uint32_t fade_color */ + int print_debug (const char *format, ...) /* {{{ */ { static _Bool have_header = 0; @@ -204,12 +257,86 @@ char *strtolower (char *str) /* {{{ */ return (str); } /* }}} char *strtolower */ -char *strtolower_copy (const char *str) +char *strtolower_copy (const char *str) /* {{{ */ { if (str == NULL) return (NULL); return (strtolower (strdup (str))); -} +} /* }}} char *strtolower_copy */ + +int get_time_args (long *ret_begin, long *ret_end, /* {{{ */ + long *ret_now) +{ + const char *begin_str; + const char *end_str; + long now; + long begin; + long end; + char *endptr; + long tmp; + + if ((ret_begin == NULL) || (ret_end == NULL)) + return (EINVAL); + + begin_str = param ("begin"); + end_str = param ("end"); + + now = (long) time (NULL); + if (ret_now != NULL) + *ret_now = now; + *ret_begin = now - 86400; + *ret_end = now; + + if (begin_str != NULL) + { + endptr = NULL; + errno = 0; + tmp = strtol (begin_str, &endptr, /* base = */ 0); + if ((endptr == begin_str) || (errno != 0)) + return (-1); + if (tmp <= 0) + begin = now + tmp; + else + begin = tmp; + } + else /* if (begin_str == NULL) */ + { + begin = now - 86400; + } + + if (end_str != NULL) + { + endptr = NULL; + errno = 0; + tmp = strtol (end_str, &endptr, /* base = */ 0); + if ((endptr == end_str) || (errno != 0)) + return (-1); + end = tmp; + if (tmp <= 0) + end = now + tmp; + else + end = tmp; + } + else /* if (end_str == NULL) */ + { + end = now; + } + + if (begin == end) + return (-1); + + if (begin > end) + { + tmp = begin; + begin = end; + end = tmp; + } + + *ret_begin = begin; + *ret_end = end; + + return (0); +} /* }}} int get_time_args */ /* vim: set sw=2 sts=2 et fdm=marker : */