summaryrefslogtreecommitdiff
path: root/src/face.hh
blob: 7e887705206eee9a9231aa459ddd0f4a9f93433e (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
#ifndef face_hh_INCLUDED
#define face_hh_INCLUDED

#include "color.hh"
#include "flags.hh"

namespace Kakoune
{

enum class Attribute : int
{
    Normal          = 0,
    Underline       = 1 << 1,
    CurlyUnderline  = 1 << 2,
    DoubleUnderline = 1 << 3,
    Reverse         = 1 << 4,
    Blink           = 1 << 5,
    Bold            = 1 << 6,
    Dim             = 1 << 7,
    Italic          = 1 << 8,
    Strikethrough   = 1 << 9,
    FinalFg         = 1 << 10,
    FinalBg         = 1 << 11,
    FinalAttr       = 1 << 12,
    Final           = FinalFg | FinalBg | FinalAttr
};

constexpr bool with_bit_ops(Meta::Type<Attribute>) { return true; }

struct Face
{
    Color fg = Color::Default;
    Color bg = Color::Default;
    Attribute attributes = Attribute::Normal;
    Color underline = Color::Default;

    friend constexpr bool operator==(const Face& lhs, const Face& rhs) = default;

    friend constexpr size_t hash_value(const Face& val)
    {
        return hash_values(val.fg, val.bg, val.underline, val.attributes);
    }
};

inline Face merge_faces(const Face& base, const Face& face)
{
    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);
        };
        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)};
    };

    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;
        if ((base.*color).isRGB() and (face.*color).isRGB() and (face.*color).a != 255)
            return alpha_blend(base.*color, face.*color);
        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,
                 choose(&Face::underline, Attribute{0}) };
}

}

#endif // face_hh_INCLUDED