Added more checks for colors.
All checks were successful
the build was successful

This commit is contained in:
tastytea 2018-12-26 04:55:18 +01:00
parent 024fd4eb79
commit 4201830e43
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 53 additions and 15 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.2)
project(identiconpp
VERSION 0.1.0
VERSION 0.1.1
LANGUAGES CXX
)

View File

@ -18,7 +18,7 @@
#include <stdexcept>
#include <sstream>
#include <bitset>
#include <iomanip>
#include <algorithm>
#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 + ")."
);
}
}

View File

@ -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<string> &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);
};