Luma 0.1.0
A low-level compiled alternative to C, C++, and more!
Loading...
Searching...
No Matches
Functions
color.h File Reference

ANSI escape codes and convenience macros for terminal text coloring and styling. More...

#include <stdbool.h>
Include dependency graph for color.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

ANSI Color Codes
#define COLOR_RED   "\033[31m"
 
#define COLOR_GREEN   "\033[32m"
 
#define COLOR_BLUE   "\033[34m"
 
#define COLOR_CYAN   "\033[36m"
 
#define COLOR_MAGENTA   "\033[35m"
 
#define COLOR_YELLOW   "\033[33m"
 
#define COLOR_WHITE   "\033[37m"
 
#define COLOR_BLACK   "\033[30m"
 
#define COLOR_GRAY   "\033[90m"
 
ANSI Style Codes
#define STYLE_RESET   "\033[0m"
 
#define STYLE_BOLD   "\033[1m"
 
#define STYLE_UNDERLINE   "\033[4m"
 
Background Color Codes
#define BG_RED   "\033[41m"
 
#define BG_GREEN   "\033[42m"
 
#define BG_BLUE   "\033[44m"
 
#define BG_CYAN   "\033[46m"
 
#define BG_MAGENTA   "\033[45m"
 
#define BG_YELLOW   "\033[43m"
 
#define BG_WHITE   "\033[47m"
 
#define BG_BLACK   "\033[40m"
 
#define BG_GRAY   "\033[100m"
 
Convenience Macros for Common Color Usage

These macros conditionally apply color styling to text if the terminal supports color, otherwise return the original text unchanged.

#define RED(text)   (terminal_supports_color() ? COLOR_RED text STYLE_RESET : text)
 
#define GREEN(text)   (terminal_supports_color() ? COLOR_GREEN text STYLE_RESET : text)
 
#define BLUE(text)   (terminal_supports_color() ? COLOR_BLUE text STYLE_RESET : text)
 
#define CYAN(text)   (terminal_supports_color() ? COLOR_CYAN text STYLE_RESET : text)
 
#define MAGENTA(text)   (terminal_supports_color() ? COLOR_MAGENTA text STYLE_RESET : text)
 
#define YELLOW(text)   (terminal_supports_color() ? COLOR_YELLOW text STYLE_RESET : text)
 
#define WHITE(text)   (terminal_supports_color() ? COLOR_WHITE text STYLE_RESET : text)
 
#define BLACK(text)   (terminal_supports_color() ? COLOR_BLACK text STYLE_RESET : text)
 
#define GRAY(text)   (terminal_supports_color() ? COLOR_GRAY text STYLE_RESET : text)
 
Bold Color Macros

Macros to print bold colored text conditionally based on terminal support.

#define BOLD_RED(text)   (terminal_supports_color() ? STYLE_BOLD COLOR_RED text STYLE_RESET : text)
 
#define BOLD_GREEN(text)   (terminal_supports_color() ? STYLE_BOLD COLOR_GREEN text STYLE_RESET : text)
 
#define BOLD_BLUE(text)   (terminal_supports_color() ? STYLE_BOLD COLOR_BLUE text STYLE_RESET : text)
 
#define BOLD_CYAN(text)   (terminal_supports_color() ? STYLE_BOLD COLOR_CYAN text STYLE_RESET : text)
 
#define BOLD_MAGENTA(text)   (terminal_supports_color() ? STYLE_BOLD COLOR_MAGENTA text STYLE_RESET : text)
 
#define BOLD_YELLOW(text)   (terminal_supports_color() ? STYLE_BOLD COLOR_YELLOW text STYLE_RESET : text)
 
#define BOLD_WHITE(text)   (terminal_supports_color() ? STYLE_BOLD COLOR_WHITE text STYLE_RESET : text)
 
#define BOLD_BLACK(text)   (terminal_supports_color() ? STYLE_BOLD COLOR_BLACK text STYLE_RESET : text)
 
