ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
foreground.c
Go to the documentation of this file.
1
4
5/* Storage capacities in elements; text buffers include the terminating NUL. */
6#define VIDEO_ASCII_SCALAR_FOREGROUND_COLOR_CODE_BUFFER_SIZE 20
7
8#include <ascii-chat/common.h>
11
12char *image_print(const image_t *image, const char *palette) {
13 return ascii_render(image, palette, ASCII_COLOR_NONE, false, false, simd_cells_backend());
14}
15
16char *image_print_color(const image_t *image, const char *palette) {
17 return ascii_render(image, palette, ASCII_COLOR_TRUE, false, false, simd_cells_backend());
18}
19
20char *image_print_color_utf8(const image_t *image, const char *palette) {
21 return ascii_render(image, palette, ASCII_COLOR_TRUE, false, false, simd_cells_backend());
22}
23
24char *image_print_256color(const image_t *image, const char *palette) {
25 return ascii_render(image, palette, ASCII_COLOR_256, false, false, simd_cells_backend());
26}
27
28char *image_print_16color(const image_t *image, const char *palette) {
29 return ascii_render(image, palette, ASCII_COLOR_16, false, false, simd_cells_backend());
30}
31
32char *image_print_16color_dithered(const image_t *image, const char *palette) {
33 return ascii_render(image, palette, ASCII_COLOR_16, false, true, simd_cells_backend());
34}
35
36char *image_print_16color_dithered_with_background(const image_t *image, bool background, const char *palette) {
37 return ascii_render(image, palette, ASCII_COLOR_16, background, true, simd_cells_backend());
38}
39
40void quantize_color(int *r, int *g, int *b, int levels) {
41 if (!r || !g || !b) {
42 SET_ERRNO(ERROR_INVALID_PARAM, "r, g, or b is NULL");
43 return;
44 }
45
46 if (levels <= 0) {
47 SET_ERRNO(ERROR_INVALID_PARAM, "levels must be positive, got %d", levels);
48 return;
49 }
50
51 int step = 256 / levels;
52 *r = (*r / step) * step;
53 *g = (*g / step) * step;
54 *b = (*b / step) * step;
55}
56
57char *rgb_to_ansi_fg(int r, int g, int b) {
58 _Thread_local static char color_code[VIDEO_ASCII_SCALAR_FOREGROUND_COLOR_CODE_BUFFER_SIZE]; // \033[38;2;255;255;255m + \0 = 20 bytes max
59 SAFE_SNPRINTF(color_code, sizeof(color_code), "\033[38;2;%d;%d;%dm", r, g, b);
60 return color_code;
61}
62
63char *rgb_to_ansi_bg(int r, int g, int b) {
64 _Thread_local static char color_code[VIDEO_ASCII_SCALAR_FOREGROUND_COLOR_CODE_BUFFER_SIZE]; // \033[48;2;255;255;255m + \0 = 20 bytes max
65 SAFE_SNPRINTF(color_code, sizeof(color_code), "\033[48;2;%d;%d;%dm", r, g, b);
66 return color_code;
67}
68
69void rgb_to_ansi_8bit(int r, int g, int b, int *fg_code, int *bg_code) {
70 if (!fg_code || !bg_code) {
71 SET_ERRNO(ERROR_INVALID_PARAM, "fg_code or bg_code is NULL");
72 return;
73 }
74
75 // Convert RGB to 8-bit color code (216 color cube + 24 grayscale)
76 if (r == g && g == b) {
77 // Grayscale
78 if (r < 8) {
79 *fg_code = 16;
80 } else if (r > 248) {
81 *fg_code = 231;
82 } else {
83 *fg_code = 232 + (r - 8) / 10;
84 }
85 } else {
86 // Color cube: 16 + 36*r + 6*g + b where r,g,b are 0-5
87 int r_level = (r * 5) / 255;
88 int g_level = (g * 5) / 255;
89 int b_level = (b * 5) / 255;
90 *fg_code = 16 + 36 * r_level + 6 * g_level + b_level;
91 }
92 *bg_code = *fg_code; // Same logic for background
93}
#define SET_ERRNO(code,...)
const simd_cells_t * simd_cells_backend(void)
Definition cells.c:61
char * image_print_color_utf8(const image_t *image, const char *palette)
Definition foreground.c:20
#define VIDEO_ASCII_SCALAR_FOREGROUND_COLOR_CODE_BUFFER_SIZE
Definition foreground.c:6
#define SAFE_SNPRINTF(buffer, buffer_size,...)
@ ERROR_INVALID_PARAM
void quantize_color(int *r, int *g, int *b, int levels)
Quantize color to specified number of levels.
Definition foreground.c:40
char * image_print_16color_dithered(const image_t *image, const char *palette)
Print image using 16-color ANSI mode with dithering.
Definition foreground.c:32
char * image_print(const image_t *image, const char *palette)
Print image as ASCII art (monochrome).
Definition foreground.c:12
char * image_print_color(const image_t *image, const char *palette)
Print image as ASCII art with color.
Definition foreground.c:16
char * rgb_to_ansi_bg(int r, int g, int b)
Convert RGB to ANSI background color code.
Definition foreground.c:63
void rgb_to_ansi_8bit(int r, int g, int b, int *fg_code, int *bg_code)
Convert RGB to 8-bit ANSI color codes.
Definition foreground.c:69
char * image_print_16color(const image_t *image, const char *palette)
Print image using 16-color ANSI mode.
Definition foreground.c:28
char * rgb_to_ansi_fg(int r, int g, int b)
Convert RGB to ANSI foreground color code.
Definition foreground.c:57
char * image_print_16color_dithered_with_background(const image_t *image, bool background, const char *palette)
Print image using 16-color ANSI mode with dithering and background colors.
Definition foreground.c:36
char * image_print_256color(const image_t *image, const char *palette)
Print image using 256-color ANSI mode.
Definition foreground.c:24
⚙️ Common definitions, error codes, macros, and types shared throughout the application
#define ascii_render
SIMD-friendly storage for one RGB pixel. r, g, and b hold the color channels and padding keeps the la...