@cryptotaxi247 / netdata-1 / commits / 002023173

converted badges color map to list with hashes

Costa Tsaousis (ktsaou) committed Jul 11, 2018 at 04:04 UTC 002023173ec1aab38d79257c07ec94e008b61dc5
1 file changed +45 -14
src/web_buffer_svg.c
+45 -14
@@ -446,7 +446,7 @@ typedef enum badge_units_format {
446 } UNITS_FORMAT;
447
448
449 -struct units_formatter {
449 +static struct units_formatter {
450 const char *units;
451 uint32_t hash;
452 UNITS_FORMAT format;
@@ -618,20 +618,51 @@ inline char *format_value_and_unit(char *value_string, size_t value_string_len,
618 return format_value_with_precision_and_unit(value_string, value_string_len, value, units, precision);
619 }
620
621 +static struct badge_color {
622 + const char *name;
623 + uint32_t hash;
624 + const char *color;
625 +} badge_colors[] = {
626 +
627 + // colors from:
628 + // https://github.com/badges/shields/blob/master/colorscheme.json
629 +
630 + { "brightgreen", 0, "#4c1" },
631 + { "green", 0, "#97CA00" },
632 + { "yellow", 0, "#dfb317" },
633 + { "yellowgreen", 0, "#a4a61d" },
634 + { "orange", 0, "#fe7d37" },
635 + { "red", 0, "#e05d44" },
636 + { "blue", 0, "#007ec6" },
637 + { "grey", 0, "#555" },
638 + { "gray", 0, "#555" },
639 + { "lightgrey", 0, "#9f9f9f" },
640 + { "lightgray", 0, "#9f9f9f" },
641 +
642 + // terminator
643 + { NULL, 0, NULL }
644 +};
645 +
646 static inline const char *color_map(const char *color) {
622 - // colors from:
623 - // https://github.com/badges/shields/blob/master/colorscheme.json
624 - if(!strcmp(color, "brightgreen")) return "#4c1";
625 - else if(!strcmp(color, "green")) return "#97CA00";
626 - else if(!strcmp(color, "yellow")) return "#dfb317";
627 - else if(!strcmp(color, "yellowgreen")) return "#a4a61d";
628 - else if(!strcmp(color, "orange")) return "#fe7d37";
629 - else if(!strcmp(color, "red")) return "#e05d44";
630 - else if(!strcmp(color, "blue")) return "#007ec6";
631 - else if(!strcmp(color, "grey")) return "#555";
632 - else if(!strcmp(color, "gray")) return "#555";
633 - else if(!strcmp(color, "lightgrey")) return "#9f9f9f";
634 - else if(!strcmp(color, "lightgray")) return "#9f9f9f";
647 + static int max = -1;
648 + int i;
649 +
650 + if(unlikely(max == -1)) {
651 + for(i = 0; badge_colors[i].name ;i++)
652 + badge_colors[i].hash = simple_hash(badge_colors[i].name);
653 +
654 + max = i;
655 + }
656 +
657 + uint32_t hash = simple_hash(color);
658 +
659 + for(i = 0; i < max; i++) {
660 + struct badge_color *ptr = &badge_colors[i];
661 +
662 + if(hash == ptr->hash && !strcmp(color, ptr->name))
663 + return ptr->color;
664 + }
665 +
666 return color;
667 }
668