diff --git a/Applications/DataHolderLib/Color.cpp b/Applications/DataHolderLib/Color.cpp index ebc31eb04ae8dbfbca8d0f4cd0ad426fa86e1b02..463b4a7157f230abc0b910735a4d9e00fa76704c 100644 --- a/Applications/DataHolderLib/Color.cpp +++ b/Applications/DataHolderLib/Color.cpp @@ -14,20 +14,10 @@ #include "Color.h" -#include <fstream> -#include <sstream> - #include <logog/include/logog.hpp> -#include "BaseLib/StringTools.h" - namespace DataHolderLib { -Color createColor(unsigned char r, unsigned char g, unsigned char b) -{ - return Color{{r,g,b,255}}; -} - Color createColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a) { return Color{{r,g,b,a}}; @@ -35,26 +25,22 @@ Color createColor(unsigned char r, unsigned char g, unsigned char b, unsigned ch Color getRandomColor() { - Color col; - col[0] = static_cast<unsigned char>((rand()%5)*50); - col[1] = static_cast<unsigned char>((rand()%5)*50); - col[2] = static_cast<unsigned char>((rand()%5)*50); - return col; + return createColor(static_cast<unsigned char>((rand() % 5) * 50), + static_cast<unsigned char>((rand() % 5) * 50), + static_cast<unsigned char>((rand() % 5) * 50)); } Color const getColor(const std::string &id, std::map<std::string, Color> &colors) { - for (auto it = colors.begin(); it != colors.end(); ++it) + auto it = colors.find(id); + + if (it == end(colors)) { - if (id == it->first) - { - return it->second; - } + WARN("Key '%s' not found in color lookup table.", id.c_str()); + it = colors.insert({id, getRandomColor()}).first; } - WARN("Key '%s' not found in color lookup table.", id.c_str()); - Color c = getRandomColor(); - colors.insert(std::pair<std::string, Color>(id, c)); - return c; + + return it->second; } } // namespace DataHolderLib diff --git a/Applications/DataHolderLib/Color.h b/Applications/DataHolderLib/Color.h index 7b245c2f70bf85d67b859bb55ef6e07898a03c76..235cfa86875bc9dacf12c8ac085ed1c21b4f322a 100644 --- a/Applications/DataHolderLib/Color.h +++ b/Applications/DataHolderLib/Color.h @@ -23,17 +23,17 @@ namespace DataHolderLib { using Color = std::array<unsigned char, 4>; -Color createColor(unsigned char r, unsigned char g, unsigned char b); - -Color createColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a); +Color createColor(unsigned char r, + unsigned char g, + unsigned char b, + unsigned char a = 255); /// Returns a random RGB colour. Color getRandomColor(); /// Uses a color-lookup-table (in form of a map) to return a colour for a specified name. If the name is not /// in the colortable a new entry is created with the new name and a random colour. -Color const getColor(const std::string &id, std::map<std::string, DataHolderLib::Color> &colors); +Color const getColor(const std::string& id, + std::map<std::string, DataHolderLib::Color>& colors); -/// Convenience function to use the getColor method with numbers as identifiers. -Color const getColor(double id, std::map<std::string, DataHolderLib::Color> &colors); } // namespace DataHolderLib