X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=common.c;h=15fbe6a3185ce2703c1da8bcc05c9005b5a7544f;hb=4568880c50d790912fb4823d0ae785e95f65cad2;hp=5d2a67deaef7276cb0e86c6c4da8ce9b51dec13f;hpb=0bdac0286e8d3c42dd8d5c3058d822e9ad0e6313;p=collection4.git diff --git a/common.c b/common.c index 5d2a67d..15fbe6a 100644 --- a/common.c +++ b/common.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include #include @@ -7,12 +9,16 @@ #include #include #include +#include #include #include "common.h" #include "graph_list.h" +#include +#include + static int foreach_rrd_file (const char *dir, /* {{{ */ int (*callback) (const char *, void *), void *user_data) @@ -245,4 +251,76 @@ int ds_list_from_rrd_file (char *file, /* {{{ */ return (0); } /* }}} int ds_list_from_rrd_file */ +static int hsv_to_rgb (double *hsv, double *rgb) /* {{{ */ +{ + double c = hsv[2] * hsv[1]; + double h = hsv[0] / 60.0; + double x = c * (1.0 - fabs (fmod (h, 2.0) - 1)); + double m = hsv[2] - c; + + rgb[0] = 0.0; + rgb[1] = 0.0; + rgb[2] = 0.0; + + if ((0.0 <= h) && (h < 1.0)) { rgb[0] = 1.0; rgb[1] = x; rgb[2] = 0.0; } + else if ((1.0 <= h) && (h < 2.0)) { rgb[0] = x; rgb[1] = 1.0; rgb[2] = 0.0; } + else if ((2.0 <= h) && (h < 3.0)) { rgb[0] = 0.0; rgb[1] = 1.0; rgb[2] = x; } + else if ((3.0 <= h) && (h < 4.0)) { rgb[0] = 0.0; rgb[1] = x; rgb[2] = 1.0; } + else if ((4.0 <= h) && (h < 5.0)) { rgb[0] = x; rgb[1] = 0.0; rgb[2] = 1.0; } + else if ((5.0 <= h) && (h < 6.0)) { rgb[0] = 1.0; rgb[1] = 0.0; rgb[2] = x; } + + rgb[0] += m; + rgb[1] += m; + rgb[2] += m; + + return (0); +} /* }}} int hsv_to_rgb */ + +static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */ +{ + uint8_t r; + uint8_t g; + uint8_t b; + + r = (uint8_t) (255.0 * rgb[0]); + g = (uint8_t) (255.0 * rgb[1]); + b = (uint8_t) (255.0 * rgb[2]); + + return ((((uint32_t) r) << 16) + | (((uint32_t) g) << 8) + | ((uint32_t) b)); +} /* }}} uint32_t rgb_to_uint32 */ + +uint32_t get_random_color (void) /* {{{ */ +{ + double hsv[3] = { 0.0, 1.0, 1.0 }; + double rgb[3] = { 0.0, 0.0, 0.0 }; + + hsv[0] = 360.0 * ((double) rand ()) / (((double) RAND_MAX) + 1.0); + + hsv_to_rgb (hsv, rgb); + + return (rgb_to_uint32 (rgb)); +} /* }}} uint32_t get_random_color */ + +int print_debug (const char *format, ...) /* {{{ */ +{ + static _Bool have_header = 0; + + va_list ap; + int status; + + if (!have_header) + { + printf ("Content-Type: text/plain\n\n"); + have_header = 1; + } + + va_start (ap, format); + status = vprintf (format, ap); + va_end (ap); + + return (status); +} /* }}} int print_debug */ + /* vim: set sw=2 sts=2 et fdm=marker : */