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
|
|
|
|
{
|
|
|
|
Normal = 0,
|
2018-09-23 15:17:12 +02:00
|
|
|
Underline = 1 << 1,
|
|
|
|
Reverse = 1 << 2,
|
|
|
|
Blink = 1 << 3,
|
|
|
|
Bold = 1 << 4,
|
|
|
|
Dim = 1 << 5,
|
|
|
|
Italic = 1 << 6,
|
|
|
|
FinalFg = 1 << 7,
|
|
|
|
FinalBg = 1 << 8,
|
2018-09-24 09:54:21 +02:00
|
|
|
FinalAttr = 1 << 9,
|
2018-09-23 15:17:12 +02:00
|
|
|
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;
|
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
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return hash_values(val.fg, val.bg, val.attributes);
|
|
|
|
}
|
|
|
|
};
|
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);
|
|
|
|
};
|
|
|
|
return Color{blend(&Color::r), blend(&Color::g), blend(&Color::b), base.a};
|
|
|
|
};
|
|
|
|
|
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 :
|
|
|
|
face.attributes | base.attributes };
|
2015-08-23 13:13:14 +02:00
|
|
|
}
|
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // face_hh_INCLUDED
|