Advertisement
xth

win32_irodelta.cpp

xth
Mar 21st, 2020
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3.  
  4. /* irodelta.cpp sets a new console color without modifying previous text */
  5.  
  6. void setConsoleColour(unsigned short colour)
  7. {
  8.         static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  9.         std::cout.flush();
  10.         SetConsoleTextAttribute(hOut, colour);
  11. }
  12.  
  13. int main(int argc, char **argv)
  14. {
  15.  
  16.     if (argc <= 1)
  17.     {
  18.         if (argv[0])
  19.             std::cout << "Usage: " << argv[0] << " <1 or 2-digit hexadecimal number (FOREGROUND BACKGROUND)>" << '\n';
  20.         else
  21.             std::cout << "Usage: <win32_irodelta.exe> <1 or 2-digit hexadecimal number (FOREGROUND BACKGROUND)>" << '\n';
  22.  
  23.         std::exit(1);
  24.     }
  25.     char *input = argv[1];
  26.     char a = input[0];
  27.     char b = input[1];
  28.     unsigned short foreground = 0; // 0-15
  29.     unsigned short background = 0; // multiple of 16
  30.     if (a>=65 && a<=70)/*ASCII for A-F -> 10-15*/{
  31.         foreground = a-55;
  32.     } else if (a>=97 && a<=102)/*ASCII for a-f -> 10-15*/{
  33.         foreground = a-87;
  34.     } else if (a>=48 && a <= 57)/*ASCII for 0-9*/{
  35.         foreground = a-48;
  36.     }
  37.  
  38.     if (b>=65 && b<=70){
  39.         background = (b-55)*16;
  40.     } else if (b>=97 && b<=102){
  41.         background = (b-87)*16;
  42.     } else if (b>=48 && b <= 57){
  43.         background = (b-48)*16;
  44.     }
  45.  
  46.     if (background!=(foreground*16)){
  47.             setConsoleColour(foreground | background);
  48.     } else {
  49.             std::cout << "Error: First argument cannot have same foreground and background" << '\n';
  50.     }
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement