From 7d4452f45cf672bb77beb42178e5d78d66c55e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Ga=C3=9Fmann?= Date: Tue, 14 Sep 2021 14:22:30 +0200 Subject: [PATCH] Add an UDL for combined hex RGB colors (#203) In order to allow using the literal on its own it has been put into the inline namespace `literals`. --- examples/dom/style_color.cpp | 6 ++++-- include/ftxui/screen/color.hpp | 10 +++++++++- src/ftxui/screen/color.cpp | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/examples/dom/style_color.cpp b/examples/dom/style_color.cpp index 6a626cd..6f42bd1 100644 --- a/examples/dom/style_color.cpp +++ b/examples/dom/style_color.cpp @@ -28,7 +28,8 @@ int main(int argc, const char* argv[]) { color(Color::Red, text("Red")), color(Color::RedLight, text("RedLight")), color(Color::Yellow, text("Yellow")), - color(Color::YellowLight, text("YellowLight")) + color(Color::YellowLight, text("YellowLight")), + color(0x66ff66_rgb, text("Phosphor")) ), vbox( bgcolor(Color::Default, text("Default")), @@ -47,7 +48,8 @@ int main(int argc, const char* argv[]) { bgcolor(Color::Red, text("Red")), bgcolor(Color::RedLight, text("RedLight")), bgcolor(Color::Yellow, text("Yellow")), - bgcolor(Color::YellowLight, text("YellowLight")) + bgcolor(Color::YellowLight, text("YellowLight")), + bgcolor(0x66ff66_rgb, text("Phosphor")) ), filler() ); diff --git a/include/ftxui/screen/color.hpp b/include/ftxui/screen/color.hpp index 521c7cd..8ebd59a 100644 --- a/include/ftxui/screen/color.hpp +++ b/include/ftxui/screen/color.hpp @@ -2,7 +2,7 @@ #define FTXUI_SCREEN_COLOR #include // for uint8_t -#include // for wstring +#include // for wstring #ifdef RGB // Workaround for wingdi.h (via Windows.h) defining macros that break things. @@ -322,6 +322,14 @@ class Color { uint8_t blue_ = 0; }; +inline namespace literals { + +/// @brief Creates a color from a combined hex RGB representation, +/// e.g. 0x808000_rgb +Color operator""_rgb(unsigned long long int combined); + +} // namespace literals + } // namespace ftxui #endif /* end of include guard: FTXUI_COLOR_H_ */ diff --git a/src/ftxui/screen/color.cpp b/src/ftxui/screen/color.cpp index ed92e13..bf402e7 100644 --- a/src/ftxui/screen/color.cpp +++ b/src/ftxui/screen/color.cpp @@ -1,5 +1,7 @@ #include "ftxui/screen/color.hpp" +#include + #include "ftxui/screen/color_info.hpp" // for GetColorInfo, ColorInfo #include "ftxui/screen/terminal.hpp" // for Terminal, Terminal::Color, Terminal::Palette256, Terminal::TrueColor @@ -143,6 +145,18 @@ Color Color::HSV(uint8_t h, uint8_t s, uint8_t v) { return Color(0, 0, 0); } +inline namespace literals { + +Color operator""_rgb(unsigned long long int combined) { + assert(combined <= 0xffffffU); + auto const red = static_cast(combined >> 16); + auto const green = static_cast(combined >> 8); + auto const blue = static_cast(combined); + return Color(red, green, blue); +} + +} // namespace literals + } // namespace ftxui // Copyright 2020 Arthur Sonzogni. All rights reserved.