#define BOLD_GRAY(text)   (terminal_supports_color() ? STYLE_BOLD COLOR_GRAY text STYLE_RESET : text)
 
Underlined Color Macros

Macros to print underlined colored text conditionally based on terminal support.

#define UNDERLINE_RED(text)   (terminal_supports_color() ? STYLE_UNDERLINE COLOR_RED text STYLE_RESET : text)
 
#define UNDERLINE_GREEN(text)   (terminal_supports_color() ? STYLE_UNDERLINE COLOR_GREEN text STYLE_RESET : text)
 
#define UNDERLINE_BLUE(text)   (terminal_supports_color() ? STYLE_UNDERLINE COLOR_BLUE text STYLE_RESET : text)
 
#define UNDERLINE_CYAN(text)   (terminal_supports_color() ? STYLE_UNDERLINE COLOR_CYAN text STYLE_RESET : text)
 
#define UNDERLINE_MAGENTA(text)   (terminal_supports_color() ? STYLE_UNDERLINE COLOR_MAGENTA text STYLE_RESET : text)
 
#define UNDERLINE_YELLOW(text)   (terminal_supports_color() ? STYLE_UNDERLINE COLOR_YELLOW text STYLE_RESET : text)
 
#define UNDERLINE_WHITE(text)   (terminal_supports_color() ? STYLE_UNDERLINE COLOR_WHITE text STYLE_RESET : text)
 
#define UNDERLINE_BLACK(text)   (terminal_supports_color() ? STYLE_UNDERLINE COLOR_BLACK text STYLE_RESET : text)
 
#define UNDERLINE_GRAY(text)   (terminal_supports_color() ? STYLE_UNDERLINE COLOR_GRAY text STYLE_RESET : text)
 
Custom Formatting Macros

Macros to apply arbitrary color and style sequences conditionally.

#define COLORIZE(color, text)   (terminal_supports_color() ? color text STYLE_RESET : text)
 
#define BOLD_COLORIZE(color, text)   (terminal_supports_color() ? STYLE_BOLD color text STYLE_RESET : text)
 
#define UNDERLINE_COLORIZE(color, text)   (terminal_supports_color() ? STYLE_UNDERLINE color text STYLE_RESET : text)
 
#define STYLE_COLORIZE(style, color, text)   (terminal_supports_color() ? style color text STYLE_RESET : text)
 

Functions

bool terminal_supports_color (void)
 Checks if the terminal supports ANSI color escape sequences.
 

Detailed Description

ANSI escape codes and convenience macros for terminal text coloring and styling.

This header provides definitions for ANSI color and style escape sequences, as well as convenient macros for coloring and styling text conditionally based on whether the terminal supports color output.

The macros wrap strings with appropriate ANSI sequences and reset codes, enabling colorful, bold, and underlined text output in terminal applications.

Usage example:

printf("%s\n", RED("Error: Something went wrong"));
printf("%s\n", BOLD_GREEN("Success!"));
#define RED(text)
Definition color.h:66
#define BOLD_GREEN(text)
Definition color.h:83

Macro Definition Documentation

◆ BG_BLACK

#define BG_BLACK   "\033[40m"

ANSI code for black background

◆ BG_BLUE

#define BG_BLUE   "\033[44m"

ANSI code for blue background

◆ BG_CYAN

#define BG_CYAN   "\033[46m"

ANSI code for cyan background

◆ BG_GRAY

#define BG_GRAY   "\033[100m"

ANSI code for gray background

◆ BG_GREEN

#define BG_GREEN   "\033[42m"

ANSI code for green background

◆ BG_MAGENTA

#define BG_MAGENTA   "\033[45m"

ANSI code for magenta background

◆ BG_RED

#define BG_RED   "\033[41m"

ANSI code for red background

◆ BG_WHITE

