0

Problem:
I have a structure that represent RGB colors. I would like to define an enum to be able to directly assign the colors to a variable.

1) Desired enum of struct:
What I would like is to define an enum that directly maps a color name to its value, just like I would do with an integer parameter or a list of items.

#include <iostream>
#include <array>

typedef struct _Color_3u8
{
    std::array<unsigned char,3> rgb;
} Color_3u8;

std::ostream& operator<<(std::ostream& os, const Color_3u8& dt)
{
    os << "R: " << (int)dt.rgb[0] << " | G: " << (int)dt.rgb[1] << " | B: " << (int)dt.rgb[2] << "\n";
    return os;
}

typedef enum _Default_color
{
    RED = (Color_3u8){255,0,0},
    GREEN = (Color_3u8){0,255,0},
    BLUE = (Color_3u8){0,0,255},
} Default_color;

int main()
{
    Color_3u8 my_color = Default_color::RED;
    std::cout << my_color;
    return 0;
}

Now, the code above clearly doesn't work because enums are limited to integers.

2) constant array + enum:
One way to make it work is to use a constant array filled with the vaules and use the enum to index that array. It looks a lot clunkier since there are two separate definitions.

#include <iostream>
#include <array>

typedef struct _Color_3u8
{
    std::array<unsigned char,3> rgb;
} Color_3u8;

std::ostream& operator<<(std::ostream& os, const Color_3u8& dt)
{
    os << "R: " << (int)dt.rgb[0] << " | G: " << (int)dt.rgb[1] << " | B: " << (int)dt.rgb[2] << "\n";
    return os;
}

typedef enum _Default_color
{
    RED = 0,
    GREEN = 1,
    BLUE = 2,
    NUM_COLORS = 3,
} Default_color;


const std::array<Color_3u8,Default_color::NUM_COLORS> c_default_colors=
{
    (Color_3u8){255,0,0},
    (Color_3u8){0,255,0},
    (Color_3u8){0,0,255},
};

int main()
{
    Color_3u8 my_color = c_default_colors[Default_color::BLUE];
    std::cout << my_color;
    return 0;
}

Output: "R: 0 | G: 0 | B: 255"

3) struct class:
Another way I can think of is to move the constant array from code 2) inside the structure and add a method to initialize the structure to a default color. It would work and it would be very clean, but like the 2), it would be more effort to add a color than I feels is necessary... I feel there should be a way to make code 1) work in some form.

4) Hack struct into integer
In this case since my structure is three bytes, I can convert a color to its integer representation and use that for the enum. I would like to find a way that works with structures bigger than an int as well.

Solution) namespace+constexpr
constexpr is used to declare a compile time variable ofthe right type with the right constructor. namespace wraps around the definitions and scope them.

struct Color_3u8
{
    std::array<unsigned char,3> rgb;
};

std::ostream& operator<<(std::ostream& os, const Color_3u8& dt)
{
    os << "R: " << (int)dt.rgb[0] << " | G: " << (int)dt.rgb[1] << " | B: " << (int)dt.rgb[2] << "\n";
    return os;
}

namespace Default_color
{
    constexpr Color_3u8 RED{255,0,0};
    constexpr Color_3u8 GREEN{0,255,0};
    constexpr Color_3u8 BLUE{0,0,255};
};

int main()
{
    Color_3u8 my_color = Default_color::RED;
    std::cout << my_color;
    return 0;
}

Question
Is there a way in C++ to make something similar to code 1)? I would like to map a name to a structure content with just one entry in one table, like the enum does.

2 Answers 2

4

I would use inline constexpr variables and wrap them in a namespace. That would look like

namespace Default_colors
{
    inline constexpr Color_3u8 RED{255,0,0};
    inline constexpr Color_3u8 GREEN{0,255,0};
    inline constexpr Color_3u8 BLUE{0,0,255};
}

and then you would use it like

Default_colors::RED

The reason to use inline variables here is so you can declare them in a header file and not have to worry about defining them in a single translation unit.


Also note that in C++, there is no reason to do

typedef struct _Color_3u8
{
    std::array<unsigned char,3> rgb;
} Color_3u8;

like you have to do in C. In C++, you can just use

struct Color_3u8
{
    std::array<unsigned char,3> rgb;
};
1
  • This works, thanks! There is a typo constexp -> constexpr. Feb 3, 2021 at 11:03
0

What if I told you that you can store information about all the three colors in a single variable? Will you believe me? Well this is how,
If you know about binary, if we OR something, it adds the '1' bit to a certain location (in binary). This means that we can assign a binary value to each color, and by OR-ing it, we can store the data about the color.

#define BIT_SHIFT(x)    (1 << x)

enum Colors {
    Red = BIR_SHIFT(0),    // Which is gonna be 1.
    Green = BIR_SHIFT(1),    // Which is gonna be 2.
    Blue = BIR_SHIFT(2),    // Which is gonna be 4.
};

Now we can have a variable to store the colors,

int colors = Colors::Red | Colors::Green;

Now when were gonna print, we just need to AND the color to check if its there.

std::ostream& operator<<(std::ostream& os, const Color_3u8& dt)
{
    // Red color value.
    if(colors & Colors::Red)
        os << "R: 255 |";
    else 
        os << "R: 0 |"

    // Green color value.
    if(colors & Colors::Green)
        os << "G: 255 |";
    else 
        os << "G: 0 |"

    // Blue color value.
    if(colors & Colors::Blue)
        os << "B: 255\n";
    else 
        os << "B: 0\n"

    return os;
}

One downside of using this is that you cant store the individual color values (meaning its gonna be either 0 or 255, which I came to conclusion using the example). But on the plus size, you only need one variable to get it done.

1
  • 1
    I think this bithacking is more useful in microcontrollers with limited memory footprint, here I was looking for a way to do it 'properly'. Thanks anyway. Feb 3, 2021 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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