dte test coverage


Directory: ./
File: src/terminal/color.h
Date: 2024-12-21 16:03:22
Exec Total Coverage
Lines: 12 12 100.0%
Functions: 5 5 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 #ifndef TERMINAL_COLOR_H
2 #define TERMINAL_COLOR_H
3
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <stdint.h>
7 #include "terminal.h"
8 #include "util/macros.h"
9
10 #define COLOR_RGB(x) (COLOR_FLAG_RGB | (x))
11
12 enum {
13 COLOR_INVALID = -3,
14 COLOR_KEEP = -2,
15 COLOR_DEFAULT = -1,
16 COLOR_BLACK = 0,
17 COLOR_RED = 1,
18 COLOR_GREEN = 2,
19 COLOR_YELLOW = 3,
20 COLOR_BLUE = 4,
21 COLOR_MAGENTA = 5,
22 COLOR_CYAN = 6,
23 COLOR_GRAY = 7,
24 COLOR_DARKGRAY = 8,
25 COLOR_LIGHTRED = 9,
26 COLOR_LIGHTGREEN = 10,
27 COLOR_LIGHTYELLOW = 11,
28 COLOR_LIGHTBLUE = 12,
29 COLOR_LIGHTMAGENTA = 13,
30 COLOR_LIGHTCYAN = 14,
31 COLOR_WHITE = 15,
32
33 // This bit flag is used to allow 24-bit RGB colors to be differentiated
34 // from basic colors (e.g. #000004 vs. COLOR_BLUE)
35 COLOR_FLAG_RGB = INT32_C(1) << 24
36 };
37
38 1202 static inline bool color_is_rgb(int32_t color)
39 {
40 1202 return !!(color & COLOR_FLAG_RGB);
41 }
42
43 1757 static inline bool color_is_valid(int32_t color)
44 {
45 1757 bool palette = (color >= COLOR_KEEP && color <= 255);
46 1757 bool rgb = (color >= COLOR_RGB(0) && color <= COLOR_RGB(0xFFFFFF));
47 1757 return palette || rgb;
48 }
49
50 // Extract red channel from RGB color
51 425 static inline uint8_t color_r(int32_t rgb)
52 {
53 425 return (rgb >> 16) & 0xFF;
54 }
55
56 // Extract green channel from RGB color
57 425 static inline uint8_t color_g(int32_t rgb)
58 {
59 425 return (rgb >> 8) & 0xFF;
60 }
61
62 // Extract blue channel from RGB color
63 425 static inline uint8_t color_b(int32_t rgb)
64 {
65 425 return rgb & 0xFF;
66 }
67
68 int32_t parse_rgb(const char *str, size_t len);
69 int32_t color_to_nearest(int32_t color, TermFeatureFlags flags, bool optimize);
70
71 #endif
72