color.hh (1758B)
1 #ifndef color_hh_INCLUDED 2 #define color_hh_INCLUDED 3 4 #include "exception.hh" 5 #include "hash.hh" 6 #include "meta.hh" 7 #include "assert.hh" 8 9 namespace Kakoune 10 { 11 12 class String; 13 class StringView; 14 15 struct Color 16 { 17 enum NamedColor : unsigned char 18 { 19 Default, 20 Black, 21 Red, 22 Green, 23 Yellow, 24 Blue, 25 Magenta, 26 Cyan, 27 White, 28 BrightBlack, 29 BrightRed, 30 BrightGreen, 31 BrightYellow, 32 BrightBlue, 33 BrightMagenta, 34 BrightCyan, 35 BrightWhite, 36 RGB, 37 }; 38 39 union 40 { 41 NamedColor color; 42 unsigned char a; 43 }; 44 unsigned char r = 0; 45 unsigned char g = 0; 46 unsigned char b = 0; 47 48 constexpr bool isRGB() const { return a >= RGB; } 49 50 constexpr Color() : Color{Default} {} 51 constexpr Color(NamedColor c) : color{c} {} 52 constexpr Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255) 53 : a{a}, r{r}, g{g}, b{b} 54 { 55 validate_alpha(); 56 } 57 58 private: 59 constexpr void validate_alpha() { 60 static_assert(RGB == 17); 61 if (a < RGB) 62 throw runtime_error("Colors alpha must be > 16"); 63 } 64 }; 65 66 constexpr bool operator==(Color lhs, Color rhs) 67 { 68 return lhs.color == rhs.color and 69 lhs.r == rhs.r and lhs.g == rhs.g and lhs.b == rhs.b; 70 } 71 72 Color str_to_color(StringView color); 73 String to_string(Color color); 74 75 String option_to_string(Color color); 76 Color option_from_string(Meta::Type<Color>, StringView str); 77 78 bool is_color_name(StringView color); 79 80 constexpr size_t hash_value(const Color& val) 81 { 82 return val.isRGB() ? 83 hash_values(val.a, val.r, val.g, val.b) 84 : hash_value(val.color); 85 } 86 87 } 88 89 #endif // color_hh_INCLUDED