NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
attr.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stddef.h>
32#include <string.h>
33#include "mutt/lib.h"
34#include "config/lib.h"
35#include "core/lib.h"
36#include "gui/lib.h"
37#include "attr.h"
38#include "color.h"
39#include "curses2.h"
40#include "debug.h"
41
49{
50 if (!ac)
51 return;
52
53 if (ac->curses_color)
54 color_debug(LL_DEBUG5, "clear %p\n", (void *) ac);
56
57 memset(&ac->fg, 0, sizeof(ac->fg));
58 memset(&ac->bg, 0, sizeof(ac->bg));
59
62 ac->attrs = A_NORMAL;
63}
64
69void attr_color_free(struct AttrColor **ptr)
70{
71 if (!ptr || !*ptr)
72 return;
73
74 struct AttrColor *ac = *ptr;
75 if (ac->ref_count > 1)
76 {
77 ac->ref_count--;
78 *ptr = NULL;
79 return;
80 }
81
83 FREE(ptr);
84}
85
91{
92 struct AttrColor *ac = mutt_mem_calloc(1, sizeof(*ac));
93
95 ac->fg.type = CT_SIMPLE;
97
99 ac->bg.type = CT_SIMPLE;
101
102 ac->attrs = A_NORMAL;
103
104 ac->ref_count = 1;
105
106 return ac;
107}
108
117void attr_color_list_clear(struct AttrColorList *acl)
118{
119 if (!acl)
120 return;
121
122 struct AttrColor *ac = NULL;
123 struct AttrColor *tmp = NULL;
124 TAILQ_FOREACH_SAFE(ac, acl, entries, tmp)
125 {
126 TAILQ_REMOVE(acl, ac, entries);
127 attr_color_free(&ac);
128 }
129}
130
139struct AttrColor *attr_color_list_find(struct AttrColorList *acl, color_t fg,
140 color_t bg, int attrs)
141{
142 if (!acl)
143 return NULL;
144
145 struct AttrColor *ac = NULL;
146 TAILQ_FOREACH(ac, acl, entries)
147 {
148 if (ac->attrs != attrs)
149 continue;
150
151 struct CursesColor *cc = ac->curses_color;
152 if (!cc)
153 continue;
154
155 if ((cc->fg == fg) && (cc->bg == bg))
156 return ac;
157 }
158 return NULL;
159}
160
166struct AttrColor attr_color_copy(const struct AttrColor *ac)
167{
168 if (ac)
169 return *ac;
170 else
171 return (struct AttrColor) { 0 };
172}
173
179bool attr_color_is_set(const struct AttrColor *ac)
180{
181 if (!ac)
182 return false;
183
184 return ((ac->attrs != A_NORMAL) || ac->curses_color);
185}
186
193bool attr_color_match(struct AttrColor *ac1, struct AttrColor *ac2)
194{
195 if ((!ac1) ^ (!ac2)) // One is set, but not the other
196 return false;
197
198 if (!ac1) // Two empty colours match
199 return true;
200
201 return ((ac1->curses_color == ac2->curses_color) && (ac1->attrs == ac2->attrs));
202}
203
211void modify_color_by_prefix(enum ColorPrefix prefix, bool is_fg, color_t *col, int *attrs)
212{
213 if (prefix == COLOR_PREFIX_NONE)
214 return; // nothing to do here
215
216 if (prefix == COLOR_PREFIX_ALERT)
217 {
218 *attrs |= A_BOLD;
219 *attrs |= A_BLINK;
220 }
221 else if (is_fg)
222 {
223 if ((COLORS >= 16) && (prefix == COLOR_PREFIX_LIGHT))
224 {
225 if (*col <= 7)
226 {
227 /* Advance the color 0-7 by 8 to get the light version */
228 *col += 8;
229 }
230 }
231 else
232 {
233 *attrs |= A_BOLD;
234 }
235 }
236 else
237 {
238 if (COLORS >= 16)
239 {
240 if (*col <= 7)
241 {
242 /* Advance the color 0-7 by 8 to get the light version */
243 *col += 8;
244 }
245 }
246 }
247}
248
249#ifdef NEOMUTT_DIRECT_COLORS
321{
322 static const color_t basic[] = {
323 0x000000, 0x800000, 0x008000, 0x808000, 0x000080, 0x800080,
324 0x008080, 0xc0c0c0, 0x808080, 0xff0000, 0x00ff00, 0xffff00,
325 0x0000ff, 0xff00ff, 0x00ffff, 0xffffff,
326 };
327
328 ASSERT(color < 256);
329
330 if (color < 0)
331 return color;
332
333 const bool c_color_directcolor = cs_subset_bool(NeoMutt->sub, "color_directcolor");
334 if (!c_color_directcolor)
335 {
336 return color;
337 }
338
339 if (color < 16)
340 {
341 color_debug(LL_DEBUG5, "Converted color 0-15: %d\n", color);
342 /* The first 16 colours are the "usual" terminal colours */
343 return basic[color];
344 }
345
346 if (color < 232)
347 {
348 /* The Color palette is divided in 6x6x6 colours, i.e. each R, G, B channel
349 * has six values:
350 *
351 * value: 1 2 3 4 5 6
352 * color: 0x00 0x5f 0x87 0xaf 0xd7 0xff
353 *
354 * The steps between the values is 0x28 = 40, the EXCEPT for the first one
355 * where it is 0x5f = 95.
356 *
357 * If we express the xterm color number minus 16 to base 6, i.e.
358 *
359 * color - 16 = (vr * 36) + (vg * 6) + (vb * 1)
360 *
361 * with vr, vg, vb integers between 0 and 5, then vr, vg, vb is the channel
362 * value for red, green, and blue, respectively.
363 */
364
365 color_t normalised_color = color - 16;
366 color_t vr = (normalised_color % 216) / 36; /* 216 = 6*6*6 */
367 color_t vg = (normalised_color % 36) / 6;
368 color_t vb = (normalised_color % 6) / 1;
369
370 /* First step is wider than the other ones, so add the difference if needed */
371 color_t r = (vr * 0x28) + ((vr > 0) ? (0x5f - 0x28) : 0);
372 color_t g = (vg * 0x28) + ((vg > 0) ? (0x5f - 0x28) : 0);
373 color_t b = (vb * 0x28) + ((vb > 0) ? (0x5f - 0x28) : 0);
374
375 color_t rgb = (r << 16) + (g << 8) + (b << 0);
376 color_debug(LL_DEBUG5, "Converted xterm color %d to RGB #%x:\n", color, rgb);
377 return rgb;
378 }
379
380 /* Grey scale starts at 0x08 and adds 0xa = 10 in very step ending in 0xee.
381 * There are a total of 6*4 = 24 grey colors in total. */
382 color_t steps = color - 232;
383 color_t grey = (steps * 0x0a) + 0x08;
384 color_t rgb = (grey << 16) + (grey << 8) + (grey << 0);
385 color_debug(LL_DEBUG5, "Converted xterm color %d to RGB #%x:\n", color, rgb);
386 return rgb;
387}
388#endif
389
395void attr_color_overwrite(struct AttrColor *ac_old, struct AttrColor *ac_new)
396{
397 if (!ac_old || !ac_new)
398 return;
399
400 color_t fg = ac_new->fg.color;
401 color_t bg = ac_new->bg.color;
402 int attrs = ac_new->attrs;
403
404 modify_color_by_prefix(ac_new->fg.prefix, true, &fg, &attrs);
405 modify_color_by_prefix(ac_new->bg.prefix, false, &bg, &attrs);
406
407#ifdef NEOMUTT_DIRECT_COLORS
408 if ((ac_new->fg.type == CT_SIMPLE) || (ac_new->fg.type == CT_PALETTE))
410 else if (fg < 8)
411 fg = 8;
412 if ((ac_new->bg.type == CT_SIMPLE) || (ac_new->bg.type == CT_PALETTE))
414 else if (bg < 8)
415 bg = 8;
416#endif
417
418 struct CursesColor *cc = curses_color_new(fg, bg);
420 ac_old->fg = ac_new->fg;
421 ac_old->bg = ac_new->bg;
422 ac_old->attrs = attrs;
423 ac_old->curses_color = cc;
424}
struct AttrColor * attr_color_list_find(struct AttrColorList *acl, color_t fg, color_t bg, int attrs)
Find an AttrColor in a list.
Definition: attr.c:139
void attr_color_overwrite(struct AttrColor *ac_old, struct AttrColor *ac_new)
Update an AttrColor in-place.
Definition: attr.c:395
void attr_color_clear(struct AttrColor *ac)
Free the contents of an AttrColor.
Definition: attr.c:48
color_t color_xterm256_to_24bit(const color_t color)
Convert a xterm color to its RGB value.
Definition: attr.c:320
bool attr_color_match(struct AttrColor *ac1, struct AttrColor *ac2)
Do the colours match?
Definition: attr.c:193
struct AttrColor attr_color_copy(const struct AttrColor *ac)
Copy a colour.
Definition: attr.c:166
struct AttrColor * attr_color_new(void)
Create a new AttrColor.
Definition: attr.c:90
void attr_color_free(struct AttrColor **ptr)
Free an AttrColor.
Definition: attr.c:69
void modify_color_by_prefix(enum ColorPrefix prefix, bool is_fg, color_t *col, int *attrs)
Modify a colour/attributes based on a prefix, e.g.
Definition: attr.c:211
void attr_color_list_clear(struct AttrColorList *acl)
Free the contents of an AttrColorList.
Definition: attr.c:117
bool attr_color_is_set(const struct AttrColor *ac)
Is the object coloured?
Definition: attr.c:179
Colour and attributes.
ColorPrefix
Constants for colour prefixes of named colours.
Definition: attr.h:45
@ COLOR_PREFIX_NONE
no prefix
Definition: attr.h:46
@ COLOR_PREFIX_ALERT
"alert" colour prefix
Definition: attr.h:47
@ COLOR_PREFIX_LIGHT
"light" colour prefix
Definition: attr.h:49
@ CT_SIMPLE
Simple colour, e.g. "Red".
Definition: attr.h:36
@ CT_PALETTE
Palette colour, e.g. "color207".
Definition: attr.h:37
Color and attribute parsing.
#define COLOR_DEFAULT
Definition: color.h:100
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
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
Colour Debugging.
static int color_debug(enum LogLevel level, const char *format,...)
Definition: debug.h:52
Convenience wrapper for the gui headers.
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
#define FREE(x)
Definition: memory.h:45
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_REMOVE(head, elm, field)
Definition: queue.h:841
#define ASSERT(COND)
Definition: signal2.h:58
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
short ref_count
Number of users.
Definition: attr.h:71
int attrs
Text attributes, e.g. A_BOLD.
Definition: attr.h:69
struct CursesColor * curses_color
Underlying Curses colour.
Definition: attr.h:70
enum ColorType type
Type of Colour.
Definition: attr.h:58
color_t color
Colour.
Definition: attr.h:57
enum ColorPrefix prefix
Optional Colour Modifier.
Definition: attr.h:59
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
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46