How to Get Color in C Program

Part 1 of 2:

Changing Output Text Color

  1. How to Get Color in C Program Picture 1
    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 
  2. How to Get Color in C Program Picture 2
    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 
  3. How to Get Color in C Program Picture 3
    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 } 

     

    ColorNumerical ValueBLACK

    0

    BLUE

    1

    GREEN

    2

    CYAN

    3

    RED

    4

    MAGENTA

    5

    BROWN

    6

    LIGHTGRAY

    7

    DARKGRAY

    8

    LIGHTBLUE

    9

    LIGHTGREEN

    10

    LIGHTCYAN

    11

    LIGHTRED

    12

    LIGHTMAGENTA

    13

    YELLOW

    14

    WHITE

    15

    1. 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]
  4. How to Get Color in C Program Picture 4
    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

  1. How to Get Color in C Program Picture 5
    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 
  2. How to Get Color in C Program Picture 6
    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 
  3. How to Get Color in C Program Picture 7
    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 } 
  4. How to Get Color in C Program Picture 8
    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 } 
  5. How to Get Color in C Program Picture 9
    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 } 
  6. How to Get Color in C Program Picture 10
    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

May be interested

  • How to Get the Hex Code of a Color on Your Computer ScreenHow to Get the Hex Code of a Color on Your Computer Screen
    colors are identified in html and css by their hexadecimal codes. if you're creating a webpage or other html project and want to include an element that matches a particular color in an image, website, or program window on your computer...
  • How to Color TextHow to Color Text
    this wikihow teaches you how to change text color in a document. popular document programs include microsoft office programs, google docs, and apple's pages program. if you're interested in coding your own colored text, you can do so using...
  • What is color space?What is color space?
    whenever you take a photo, the digital camera's sensor records information about colors from the outside world. what most people don't know is that you can choose how much color detail the camera will record.
  • Each person's eye color is different from anyone else's, why is that?Each person's eye color is different from anyone else's, why is that?
    eye color is the color of the iris, the colored part around the pupil (the small black spot in the center of the eye). there is an interesting fact that each person has a unique eye color, similar to fingerprints.
  • 4 apps that change hair color in photos on iPhone4 apps that change hair color in photos on iPhone
    sometimes changing hair color makes a difference, but how to know which hair color is right for you. there are many applications on the app store that can help you try new colors before going to the beauty salon.
  • How to Change iMessage ColorHow to Change iMessage Color
    apple's imessage is an easy to use app that many iphone owners use to communicate. it is not, however, the most easily customizable app. despite that, you do have a few available options if you would like to customize the colors of the...
  • Tools to get colors online quicklyTools to get colors online quickly
    there are many online tools to help you find palettes, or create your own color palettes. here are a few suggestions.
  • The secret to choosing dyed hair color that matches skin colorThe secret to choosing dyed hair color that matches skin color
    choosing the right hair color for your skin color is quite difficult. however, if you know which hair color is 'standard' for your skin tone, everything will be simple and effective.
  • Color command in WindowsColor command in Windows
    the color command changes the foreground and background colors in the command prompt window for the current session. if used without parameters, the color command restores the default foreground and background colors of the command prompt window.
  • How to color images in PhotoshopHow to color images in Photoshop
    on photoshop, there is a simple color filler and photo filler tool.