summaryrefslogtreecommitdiff
path: root/include/ncurses/colors.hpp
diff options
context:
space:
mode:
authorDaniil Rozanov <dev@rozanov.info>2025-03-15 18:03:23 +0400
committerDaniil Rozanov <dev@rozanov.info>2025-03-15 18:03:23 +0400
commit4a9ce6e2555dfaf9155fa279f25667350377f688 (patch)
tree11bc0ea3a7b1c0be2c47419b7058d46d16e5f9f4 /include/ncurses/colors.hpp
feat: chtype wrap
Diffstat (limited to 'include/ncurses/colors.hpp')
-rw-r--r--include/ncurses/colors.hpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/include/ncurses/colors.hpp b/include/ncurses/colors.hpp
new file mode 100644
index 0000000..a0622a9
--- /dev/null
+++ b/include/ncurses/colors.hpp
@@ -0,0 +1,87 @@
+#ifndef INCLUDE_NCURSES_COLORS_HPP_
+#define INCLUDE_NCURSES_COLORS_HPP_
+
+#include "ncurses/utils/macros.hpp"
+#include <ncurses.h>
+
+#include <ncurses/utils/preamble.hpp>
+
+namespace NCURSES_CPP_NAMESPACE {
+
+enum class curs_color {
+ black = COLOR_BLACK,
+ red = COLOR_RED,
+ green = COLOR_GREEN,
+ yellow = COLOR_YELLOW,
+ blue = COLOR_BLUE,
+ magenta = COLOR_MAGENTA,
+ cyan = COLOR_CYAN,
+ white = COLOR_WHITE
+};
+
+struct rgb_color {
+ unsigned char r;
+ unsigned char g;
+ unsigned char b;
+};
+
+NCURSES_CPP_CONSTEXPR static int
+rgb_to_curses(unsigned char c) NCURSES_CPP_NOEXCEPT {
+ return static_cast<int>(c) * 1000 / 255;
+}
+
+NCURSES_CPP_CONSTEXPR static unsigned char
+curses_to_rgb(int c) NCURSES_CPP_NOEXCEPT {
+ return static_cast<unsigned char>((c * 255 / 1000) +
+ (c * 255 % 1000 == 0 ? 0 : 1));
+}
+
+inline bool has_colors() { return static_cast<bool>(::has_colors()); }
+
+inline bool can_change_color() {
+ return static_cast<bool>(::can_change_color());
+}
+
+inline bool init_color(int index, int r, int g, int b) {
+ return init_extended_color(index, rgb_to_curses(r), rgb_to_curses(g),
+ rgb_to_curses(b)) == OK
+ ? true
+ : false;
+}
+
+inline bool color_content(int index, int *r, int *g, int *b) {
+ int res = extended_color_content(index, r, g, b);
+ if (res == ERR)
+ return false;
+ *r = curses_to_rgb(*r);
+ *g = curses_to_rgb(*g);
+ *b = curses_to_rgb(*b);
+ return true;
+}
+
+inline bool init_pair(int index, int fg, int bg) {
+ return init_extended_pair(index, fg, bg) == OK ? true : false;
+}
+
+inline bool pair_content(int index, int *fg, int *bg) {
+ return extended_pair_content(index, fg, bg);
+}
+
+class color_pair {
+public:
+ using value_type = chtype;
+ NCURSES_CPP_CONSTEXPR color_pair(int index) NCURSES_CPP_NOEXCEPT
+ : index_(index) {}
+
+ explicit NCURSES_CPP_CONSTEXPR
+ operator value_type() const NCURSES_CPP_NOEXCEPT {
+ return COLOR_PAIR(index_);
+ }
+
+private:
+ int index_;
+};
+
+} // namespace NCURSES_CPP_NAMESPACE
+
+#endif // INCLUDE_NCURSES_COLORS_HPP_