banner



How To Change The Color In C#

Calculation Color to Your Programs

Adding ColorCalculation color to the output of your programs can brand them more than visually highly-seasoned and adding to their aesthetics. Not only this, though, using color will make notices or alert more noticeable to your users resulting in ameliorate acceptance. In this blog we volition see how to can both add color and starting time making apply of additional functions in C, rather than, using soley the main function. And then when you are fix and sabbatum comfortably in your chair nosotros shall brainstorm.

ANSI Colors

We have 8 ANSI colors that nosotros can use in our output, this can be doubled to 16 if you take into consideration that these colors can be displayed every bit standard or in assuming for highlighting. To be able to access the colors we need to use and escape sequence followed by the right colour code, the print the text and finally reset the colors.

  • printf("\033[0;31m"); //Set the text to the color red
  • printf("Hello\due north"); //Display How-do-you-do in cherry
  • printf("\033[0m"); //Resets the text to default colour
  • Escape is: \033
  • Colour lawmaking is: [0;31m

It is important to reset the colour to ensuring that the selected colour is terminated and text returns to normal. Using the following tabular array y'all tin can view some of the code available.

Simple Hello World in Color

Working with a unproblematic hello world program nosotros tin can begin to understand how to brand utilize of the color. Firstly we will set the color to be red and bold earlier moving onto using functions to fix the color.

#include <stdio.h> int main () { printf("\033[ane;31m"); printf("Howdy world\n"); printf("\033[0m") return 0; }

For Bold simply replace the initial 0 before the ; with a ane. BOLD dark-green would exist\033[1;32m

  • Black \033[0;30m
  • Red \033[0;31m
  • Green \033[0;32m
  • Yellow \033[0;33m
  • Bluish \033[0;34m
  • Purple \033[0;35m
  • Cyan \033[0;36m
  • White \033[0;37m

Adding color to the output was really quite simple; however setting many colors or changing the colors many times will be repetitive. Setting the color oftentimes and using more than one color is going to be required where we want to highlight the output with information and warnings.

Using Functions

This is where functions can help. It is simple to create functions for ruby, yellow etc. Permit'southward take a look, assuming nosotros create the source file helloc.c.

#include <stdio.h> void red () {   printf("\033[1;31m"); } void yellowish {   printf("\033[one;33m"); } void reset () {   printf("\033[0m"); } int main () {   ruby-red();   printf("How-do-you-do ");   yellow();   printf("world\northward");   reset();   return 0; }
$ gcc -Wall -o helloc helloc.c                $ ./helloc                                                  Hello                                                                    World!                              

We can use the newly created functions as many time as nosotros want and it is as uncomplicated as yellow(); reset(); or blood-red(); Nosotros, of grade, can create more than functions to back up all colors.

Using the functions the manner we have we practice not demand to return whatsoever values. The functions effect simply in printing the ANSI colour codes to the terminal. Every bit we do not return whatsoever value and then we fix the output parameter explicitly to void.

Moving On

It is also likely that we can reuse these part in nigh any C plan that has text output. In another blog, we will see how nosotros tin can reuse this code by creating and referencing our ain header files.

The following video steps you through the process of press in colour.

How To Change The Color In C#,

Source: https://www.theurbanpenguin.com/4184-2/

Posted by: lealsoms1959.blogspot.com

0 Response to "How To Change The Color In C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel