/********************************************************************************************************************** * * * @file ansi_escapes.h * * * * @brief Defines constants and functions for working with screen colors. * * @copyright Copyright (C) 2023 by Tom Hicks * * * * Licensed under the MIT license see below for details. * * * * MIT License * * * * Copyright (c) 2023 Tom Hicks * * * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * * documentation files (the "Software"), to deal in the Software without restriction, including without limitation * * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and * * to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of * * the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO * * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * * IN THE SOFTWARE. * * * **********************************************************************************************************************/ #ifndef CPP_Utils__ansi_escapes_h__ #define CPP_Utils__ansi_escapes_h__ #include #include #include /** \addtogroup Ansi Escape Sequences * @{ */ namespace CPPUtils { constexpr uint8_t GetRedComponent(uint32_t color) { return (color & 0x00FF0000) >> 16; } constexpr uint8_t GetGreenComponent(uint32_t color) { return (color & 0x0000FF00) >> 8; } constexpr uint8_t GetBlueComponent(uint32_t color) { return (color & 0x000000FF); } template auto& Escape(std::basic_ostream& os, const std::basic_string& escape_code) { return os << "\033[" << escape_code << "m"; } template auto& Escape(std::basic_ostream& os, const std::basic_string_view& escape_code) { return os << "\033[" << escape_code << "m"; } template auto& Escape(std::basic_ostream& os, const TChar* escape_code) { return os << "\033[" << escape_code << "m"; } template auto& ForegroundColor8Bit(std::basic_ostream& os, uint8_t color) { return Escape(os, "38;5;" + std::to_string(color)); } template auto& BackgroundColor8Bit(std::basic_ostream& os, uint8_t color) { return Escape(os, "48;5;" + std::to_string(color)); } template auto& ForegroundTrueColor(std::basic_ostream& os, uint8_t red, uint8_t green, uint8_t blue) { return os << "\033[" << "38;2;" << (uint16_t)red << ";" << (uint16_t)green << ";" << (uint16_t)blue << "m"; } template auto& ForegroundTrueColor(std::basic_ostream& os, uint32_t color) { return ForegroundTrueColor(os, GetRedComponent(color), GetGreenComponent(color), GetBlueComponent(color)); } template auto& BackgroundTrueColor(std::basic_ostream& os, uint8_t red, uint8_t green, uint8_t blue) { return os << "\033[" << "48;2;" << (uint16_t)red << ";" << (uint16_t)green << ";" << (uint16_t)blue << "m"; } template auto& BackgroundTrueColor(std::basic_ostream& os, uint32_t color) { return BackgroundTrueColor(os, GetRedComponent(color), GetGreenComponent(color), GetBlueComponent(color)); } template auto& Reset(std::basic_ostream& os) { return os << "\033[" << "m"; } } // End namespace CPPUtils /** @}*/ #endif // End !defined CPP_Utils__ansi_escapes_h__