It works by overriding std::streambuf to intercept what you print to std::cout and decorate it with ANSI escape codes for colored text
- **Rainbow **: Automatically cycles through colors for each space-separated word
- Specific Colors: Set output to any supported color by name
Simply include the header file in your C++ project:
#include "ColoredBuffer.hpp"
// Create ColoredBuffer instance and wrapper
ColoredBuffer rb(std::cout.rdbuf());
wrapper guard(rb);
// Now all std::cout output will be colored!// Enable rainbow cycling (changes color on each space)
rb.rainbow();
// Set a specific color by name
rb.set_color("blue");
rb.set_color("green");
// Disable coloring (resets to default)
rb.disable();
// Show available colors
rb.check_avaiable_colors();
#include <iostream>
#include "ColoredBuffer.hpp"
int main() {
ColoredBuffer rb(std::cout.rdbuf());
wrapper guard(rb);
rb.rainbow();
std::cout << "This text cycles through rainbow colors word by word!\n";
rb.disable();
rb.set_color("yellow");
std::cout << "Now printing in yellow.\n";
rb.set_color("cyan");
std::cout << "And now in cyan.\n";
rb.set_color("darkgreen"); // not supported -> shows available colors
}- red
- green
- yellow
- blue
- magenta
- cyan
You can easily add more colors by extending the colors vector with new Color entries by just adding a name aswell its corresponding ANSI Color Code
instead of using a vector to store the Colors, consider using a Enum. trying to access colors that are not present will now be a compile time error.
- basic understanding on how iostream/streams really work
- why RAII wrappers are important
- using error codes can be more useful than writing methods - like i did in check_avaiable_colors

