Skip to content

Commit

Permalink
basic string colors and hex colors with alpha handling (fixes #20)
Browse files Browse the repository at this point in the history
  • Loading branch information
neri14 committed Aug 29, 2024
1 parent ea6d2f3 commit b066647
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 9 deletions.
17 changes: 16 additions & 1 deletion src/video/overlay/widget/color.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "color.h"
#include "utils/logging/logger.h"

#include <sstream>
#include <iostream>

namespace vgraph {
namespace video {
namespace overlay {
Expand All @@ -9,9 +12,21 @@ rgba color_from_string(const std::string& str)
{
if (color::map.contains(str)) {
return color::map.at(str);
} else if (str.starts_with('#') && str.size() >= 7 && str.size() <= 9) {
rgba res {
std::stoi(str.substr(1, 2), nullptr, 16) / 255.0,
std::stoi(str.substr(3, 2), nullptr, 16) / 255.0,
std::stoi(str.substr(5, 2), nullptr, 16) / 255.0
};

if (str.size() == 9) {
res.a = std::stoi(str.substr(7, 2), nullptr, 16) / 255.0;
}

return res;
}

utils::logging::logger{"text_align_from_string"}.warning("Unsupported color \"{}\" provided, falling back to white", str);
utils::logging::logger{"color_from_string"}.warning("Failed to parse provided color \"{}\", falling back to white", str);
return color::white;
}

Expand Down
44 changes: 36 additions & 8 deletions src/video/overlay/widget/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,47 @@ struct rgba {
using rgb = rgba;

namespace color {
const rgb white{1.0, 1.0, 1.0};
const rgb black{0.0, 0.0, 0.0};
const rgb red {1.0, 0.0, 0.0};
const rgb black {0.00, 0.00, 0.00};
const rgb white {1.00, 1.00, 1.00};
const rgb red {1.00, 0.00, 0.00};
const rgb lime {0.00, 1.00, 0.00};
const rgb blue {0.00, 0.00, 1.00};
const rgb yellow {1.00, 1.00, 0.00};
const rgb cyan {0.00, 1.00, 1.00};
const rgb aqua {0.00, 1.00, 1.00};
const rgb magenta {1.00, 0.00, 1.00};
const rgb fuchsia {1.00, 0.00, 1.00};
const rgb silver {0.75, 0.75, 0.75};
const rgb gray {0.50, 0.50, 0.50};
const rgb maroon {0.50, 0.00, 0.00};
const rgb olive {0.50, 0.50, 0.00};
const rgb green {0.00, 0.50, 0.00};
const rgb purple {0.50, 0.00, 0.50};
const rgb teal {0.00, 0.50, 0.50};
const rgb navy {0.00, 0.00, 0.50};

const std::map<std::string, rgba> map = {
{"white", color::white},
{"black", color::black},
{"red" , color::red}
{"black" , color::black},
{"white" , color::white},
{"red" , color::red},
{"lime" , color::lime},
{"blue" , color::blue},
{"yellow" , color::yellow},
{"cyan" , color::cyan},
{"aqua" , color::aqua},
{"magenta", color::magenta},
{"fuchsia", color::fuchsia},
{"silver" , color::silver},
{"gray" , color::gray},
{"maroon" , color::maroon},
{"olive" , color::olive},
{"green" , color::green},
{"purple" , color::purple},
{"teal" , color::teal},
{"navy" , color::navy}
};
}



rgba color_from_string(const std::string& str);

} // namespace overlay
Expand Down
1 change: 1 addition & 0 deletions test/video/overlay/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ cmake_minimum_required(VERSION 3.20.0)
target_sources(vgraph_test
PRIVATE
layout_test.cpp
color_test.cpp
)
53 changes: 53 additions & 0 deletions test/video/overlay/color_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <gtest/gtest.h>

#include "video/overlay/widget/color.h"

namespace vgraph {
namespace video {
namespace overlay {
namespace consts {
double epsilon(0.01);
}

void expect_rgba(rgba color, std::string str)
{
rgba result = color_from_string(str);

EXPECT_NEAR(color.r, result.r, consts::epsilon);
EXPECT_NEAR(color.g, result.g, consts::epsilon);
EXPECT_NEAR(color.b, result.b, consts::epsilon);
EXPECT_NEAR(color.a, result.a, consts::epsilon);
}

TEST(color_test, text_colors)
{
expect_rgba({0.0, 0.0, 0.0, 1.0}, "black");
expect_rgba({1.0, 1.0, 1.0, 1.0}, "white");
expect_rgba({1.0, 0.0, 0.0, 1.0}, "red");
expect_rgba({0.0, 1.0, 0.0, 1.0}, "lime");
expect_rgba({0.0, 0.0, 1.0, 1.0}, "blue");
}

TEST(color_test, hex_colors)
{
expect_rgba({0.0, 0.0, 0.0, 1.0}, "#000000");
expect_rgba({1.0, 1.0, 1.0, 1.0}, "#FFFFFF");
expect_rgba({1.0, 1.0, 1.0, 1.0}, "#ffffff");
expect_rgba({1.0, 0.0, 0.0, 1.0}, "#FF0000");
expect_rgba({0.0, 1.0, 0.0, 1.0}, "#00FF00");
expect_rgba({0.0, 0.0, 1.0, 1.0}, "#0000FF");
}

TEST(color_test, hex_colors_with_alpha)
{
expect_rgba({0.0, 0.0, 0.0, 0.0}, "#00000000");
expect_rgba({1.0, 1.0, 1.0, 1.0}, "#FFFFFFFF");
expect_rgba({1.0, 1.0, 1.0, 1.0}, "#ffffffff");
expect_rgba({1.0, 0.0, 0.0, 0.5}, "#FF00007F");
expect_rgba({0.0, 1.0, 0.0, 1.0}, "#00FF00ff");
expect_rgba({0.0, 0.0, 1.0, 0.0}, "#0000FF00");
}

}
}
}

0 comments on commit b066647

Please sign in to comment.