3
\$\begingroup\$

I have implemented an user defined colors enumerator, and if is possible I appreciate if I will get a code review. I want to know if my implementation is correctly or must improve it. If there exist a better alternative (color enumerator) to my code which is?

Usage example:

 HANDLE hConsole;
 setTextColor(hConsole,YELLOW_TEXT|PINK_BACKGROUND);
 std::cout << "YELLOW_TEXT|PINK_BACKGROUND";
 resetTextColor(hConsole);

Colors enumerator:

 enum COLOR
 {
     // Text foreground colors
     // Standard text colors
     GRAY_TEXT=8,      BLUE_TEXT,       GREEN_TEXT,
     TEAL_TEXT,        RED_TEXT,        PINK_TEXT,
     YELLOW_TEXT,      WHITE_TEXT,
     // Faded text colors
     BLACK_TEXT=0,     BLUE_FADE_TEXT,  GREEN_FADE_TEXT,
     TEAL_FADE_TEXT,   RED_FADE_TEXT,   PINK_FADE_TEXT,
     YELLOW_FADE_TEXT, WHITE_FADE_TEXT,
     // Standard text background color
     GRAY_BACKGROUND=GRAY_TEXT<<4,     BLUE_BACKGROUND=BLUE_TEXT<<4,
     GREEN_BACKGROUND=GREEN_TEXT<<4,   TEAL_BACKGROUND=TEAL_TEXT<<4,
     RED_BACKGROUND=RED_TEXT<<4,       PINK_BACKGROUND=PINK_TEXT<<4,
     YELLOW_BACKGROUND=YELLOW_TEXT<<4, WHITE_BACKGROUND=WHITE_TEXT<<4,
     // Faded text background color
     BLACK_BACKGROUND=BLACK_TEXT<<4,           BLUE_FADE_BACKGROUND=BLUE_FADE_TEXT<<4,
     GREEN_FADE_BACKGROUND=GREEN_FADE_TEXT<<4, TEAL_FADE_BACKGROUND=TEAL_FADE_TEXT<<4,
     RED_FADE_BACKGROUND=RED_FADE_TEXT<<4,       PINK_FADE_BACKGROUND=PINK_FADE_TEXT<<4,
     YELLOW_FADE_BACKGROUND=YELLOW_FADE_TEXT<<4, WHITE_FADE_BACKGROUND=WHITE_FADE_TEXT<<4
 };

Set and reset functions:

  BOOL resetTextColor(HANDLE h)
  {
      return SetConsoleTextAttribute(h,WHITE_FADE_TEXT);
  }

  BOOL setTextColor(HANDLE h, WORD c)
  {
      return SetConsoleTextAttribute(h,c);
  }

Capture: Program output

\$\endgroup\$
1

1 Answer 1

3
\$\begingroup\$

If there exist a better alternative (color enumerator) to my code which is?

You should use the constants defined in a Windows header file named Wincon.h, which are as follows:

#define FOREGROUND_BLUE      0x0001 // text color contains blue.
#define FOREGROUND_GREEN     0x0002 // text color contains green.
#define FOREGROUND_RED       0x0004 // text color contains red.
#define FOREGROUND_INTENSITY 0x0008 // text color is intensified.
#define BACKGROUND_BLUE      0x0010 // background color contains blue.
#define BACKGROUND_GREEN     0x0020 // background color contains green.
#define BACKGROUND_RED       0x0040 // background color contains red.
#define BACKGROUND_INTENSITY 0x0080 // background color is intensified.

For example, you could write:

enum COLOR
{
    // Text foreground colors
    // Standard text colors
    GRAY_TEXT=FOREGROUND_INTENSITY,
    BLUE_TEXT=FOREGROUND_BLUE,

The answers to C++ Win32 Console Color should be useful to you.

Alternatively here's a copy-and-paste of the equivalent .NET enum for C#, which I believe uses the same bit-values and assigns conventional color-names:

#region Assembly mscorlib.dll, v4.0.30319
// C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\mscorlib.dll
#endregion

namespace System
{
    // Summary:
    //     Specifies constants that define foreground and background colors for the
    //     console.
    [Serializable]
    public enum ConsoleColor
    {
        // Summary:
        //     The color black.
        Black = 0,
        //
        // Summary:
        //     The color dark blue.
        DarkBlue = 1,
        //
        // Summary:
        //     The color dark green.
        DarkGreen = 2,
        //
        // Summary:
        //     The color dark cyan (dark blue-green).
        DarkCyan = 3,
        //
        // Summary:
        //     The color dark red.
        DarkRed = 4,
        //
        // Summary:
        //     The color dark magenta (dark purplish-red).
        DarkMagenta = 5,
        //
        // Summary:
        //     The color dark yellow (ochre).
        DarkYellow = 6,
        //
        // Summary:
        //     The color gray.
        Gray = 7,
        //
        // Summary:
        //     The color dark gray.
        DarkGray = 8,
        //
        // Summary:
        //     The color blue.
        Blue = 9,
        //
        // Summary:
        //     The color green.
        Green = 10,
        //
        // Summary:
        //     The color cyan (blue-green).
        Cyan = 11,
        //
        // Summary:
        //     The color red.
        Red = 12,
        //
        // Summary:
        //     The color magenta (purplish-red).
        Magenta = 13,
        //
        // Summary:
        //     The color yellow.
        Yellow = 14,
        //
        // Summary:
        //     The color white.
        White = 15,
    }
}

If you're going to define your own enum then use it in your function declaration, i.e.:

BOOL setTextColor(HANDLE h, COLOR c)

... not ...

BOOL setTextColor(HANDLE h, WORD c)

I'd usually use mixed-case e.g. Color instead of COLOR for an enum; and I'd worry that it's already been defined in a header file.

Also I'd split it into two parameters instead of doubling the number of enum values, for example:

BOOL setTextColor(HANDLE h, ConsoleColor foregound, ConsoleColor background)
{
    WORD color = foregound | (background << 4);
    return SetConsoleTextAttribute(h, color);
}
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.