C++ gurus and learners, we program ourselves :P all work is CC licensed

Tuesday, November 19

Easy Colouring

As promissed here is bit easy technique for those colour commands;
Hope you guys understand it :
Theme here is to use funcitons to determin command stuff i.e. how much easy would it be if you only have to type 

color(r); //red colour

here is the function that will help you to use colours easily
______________________________________________________________________

  1. HANDLE Output=GetStdHandle(STD_OUTPUT_HANDLE);
  2. void colour(int x)
  3. {SetTextAttribute(Output,x);}
______________________________________________________________________
here if you remember colour codes integers just use them as follows:

colour(10);      //green


and if you want to make it runing upon character values you can place switch in it

  1. HANDLE Output=GetStdHandle(STD_OUTPUT_HANDLE);
  2. void colour(char ch)
  3. {
  4. int x;
  5. switch (ch)
  6. {
  7. case 'r':  x=12; break;                       //red
  8. case 'b': x=9;   break;                        //blue
  9. case 'g': x=10; break;                        //green
  10. case 'a': x=11; break;                        //aqua
  11. case 'v': x=13; break;                         //voilet
  12. case 'y': x=14; break;                         //yellow
  13. case 'w: x=15; break;                         //white
  14. }
  15.  SetTextAttribute(Output,x);
  16. }

now you can call it as this;
colour('r');                    //red colour
end of post;




3 comments :

  1. Dont U Think It is SetConsoleTextAttribute ,,,,,,,

    ReplyDelete
  2. 2 stars already for this post and no explanation needed in comments :)

    ReplyDelete
  3. these are two 5 stars from community ++ :)

    ReplyDelete