2014-07-11 01:27:04 +02:00
|
|
|
#ifndef face_hh_INCLUDED
|
|
|
|
#define face_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "color.hh"
|
2014-11-12 21:31:56 +01:00
|
|
|
#include "flags.hh"
|
2014-07-11 01:27:04 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-07-15 21:11:47 +02:00
|
|
|
enum class Attribute : int
|
|
|
|
{
|
2021-09-07 00:21:26 +02:00
|
|
|
Normal = 0,
|
|
|
|
Underline = 1 << 1,
|
|
|
|
CurlyUnderline = 1 << 2,
|
|
|
|
Reverse = 1 << 3,
|
|
|
|
Blink = 1 << 4,
|
|
|
|
Bold = 1 << 5,
|
|
|
|
Dim = 1 << 6,
|
|
|
|
Italic = 1 << 7,
|
|
|
|
Strikethrough = 1 << 8,
|
|
|
|
FinalFg = 1 << 9,
|
|
|
|
FinalBg = 1 << 10,
|
|
|
|
FinalAttr = 1 << 11,
|
|
|
|
Final = FinalFg | FinalBg | FinalAttr
|
2014-07-11 01:27:04 +02:00
|
|
|
};
|
|
|
|
|
2017-03-15 18:55:34 +01:00
|
|
|
constexpr bool with_bit_ops(Meta::Type<Attribute>) { return true; }
|
2014-07-15 21:11:47 +02:00
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
struct Face
|
|
|
|
{
|
2019-04-18 14:37:24 +02:00
|
|
|
Color fg = Color::Default;
|
|
|
|
Color bg = Color::Default;
|
|
|
|
Attribute attributes = Attribute::Normal;
|
2021-09-07 00:21:26 +02:00
|
|
|
Color underline = Color::Default;
|
2014-07-11 01:27:04 +02:00
|
|
|
|
2019-04-18 14:37:24 +02:00
|
|
|
friend constexpr bool operator==(const Face& lhs, const Face& rhs)
|
|
|
|
{
|
|
|
|
return lhs.fg == rhs.fg and
|
|
|
|
lhs.bg == rhs.bg and
|
2021-09-07 00:21:26 +02:00
|
|
|
lhs.underline == rhs.underline and
|
2019-04-18 14:37:24 +02:00
|
|
|
lhs.attributes == rhs.attributes;
|
|
|
|
}
|
2014-07-11 01:27:04 +02:00
|
|
|
|
2019-04-18 14:37:24 +02:00
|
|
|
friend constexpr bool operator!=(const Face& lhs, const Face& rhs)
|
|
|
|
{
|
|
|
|
return not (lhs == rhs);
|
|
|
|
}
|
2014-07-11 01:27:04 +02:00
|
|
|
|
2019-04-18 14:37:24 +02:00
|
|
|
friend constexpr size_t hash_value(const Face& val)
|
|
|
|
{
|
2021-09-07 00:21:26 +02:00
|
|
|
return hash_values(val.fg, val.bg, val.underline, val.attributes);
|
2019-04-18 14:37:24 +02:00
|
|
|
}
|
|
|
|
};
|
2018-04-10 12:35:23 +02:00
|
|
|
|
2018-09-23 15:17:12 +02:00
|
|
|
inline Face merge_faces(const Face& base, const Face& face)
|
2015-08-23 13:13:14 +02:00
|
|
|
{
|
2020-05-02 04:57:36 +02:00
|
|
|
auto alpha_blend = [](Color base, Color color) {
|
|
|
|
auto blend = [&](unsigned char Color::*field) {
|
|
|
|
int blended = (base.*field * (255 - color.a) + color.*field * color.a) / 255;
|
|
|
|
return static_cast<unsigned char>(blended <= 255 ? blended : 255);
|
|
|
|
};
|
2020-07-31 07:45:52 +02:00
|
|
|
int alpha = color.a + base.a * (255 - color.a) / 255;
|
|
|
|
return Color{blend(&Color::r), blend(&Color::g), blend(&Color::b),
|
|
|
|
static_cast<unsigned char>(alpha <= 255 ? alpha : 255)};
|
2020-05-02 04:57:36 +02:00
|
|
|
};
|
|
|
|
|
2018-09-23 15:17:12 +02:00
|
|
|
auto choose = [&](Color Face::*color, Attribute final_attr) {
|
|
|
|
if (face.attributes & final_attr)
|
|
|
|
return face.*color;
|
|
|
|
if (base.attributes & final_attr)
|
|
|
|
return base.*color;
|
|
|
|
if (face.*color == Color::Default)
|
|
|
|
return base.*color;
|
2020-05-02 04:57:36 +02:00
|
|
|
if ((base.*color).isRGB() and (face.*color).isRGB() and (face.*color).a != 255)
|
|
|
|
return alpha_blend(base.*color, face.*color);
|
2018-09-23 15:17:12 +02:00
|
|
|
return face.*color;
|
|
|
|
};
|
|
|
|
|
|
|
|
return Face{ choose(&Face::fg, Attribute::FinalFg),
|
|
|
|
choose(&Face::bg, Attribute::FinalBg),
|
|
|
|
face.attributes & Attribute::FinalAttr ? face.attributes :
|
|
|
|
base.attributes & Attribute::FinalAttr ? base.attributes :
|
2021-09-07 00:21:26 +02:00
|
|
|
face.attributes | base.attributes,
|
|
|
|
choose(&Face::underline, Attribute{0}) };
|
2015-08-23 13:13:14 +02:00
|
|
|
}
|
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // face_hh_INCLUDED
|