This library attempts to provide a single include header solution to easily printing in color at the terminal using C. This project is a personal proof-of-concept that I developed out of my own curiosity, and it has not been rigorously tested.
TODO; This documentation is incomplete
This header provides an implementation for the set of 4-bit ANSI color escape codes, in the form of a struct (Color) containing const char *
representations of all of the codes required for constructing Background ; Foreground color pairs.
There are several primary functions included in the header.
int cprintf(const char *format, ...);
- A wrapper for
cvfprintf()
which passesstdout
along with the providedva_list
and format string tocvfprintf()
- The equivalent of
printf()
, but with color!
- A wrapper for
ColorCode Colors(char *foreground, char *background);
- This function constructs a foreground;background color code pair representing the two colors (or instructions) provided. For instance, a call of
Colors("red", "reset");
will return a ColorCode struct with the values:
- This function constructs a foreground;background color code pair representing the two colors (or instructions) provided. For instance, a call of
ColorCode {
fg = "red";
bg = "reset";
code = "\x1b[31;49m";
}
ColorCode singleColor(char *color, int isFG);
- this function accepts a string representation of a color or instruction, as well as a boolean
isFG
which indicates to the function whether to pick from the foreground or background palette. The return is a ColorCode struct containing only the selected color code (leaving the other member occupied by an empty string).
- this function accepts a string representation of a color or instruction, as well as a boolean
All functions which return a ColorCode struct will return the following value if the color could not be found:
return (ColorCode){ "\0", "\0", "\0" };
The included makefile will build, test, install, and uninstall the library. To run a test, execute:
make test
in your terminal of choice. You should see the test program run with color!
To use this library systemwide, run sudo make install
(only supported on Linux). For example, let's compile the below program:
#include <stdio.h>
#include <colors.h>
// color-example.c
int main() {
// Background color names begin with an underscore
cprintf("&(_br_yellow)&&(blue)&Hello World!\n");
}
With the shared library installation, we can compile the above using:
gcc -Wall -Werror color-example.c -lcolors