NeoMutt  2024-03-23-23-gec7045
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
merged.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <stddef.h>
33#include <stdbool.h>
34#include "mutt/lib.h"
35#include "attr.h"
36#include "color.h"
37#include "curses2.h"
38
39struct AttrColorList MergedColors;
40
45{
47}
48
53{
54 struct AttrColor *ac = NULL;
55 struct AttrColor *tmp = NULL;
56
57 TAILQ_FOREACH_SAFE(ac, &MergedColors, entries, tmp)
58 {
59 TAILQ_REMOVE(&MergedColors, ac, entries);
61 FREE(&ac);
62 }
63}
64
73{
74 struct AttrColor *ac = NULL;
75 TAILQ_FOREACH(ac, &MergedColors, entries)
76 {
77 if (ac->attrs != attrs)
78 continue;
79
80 bool has_color = (fg != COLOR_DEFAULT) || (bg != COLOR_DEFAULT);
81 struct CursesColor *cc = ac->curses_color;
82
83 // Both have only attributes
84 if (!has_color && !cc)
85 return ac;
86
87 // One has colour, but not the other
88 if ((has_color && !cc) || (!has_color && cc))
89 continue;
90
91 if ((cc->fg == fg) && (cc->bg == bg))
92 return ac;
93 }
94 return NULL;
95}
96
107const struct AttrColor *merged_color_overlay(const struct AttrColor *base,
108 const struct AttrColor *over)
109{
110 if (!attr_color_is_set(over))
111 return base;
112 if (!attr_color_is_set(base))
113 return over;
114
115 struct CursesColor *cc_base = base->curses_color;
116 struct CursesColor *cc_over = over->curses_color;
117
120
121 if (cc_over)
122 {
123 fg = cc_over->fg;
124 bg = cc_over->bg;
125 }
126
127 if (cc_base)
128 {
129 if (fg == COLOR_DEFAULT)
130 fg = cc_base->fg;
131 if (bg == COLOR_DEFAULT)
132 bg = cc_base->bg;
133 }
134
135 int attrs = base->attrs | over->attrs;
136
137 struct AttrColor *ac = merged_colors_find(fg, bg, attrs);
138 if (ac)
139 return ac;
140
141 ac = attr_color_new();
143 ac->attrs = attrs;
144 ac->fg = (base->fg.color == COLOR_DEFAULT) ? over->fg : base->fg;
145 ac->bg = (base->bg.color == COLOR_DEFAULT) ? over->bg : base->bg;
146 TAILQ_INSERT_TAIL(&MergedColors, ac, entries);
147
148 return ac;
149}
struct AttrColor * attr_color_new(void)
Create a new AttrColor.
Definition: attr.c:91
bool attr_color_is_set(const struct AttrColor *ac)
Is the object coloured?
Definition: attr.c:180
Colour and attributes.
Color and attribute parsing.
#define COLOR_DEFAULT
Definition: color.h:100
Curses Colour.
int32_t color_t
Type for 24-bit colour value.
Definition: curses2.h:31
struct CursesColor * curses_color_new(color_t fg, color_t bg)
Create a new CursesColor.
Definition: curses.c:151
void curses_color_free(struct CursesColor **ptr)
Free a CursesColor.
Definition: curses.c:120
#define FREE(x)
Definition: memory.h:45
void merged_colors_cleanup(void)
Free the list of Merged colours.
Definition: merged.c:52
void merged_colors_init(void)
Initialise the Merged colours.
Definition: merged.c:44
const struct AttrColor * merged_color_overlay(const struct AttrColor *base, const struct AttrColor *over)
Combine two colours.
Definition: merged.c:107
struct AttrColorList MergedColors
Array of user colours.
Definition: merged.c:39
static struct AttrColor * merged_colors_find(color_t fg, color_t bg, int attrs)
Find a Merged colour.
Definition: merged.c:72
Convenience wrapper for the library headers.
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:735
#define TAILQ_INIT(head)
Definition: queue.h:765
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:809
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:841
A curses colour and its attributes.
Definition: attr.h:66
struct ColorElement bg
Background colour.
Definition: attr.h:68
struct ColorElement fg
Foreground colour.
Definition: attr.h:67
int attrs
Text attributes, e.g. A_BOLD.
Definition: attr.h:69
struct CursesColor * curses_color
Underlying Curses colour.
Definition: attr.h:70
color_t color
Colour.
Definition: attr.h:57
Colour in the ncurses palette.
Definition: curses2.h:41
color_t fg
Foreground colour.
Definition: curses2.h:42
color_t bg
Background colour.
Definition: curses2.h:43