#define BG_WHITE   "\033[47m"

ANSI code for white background

◆ BG_YELLOW

#define BG_YELLOW   "\033[43m"

ANSI code for yellow background

◆ BLACK

#define BLACK (   text)    (terminal_supports_color() ? COLOR_BLACK text STYLE_RESET : text)

◆ BLUE

#define BLUE (   text)    (terminal_supports_color() ? COLOR_BLUE text STYLE_RESET : text)

◆ BOLD_BLACK

#define BOLD_BLACK (   text)    (terminal_supports_color() ? STYLE_BOLD COLOR_BLACK text STYLE_RESET : text)

◆ BOLD_BLUE

#define BOLD_BLUE (   text)    (terminal_supports_color() ? STYLE_BOLD COLOR_BLUE text STYLE_RESET : text)

◆ BOLD_COLORIZE

#define BOLD_COLORIZE (   color,
  text 
)    (terminal_supports_color() ? STYLE_BOLD color text STYLE_RESET : text)

◆ BOLD_CYAN

#define BOLD_CYAN (   text)    (terminal_supports_color() ? STYLE_BOLD COLOR_CYAN text STYLE_RESET : text)

◆ BOLD_GRAY

#define BOLD_GRAY (   text)    (terminal_supports_color() ? STYLE_BOLD COLOR_GRAY text STYLE_RESET : text)

◆ BOLD_GREEN

#define BOLD_GREEN (   text)    (terminal_supports_color() ? STYLE_BOLD COLOR_GREEN text STYLE_RESET : text)

◆ BOLD_MAGENTA

#define BOLD_MAGENTA (   text)    (terminal_supports_color() ? STYLE_BOLD COLOR_MAGENTA text STYLE_RESET : text)

◆ BOLD_RED

#define BOLD_RED (   text)    (terminal_supports_color() ? STYLE_BOLD COLOR_RED text STYLE_RESET : text)

◆ BOLD_WHITE

#define BOLD_WHITE (   text)    (terminal_supports_color() ? STYLE_BOLD COLOR_WHITE text STYLE_RESET : text)

◆ BOLD_YELLOW

#define BOLD_YELLOW (   text)    (terminal_supports_color() ? STYLE_BOLD COLOR_YELLOW text STYLE_RESET : text)

◆ COLOR_BLACK

#define COLOR_BLACK   "\033[30m"

ANSI code for black text

◆ COLOR_BLUE

#define COLOR_BLUE   "\033[34m"

ANSI code for blue text

◆ COLOR_CYAN

#define COLOR_CYAN   "\033[36m"

ANSI code for cyan text

◆ COLOR_GRAY

#define COLOR_GRAY   "\033[90m"

ANSI code for gray (bright black) text

◆ COLOR_GREEN

#define COLOR_GREEN   "\033[32m"

ANSI code for green text

◆ COLOR_MAGENTA

#define COLOR_MAGENTA   "\033[35m"

ANSI code for magenta text

◆ COLOR_RED

#define COLOR_RED   "\033[31m"

ANSI code for red text

◆ COLOR_WHITE

#define COLOR_WHITE   "\033[37m"

ANSI code for white text

◆ COLOR_YELLOW

#define COLOR_YELLOW   "\033[33m"

ANSI code for yellow text

◆ COLORIZE

#define COLORIZE (   color,
  text 
)    (terminal_supports_color() ? color text STYLE_RESET : text)

◆ CYAN

#define CYAN (   text)    (terminal_supports_color() ? COLOR_CYAN text STYLE_RESET : text)

◆ GRAY

#define GRAY (   text)    (terminal_supports_color() ? COLOR_GRAY text STYLE_RESET : text)

◆ GREEN

#define GREEN (   text)    (terminal_supports_color() ? COLOR_GREEN text STYLE_RESET : text)

◆ MAGENTA

#define MAGENTA (   text)    (terminal_supports_color() ? COLOR_MAGENTA text STYLE_RESET : text)

