NeoMutt  2022-04-29-215-gc12b98
Teaching an old dog new tricks
DOXYGEN
merged.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <stddef.h>
33#include <stdbool.h>
34#include <stdint.h>
35#include "mutt/lib.h"
36#include "attr.h"
37#include "color.h"
38#include "curses2.h"
39#include "debug.h"
40
41struct AttrColorList MergedColors;
42
47{
49}
50
55{
56 struct AttrColor *ac = NULL;
57 struct AttrColor *tmp = NULL;
58
59 TAILQ_FOREACH_SAFE(ac, &MergedColors, entries, tmp)
60 {
61 TAILQ_REMOVE(&MergedColors, ac, entries);
63 FREE(&ac);
64 }
65}
66
74struct AttrColor *merged_colors_find(int fg, int bg, int attrs)
75{
76 struct AttrColor *ac = NULL;
77 TAILQ_FOREACH(ac, &MergedColors, entries)
78 {
79 if (ac->attrs != attrs)
80 continue;
81
82 bool has_color = (fg != COLOR_DEFAULT) || (bg != COLOR_DEFAULT);
83 struct CursesColor *cc = ac->curses_color;
84
85 // Both have only attributes
86 if (!has_color && !cc)
87 return ac;
88
89 // One has colour, but not the other
90 if ((has_color && !cc) || (!has_color && cc))
91 continue;
92
93 if ((cc->fg == fg) && (cc->bg == bg))
94 return ac;
95 }
96 return NULL;
97}
98
109struct AttrColor *merged_color_overlay(struct AttrColor *base, struct AttrColor *over)
110{
111 if (!over || (!over->curses_color && (over->attrs == 0)))
112 return base;
113 if (!base || (!base->curses_color && (base->attrs == 0)))
114 return over;
115
116 struct CursesColor *cc_base = base->curses_color;
117 struct CursesColor *cc_over = over->curses_color;
118
119 uint32_t fg = COLOR_DEFAULT;
120 uint32_t bg = COLOR_DEFAULT;
121
122 if (cc_over)
123 {
124 fg = cc_over->fg;
125 bg = cc_over->bg;
126 }
127
128 if (cc_base)
129 {
130 if (fg == COLOR_DEFAULT)
131 fg = cc_base->fg;
132 if (bg == COLOR_DEFAULT)
133 bg = cc_base->bg;
134 }
135
136 int attrs = base->attrs | over->attrs;
137
138 struct AttrColor *ac = merged_colors_find(fg, bg, attrs);
139 if (ac)
140 return ac;
141
142 ac = attr_color_new();
143 ac->curses_color = curses_color_new(fg, bg);
144 ac->attrs = attrs;
145 TAILQ_INSERT_TAIL(&MergedColors, ac, entries);
147
148 return ac;
149}
struct AttrColor * attr_color_new(void)
Create a new AttrColor.
Definition: attr.c:80
Colour and attributes.
Color and attribute parsing.
#define COLOR_DEFAULT
Definition: color.h:100
Curses Colour.
void curses_color_free(struct CursesColor **ptr)
Free a CursesColor.
Definition: curses.c:116
struct CursesColor * curses_color_new(int fg, int bg)
Create a new CursesColor.
Definition: curses.c:148
void merged_colors_dump(void)
Dump all the Merged colours to the log.
Definition: debug.c:449
Colour Debugging.
#define FREE(x)
Definition: memory.h:43
void merged_colors_init(void)
Initialise the Merged colours.
Definition: merged.c:46
struct AttrColorList MergedColors
Array of user colours.
Definition: merged.c:41
void merged_colors_clear(void)
Free the list of Merged colours.
Definition: merged.c:54
struct AttrColor * merged_color_overlay(struct AttrColor *base, struct AttrColor *over)
Combine two colours.
Definition: merged.c:109
struct AttrColor * merged_colors_find(int fg, int bg, int attrs)
Find a Merged colour.
Definition: merged.c:74
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:35
int attrs
Text attributes, e.g. A_BOLD.
Definition: attr.h:37
struct CursesColor * curses_color
Underlying Curses colour.
Definition: attr.h:36
Colour in the ncurses palette.
Definition: curses2.h:38
uint32_t fg
Foreground colour.
Definition: curses2.h:41
uint32_t bg
Background colour.
Definition: curses2.h:42