diff --git a/CMakeLists.txt b/CMakeLists.txt index ff9c652..cdafe45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project(identiconpp - VERSION 0.1.0 + VERSION 0.1.1 LANGUAGES CXX ) diff --git a/src/identiconpp.cpp b/src/identiconpp.cpp index f8f2e2e..cc071e7 100644 --- a/src/identiconpp.cpp +++ b/src/identiconpp.cpp @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include "identiconpp.hpp" #include "debug.hpp" @@ -32,21 +32,11 @@ Identiconpp::Identiconpp(const uint8_t rows, const uint8_t columns, , _background(background) , _foreground(foreground) { - if (background.length() != 8) - { - throw std::invalid_argument - ( - "The background color must consist of exactly 8 digits." - ); - } + check_color(background); for (const string &color : foreground) - if (color.length() != 8) { - throw std::invalid_argument - ( - "The foreground colors must consist of exactly 8 digits." - ); + check_color(color); } } @@ -194,3 +184,35 @@ Magick::Color Identiconpp::get_color(const uint16_t firstbit, ttdebug << "Color: #" << _foreground[bits - 1] << '\n'; return Magick::Color("#" + _foreground[bits - 1]); } + +bool Identiconpp::not_hex(const char c) +{ + if (c >= 0x61 && c <= 0x66) + { // a-f + return false; + } + if (c >= 0x30 && c <= 0x39) + { // 0-9 + return false; + } + + return true; +} + +void Identiconpp::check_color(const string &color) +{ + if (color.length() != 8) + { + throw std::invalid_argument + ( + "Colors must consist of exactly 8 digits(" + color + ")." + ); + } + if (std::any_of(color.begin(), color.end(), not_hex)) + { + throw std::invalid_argument + ( + "Colors must consist of hexadecimal digits (" + color + ")." + ); + } +} diff --git a/src/identiconpp.hpp b/src/identiconpp.hpp index b67ed7e..81ec234 100644 --- a/src/identiconpp.hpp +++ b/src/identiconpp.hpp @@ -61,7 +61,7 @@ public: * @param foreground vector of foreground colors */ explicit Identiconpp(const uint8_t rows, const uint8_t columns, - identicon_type type, + identicon_type type = identicon_type::simple, const string background = "ffffffff", const vector &foreground = { "000000ff" } ); @@ -139,4 +139,20 @@ private: * @return A foreground color. */ Magick::Color get_color(const uint16_t firstbit, const string &digest); + + /*! + * @brief Checks if character is not hexadecimal. + * + * @param c Character to check + * + * @return true if not hex. + */ + static bool not_hex(const char c); + + /*! + * @brief Performs checks on a color definition in a string. + * + * @param color The color as string + */ + void check_color(const string &color); };