diff --git a/histogram_internal.h b/histogram_internal.h index 59c1874..7cfb60d 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -1,8 +1,11 @@ #ifndef HISTOGRAM_INTERNAL_H_INCLUDED #define HISTOGRAM_INTERNAL_H_INCLUDED #include +#include void find_minmax(const std::vector& numbers, double& min, double& max); +std::string +bin_color(size_t bin, size_t max_count); #endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/svg.cpp b/svg.cpp index 8d30bab..89fe2fe 100644 --- a/svg.cpp +++ b/svg.cpp @@ -28,6 +28,10 @@ svg_rect(double x, double y, double width, double height, string stroke, string cout << ""; } +string bin_color(size_t bin, size_t max_count){ + return ("#" + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count)); +} + void show_histogram_svg(const vector& bins) { const auto IMAGE_WIDTH = 400; @@ -46,12 +50,12 @@ show_histogram_svg(const vector& bins) { } } - double top = 0; for (size_t bin : bins) { const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count; + const auto color = bin_color(bin,max_count); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red","#ffeeee"); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"grey", color); top += BIN_HEIGHT; } svg_end(); diff --git a/svg.h b/svg.h index 86a4a8f..66a0f20 100644 --- a/svg.h +++ b/svg.h @@ -15,6 +15,9 @@ show_histogram_svg(const std::vector& bins); void svg_text(double left, double baseline, std::string text); +std::string +bin_color(size_t bin, size_t max_count); + void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fil = "black"); diff --git a/unittest.cpp b/unittest.cpp index a9ba7e5..dbb684b 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -3,6 +3,8 @@ #include "doctest.h" #include "histogram_internal.h" +using namespace std; + TEST_CASE("distinct positive numbers") { double min = 0; double max = 0; @@ -10,3 +12,13 @@ TEST_CASE("distinct positive numbers") { CHECK(min == 1); CHECK(max == 2); } + +TEST_CASE("bin color") { + size_t max_bin = 10; + size_t min_bin = 3; + string rat = "#" + to_string(10 - (min_bin * 9) / max_bin) + to_string(10 - (min_bin * 9) / max_bin) + to_string(10 - (min_bin * 9) / max_bin); + auto test1 = bin_color(max_bin,max_bin); + auto test2 = bin_color(min_bin,max_bin); + CHECK(test1 == "#111"); + CHECK(test2 == rat); +}