Advertisement
ohusq

C++ coloured #define with RGB function

Apr 29th, 2024
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. // Color Attributes for SetConsoleTextAttribute()
  2. #define BLACK         0
  3. #define DARK_BLUE     FOREGROUND_BLUE
  4. #define DARK_GREEN    FOREGROUND_GREEN
  5. #define DARK_CYAN     (FOREGROUND_GREEN | FOREGROUND_BLUE)
  6. #define DARK_RED      FOREGROUND_RED
  7. #define DARK_MAGENTA  (FOREGROUND_RED | FOREGROUND_BLUE)
  8. #define DARK_YELLOW   (FOREGROUND_RED | FOREGROUND_GREEN)
  9. #define GRAY          (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
  10. #define DARK_GRAY     (FOREGROUND_INTENSITY)
  11. #define BLUE          (FOREGROUND_BLUE | FOREGROUND_INTENSITY)
  12. #define GREEN         (FOREGROUND_GREEN | FOREGROUND_INTENSITY)
  13. #define CYAN          (FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
  14. #define RED           (FOREGROUND_RED | FOREGROUND_INTENSITY)
  15. #define MAGENTA       (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
  16. #define YELLOW        (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY)
  17. #define WHITE         (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
  18.  
  19.  
  20. #include <windows.h>
  21.  
  22. // Function prototype
  23. void SetColorRGB(int r, int g, int b);
  24.  
  25. int main() {
  26.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  27.    
  28.     // Example usage: Set to a color with full red and green components (yellow)
  29.     SetColorRGB(255, 255, 0);
  30.  
  31.     printf("This text is in custom color based on RGB input.\n");
  32.  
  33.     // Reset to default attributes
  34.     SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
  35.  
  36.     return 0;
  37. }
  38.  
  39. // Function definition
  40. void SetColorRGB(int r, int g, int b) {
  41.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  42.     WORD color = 0;
  43.  
  44.     // Red component
  45.     if (r > 128) color |= FOREGROUND_RED;
  46.     // Green component
  47.     if (g > 128) color |= FOREGROUND_GREEN;
  48.     // Blue component
  49.     if (b > 128) color |= FOREGROUND_BLUE;
  50.  
  51.     // Adjust intensity
  52.     if (r > 192 || g > 192 || b > 192) color |= FOREGROUND_INTENSITY;
  53.  
  54.     SetConsoleTextAttribute(hConsole, color);
  55. }
  56.  
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement