2012-09-17 19:01:13 +02:00
|
|
|
#ifndef color_hh_INCLUDED
|
|
|
|
#define color_hh_INCLUDED
|
|
|
|
|
2022-02-12 11:35:33 +01:00
|
|
|
#include "exception.hh"
|
2014-12-16 19:57:19 +01:00
|
|
|
#include "hash.hh"
|
2018-05-27 05:00:04 +02:00
|
|
|
#include "meta.hh"
|
2020-05-02 04:57:36 +02:00
|
|
|
#include "assert.hh"
|
2014-12-16 19:57:19 +01:00
|
|
|
|
2012-09-17 19:01:13 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-03-25 23:35:59 +01:00
|
|
|
class String;
|
2014-08-20 00:16:21 +02:00
|
|
|
class StringView;
|
2013-03-25 23:35:59 +01:00
|
|
|
|
2013-05-07 18:52:23 +02:00
|
|
|
struct Color
|
|
|
|
{
|
2020-05-02 04:57:36 +02:00
|
|
|
enum NamedColor : unsigned char
|
2015-04-25 11:47:39 +02:00
|
|
|
{
|
|
|
|
Default,
|
|
|
|
Black,
|
|
|
|
Red,
|
|
|
|
Green,
|
|
|
|
Yellow,
|
|
|
|
Blue,
|
|
|
|
Magenta,
|
|
|
|
Cyan,
|
|
|
|
White,
|
2017-10-25 05:08:22 +02:00
|
|
|
BrightBlack,
|
|
|
|
BrightRed,
|
|
|
|
BrightGreen,
|
|
|
|
BrightYellow,
|
|
|
|
BrightBlue,
|
|
|
|
BrightMagenta,
|
|
|
|
BrightCyan,
|
|
|
|
BrightWhite,
|
2015-04-25 11:47:39 +02:00
|
|
|
RGB,
|
|
|
|
};
|
|
|
|
|
2020-05-02 04:57:36 +02:00
|
|
|
union
|
|
|
|
{
|
|
|
|
NamedColor color;
|
|
|
|
unsigned char a;
|
|
|
|
};
|
2015-04-25 11:47:39 +02:00
|
|
|
unsigned char r = 0;
|
|
|
|
unsigned char g = 0;
|
|
|
|
unsigned char b = 0;
|
2013-05-07 18:52:23 +02:00
|
|
|
|
2020-05-02 04:57:36 +02:00
|
|
|
constexpr bool isRGB() const { return a >= RGB; }
|
|
|
|
|
2015-04-25 11:47:39 +02:00
|
|
|
constexpr Color() : Color{Default} {}
|
|
|
|
constexpr Color(NamedColor c) : color{c} {}
|
2020-05-02 04:57:36 +02:00
|
|
|
constexpr Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255)
|
|
|
|
: a{a}, r{r}, g{g}, b{b}
|
|
|
|
{
|
|
|
|
validate_alpha();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-02-12 11:35:33 +01:00
|
|
|
constexpr void validate_alpha() {
|
|
|
|
static_assert(RGB == 17);
|
|
|
|
if (a < RGB)
|
|
|
|
throw runtime_error("Colors alpha must be > 16");
|
|
|
|
}
|
2012-09-17 19:01:13 +02:00
|
|
|
};
|
|
|
|
|
2015-03-20 20:03:41 +01:00
|
|
|
constexpr bool operator==(Color lhs, Color rhs)
|
2014-12-16 19:57:19 +01:00
|
|
|
{
|
|
|
|
return lhs.color == rhs.color and
|
|
|
|
lhs.r == rhs.r and lhs.g == rhs.g and lhs.b == rhs.b;
|
|
|
|
}
|
|
|
|
|
2015-03-20 20:03:41 +01:00
|
|
|
constexpr bool operator!=(Color lhs, Color rhs)
|
2014-12-16 19:57:19 +01:00
|
|
|
{
|
|
|
|
return not (lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2014-08-20 00:16:21 +02:00
|
|
|
Color str_to_color(StringView color);
|
2017-09-12 05:31:57 +02:00
|
|
|
String to_string(Color color);
|
2013-03-25 23:35:59 +01:00
|
|
|
|
2013-07-26 00:26:43 +02:00
|
|
|
String option_to_string(Color color);
|
2018-05-27 05:00:04 +02:00
|
|
|
Color option_from_string(Meta::Type<Color>, StringView str);
|
2014-08-20 00:16:21 +02:00
|
|
|
|
|
|
|
bool is_color_name(StringView color);
|
2013-03-29 19:31:06 +01:00
|
|
|
|
2017-10-17 16:45:17 +02:00
|
|
|
constexpr size_t hash_value(const Color& val)
|
2014-12-16 19:57:19 +01:00
|
|
|
{
|
2020-05-02 04:57:36 +02:00
|
|
|
return val.isRGB() ?
|
|
|
|
hash_values(val.a, val.r, val.g, val.b)
|
2015-02-10 23:53:37 +01:00
|
|
|
: hash_value(val.color);
|
2014-12-16 19:57:19 +01:00
|
|
|
}
|
|
|
|
|
2012-09-17 19:01:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // color_hh_INCLUDED
|