◆ RED

#define RED (   text)    (terminal_supports_color() ? COLOR_RED text STYLE_RESET : text)

◆ STYLE_BOLD

#define STYLE_BOLD   "\033[1m"

Bold text style

◆ STYLE_COLORIZE

#define STYLE_COLORIZE (   style,
  color,
  text 
)    (terminal_supports_color() ? style color text STYLE_RESET : text)

◆ STYLE_RESET

#define STYLE_RESET   "\033[0m"

Reset all styles

◆ STYLE_UNDERLINE

#define STYLE_UNDERLINE   "\033[4m"

Underline text style

◆ UNDERLINE_BLACK

#define UNDERLINE_BLACK (   text)    (terminal_supports_color() ? STYLE_UNDERLINE COLOR_BLACK text STYLE_RESET : text)

◆ UNDERLINE_BLUE

#define UNDERLINE_BLUE (   text)    (terminal_supports_color() ? STYLE_UNDERLINE COLOR_BLUE text STYLE_RESET : text)

◆ UNDERLINE_COLORIZE

#define UNDERLINE_COLORIZE (   color,
  text 
)    (terminal_supports_color() ? STYLE_UNDERLINE color text STYLE_RESET : text)

◆ UNDERLINE_CYAN

#define UNDERLINE_CYAN (   text)    (terminal_supports_color() ? STYLE_UNDERLINE COLOR_CYAN text STYLE_RESET : text)

◆ UNDERLINE_GRAY

#define UNDERLINE_GRAY (   text)    (terminal_supports_color() ? STYLE_UNDERLINE COLOR_GRAY text STYLE_RESET : text)

◆ UNDERLINE_GREEN

#define UNDERLINE_GREEN (   text)    (terminal_supports_color() ? STYLE_UNDERLINE COLOR_GREEN text STYLE_RESET : text)

◆ UNDERLINE_MAGENTA

#define UNDERLINE_MAGENTA (   text)    (terminal_supports_color() ? STYLE_UNDERLINE COLOR_MAGENTA text STYLE_RESET : text)

◆ UNDERLINE_RED

#define UNDERLINE_RED (   text)    (terminal_supports_color() ? STYLE_UNDERLINE COLOR_RED text STYLE_RESET : text)

◆ UNDERLINE_WHITE

#define UNDERLINE_WHITE (   text)    (terminal_supports_color() ? STYLE_UNDERLINE COLOR_WHITE text STYLE_RESET : text)

◆ UNDERLINE_YELLOW

#define UNDERLINE_YELLOW (   text)    (terminal_supports_color() ? STYLE_UNDERLINE COLOR_YELLOW text STYLE_RESET : text)

◆ WHITE

#define WHITE (   text)    (terminal_supports_color() ? COLOR_WHITE text STYLE_RESET : text)

◆ YELLOW

#define YELLOW (   text)    (terminal_supports_color() ? COLOR_YELLOW text STYLE_RESET : text)

Function Documentation

◆ terminal_supports_color()

bool terminal_supports_color ( void  )

Checks if the terminal supports ANSI color escape sequences.

This function should detect whether the current terminal session supports colored output. Common implementations check environment variables, platform, or terminal capabilities.

Returns
true if color output is supported, false otherwise.

Checks if the terminal supports ANSI color escape sequences.

This function determines whether the terminal connected to standard output supports colored output using ANSI escape sequences.

The result is cached after the first call for efficiency.

On Windows:

  • Detects Windows Terminal, VS Code terminal, ConEmu, Cmder via environment variables.
  • Attempts to enable ANSI escape code processing via the console API.
  • Checks Windows version for fallback support.

On Unix/Linux:

  • Checks TERM environment variable for common terminal types supporting color.
  • Uses tput colors command to verify the number of colors supported.
Returns
true if ANSI color output is supported, false otherwise.