summaryrefslogtreecommitdiff
path: root/include/ncurses/colors.hpp
blob: a0622a9c32689c5c3ff80860caf848d5942a2313 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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_