ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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
59 color_code[VIDEO_ASCII_SCALAR_FOREGROUND_COLOR_CODE_BUFFER_SIZE]; // \033[38;2;255;255;255m + \0 = 20 bytes max
60 SAFE_SNPRINTF(color_code, sizeof(color_code), "\033[38;2;%d;%d;%dm", r, g, b);
61 return color_code;
62}
63
64char *rgb_to_ansi_bg(int r, int g, int b) {
65 _Thread_local static char
66 color_code[VIDEO_ASCII_SCALAR_FOREGROUND_COLOR_CODE_BUFFER_SIZE]; // \033[48;2;255;255;255m + \0 = 20 bytes max
67 SAFE_SNPRINTF(color_code, sizeof(color_code), "\033[48;2;%d;%d;%dm", r, g, b);
68 return color_code;
69}
70
71void rgb_to_ansi_8bit(int r, int g, int b, int *fg_code, int *bg_code) {
72 if (!fg_code || !bg_code) {
73 SET_ERRNO(ERROR_INVALID_PARAM, "fg_code or bg_code is NULL");
74 return;
75 }
76
77 // Convert RGB to 8-bit color code (216 color cube + 24 grayscale)
78 if (r == g && g == b) {
79 // Grayscale
80 if (r < 8) {
81 *fg_code = 16;
82 } else if (r > 248) {
83 *fg_code = 231;
84 } else {
85 *fg_code = 232 + (r - 8) / 10;
86 }
87 } else {
88 // Color cube: 16 + 36*r + 6*g + b where r,g,b are 0-5
89 int r_level = (r * 5) / 255;
90 int g_level = (g * 5) / 255;
91 int b_level = (b * 5) / 255;
92 *fg_code = 16 + 36 * r_level + 6 * g_level + b_level;
93 }
94 *bg_code = *fg_code; // Same logic for background
95}
#define SET_ERRNO(code,...)
const simd_cells_t * simd_cells_backend(void)
Definition cells.c:70
⚙️ Common definitions, error codes, macros, and types shared throughout the application
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,...)
Definition common.h:507
@ 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:64
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:71
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
char * ascii_render(const image_t *image, const char *palette, ascii_color_t mode, bool background, bool dither, const simd_cells_t *ops)
SIMD-friendly storage for one RGB pixel. r, g, and b hold the color channels and padding keeps the la...