How to Get Color in C Program
Changing the color of text or shapes in your C program can help them pop when the user runs your program. Changing the color of your text and objects is a fairly straightforward process, and the necessary functions are included in the...
Part 1 of 2:
Changing Output Text Color
- Include the Standard Input and Output library. This common library allows you to change the color that the text output displays. Add the following code to the top of your program:[1]
#include
- Include the Console Input and Output library. This will make it easier to capture keyboard input from the user. Add the library below the stdio.h library:
#include #include
- Use the textcolor function to define what color you want to use for text. You can use this function to vary the text colors of your output. Colors must be written in all caps, or expressed as a numeral:
#include #include main() { textcolor(RED); // You could type "4" instead of "RED", but it is not as readable }
0
BLUE1
GREEN2
CYAN3
RED4
MAGENTA5
BROWN6
LIGHTGRAY7
DARKGRAY8
LIGHTBLUE9
LIGHTGREEN10
LIGHTCYAN11
LIGHTRED12
LIGHTMAGENTA13
YELLOW14
WHITE15
- There are more colors than this. The colors available depend on the installed graphics drivers and current mode. Colors must be written in all caps.[2]
- Add output text and finish the program. Include a cprintf function to display some text in your new color. Use a getch function at the end to close the program when the user presses a key.
#include #include main() { textcolor(RED); // You could type "4" instead of "RED", but it is not as readable cprintf("Hello, World!"); getch(); return 0; }
Part 2 of 2:
Changing Drawing Color
- Include the graphics library. The C graphics library allows you to draw objects, as well as adjust their color. You can get access to the graphics library by including it at the top of your program:
#include
- Include the Console Input and Output library. You can use this library to easily capture a user's input. Add the library below the graphics.h library:
#include #include
- Initialize the variables for the graphics driver and mode. You'll need to do this before you begin drawing objects, so that the program has access to the system graphics drivers. This will create an area on the screen that the object will be drawn on.
#include #include main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); // Change this to the path of your compiler }
- Set the color of the object you want to draw. Before coding in an object, use the setcolor function to define the color of the object you are about to draw: [3]
#include #include main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); setcolor(BLUE); // You can enter "1" instead of "BLUE" to get the same color, but this is not as readable }
- Draw an object of your choice. For this example, you'll be drawing a rectangle using the rectangle function. You can use any of the graphics.h drawing tools to draw in the color that you set.
#include #include main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); setcolor(BLUE); rectangle(50,50,100,100); // These numbers indicate the location of the left-top and right-bottom corners }
- Finish off the program and test it. Add the getch command and turn off the graphics area as you close the program. Compile it and give it a test run.
#include #include main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); setcolor(BLUE); rectangle(50,50,100,100); getch(); closegraph(); return 0; }
4.3 ★ | 3 Vote
You should read it
- The terminology of color, wheel color and skill used to color
- How to Set Your Desktop Color on a Mac
- ATI Radeon HD 5700: Cheap DX11 graphics card for gamers
- Work with the library item in Adobe Dreamweaver CS6
- How to Get the Hex Code of a Color on Your Computer Screen
- How to Color Text
- The 6 best graphics cards for your all-white gaming PC
- How to Change Color Saturation on Windows 10
- What is the profile color?
- The difference in the Color Profile mode of the photos
- How to Have Fun at the Library (for Kids and Teens)
- CSS color coding, standard color code in website design
Maybe you are interested
What is computer hardware? What parts does hardware include?
Learning from Threads, TikTok allows posting posts that only include text
Discover all the Titans included in Attack on Titan
Oxygen Not Included tips for beginners
How to include shapes in PowerPoint
Official: Ubuntu 19.10 includes Nvidia driver in ISO