遍历常量枚举#define

loop through constant enum #define

我的 C++ 代码中有一个 const 枚举,我想知道我是否可以循环遍历这些颜色,例如对这个枚举的每个成员的整数引用

const enum Colors
{
#define WHITE(alpha)            D3DCOLOR_ARGB(alpha, 255, 255, 255)//
#define RED(alpha)              D3DCOLOR_ARGB(alpha, 255, 000, 000)//
#define GREEN(alpha)            D3DCOLOR_ARGB(alpha, 000, 255, 000)//
#define BLUE(alpha)             D3DCOLOR_ARGB(alpha, 000, 000, 255)//
#define BLACK(alpha)            D3DCOLOR_ARGB(alpha, 000, 000, 000)//
#define PURPLE(alpha)           D3DCOLOR_ARGB(alpha, 125, 000, 255)//
#define GREY(alpha)             D3DCOLOR_ARGB(alpha, 44,44, 46)    //
#define YELLOW(alpha)           D3DCOLOR_ARGB(alpha, 255, 255, 000)//
#define ORANGE(alpha)           D3DCOLOR_ARGB(alpha, 255, 165, 000)//
#define DEEPSKYBLUE(alpha)      D3DCOLOR_ARGB(alpha, 30, 144, 255) //
#define CHOCOLATE2(alpha)       D3DCOLOR_ARGB(alpha, 238, 118, 33) //
};

此列表不完整 -> 我的枚举中有很多颜色

所以我真的想知道我是否可以使用可以循环遍历此枚举的键盘快捷键循环遍历所有颜色...

#define WHITE(alpha)            D3DCOLOR_ARGB(alpha, 255, 255, 255) = 1//<< something like that

我试过了,但不可能...

首先,你不应该混合使用 #define 宏和 enums - 它们是完全不同的东西,你的代码等于 -

// Why was there `const`..?
enum Colors
{
    /* empty enum */
};

#define WHITE(alpha)            D3DCOLOR_ARGB(alpha, 255, 255, 255)//
#define RED(alpha)              D3DCOLOR_ARGB(alpha, 255, 000, 000)//
#define GREEN(alpha)            D3DCOLOR_ARGB(alpha, 000, 255, 000)//
#define BLUE(alpha)             D3DCOLOR_ARGB(alpha, 000, 000, 255)//
#define BLACK(alpha)            D3DCOLOR_ARGB(alpha, 000, 000, 000)//
#define PURPLE(alpha)           D3DCOLOR_ARGB(alpha, 125, 000, 255)//
#define GREY(alpha)             D3DCOLOR_ARGB(alpha, 44,44, 46)    //
#define YELLOW(alpha)           D3DCOLOR_ARGB(alpha, 255, 255, 000)//
#define ORANGE(alpha)           D3DCOLOR_ARGB(alpha, 255, 165, 000)//
#define DEEPSKYBLUE(alpha)      D3DCOLOR_ARGB(alpha, 30, 144, 255) //
#define CHOCOLATE2(alpha)       D3DCOLOR_ARGB(alpha, 238, 118, 33) //

所以,从现在开始,我将忽略 enum Colors 并谈论那些宏。


我建议你将值存储到数组中。

COLOR_TYPE colors[] = {
    WHITE(0),
    RED(0),
    ...
};

请注意 alpha 为零。由于数组只能存储值(不是宏函数),我应该用一些东西修复 alpha

然后,你可以这样使用它:

color[42] & (alpha << 24)

如果觉得乱,可以再做一个宏

#define GET_COLOR(n, alpha) (color[(n)] & ((alpha) << 24))

或内联函数(..推荐)

inline COLOR_TYPE GetColor(int n, uint8_t alpha)
{
    return color[n] & (alpha << 24);
}

我找到了一种方法,但我不知道它是否好:

首先我做了一个颜色class如下:

class MyColor
{
public:
    string name;
    int a, r, g, b;
    D3DCOLOR color;
    MyColor();
    MyColor(string name, int a, int r, int g, int b);
    void changeAlpha(int a);
};

MyColor::MyColor()
{

}
MyColor::MyColor(string name, int a, int r, int g, int b)
{
    this->name = name;
    this->a = a;
    this->r = r;
    this->g = g;
    this->b = b;
};

void MyColor::changeAlpha(int a)
{

    this->color = D3DCOLOR_ARGB(a, r, g, b);
}

然后我将所有定义复制到声明如下的向量中:

vector<MyColor*> colorArray
{
    new MyColor("WHITE", 255, 255, 255, 255),
    new MyColor("RED", 255, 255, 000, 000),
    new MyColor("GREEN", 255, 000, 255, 000),
    new MyColor("BLUE", 255, 000, 000, 255),
    new MyColor("BLACK", 255, 000, 000, 000),

    new MyColor("PURPLE", 255, 125, 000, 255),
    new MyColor("GREY", 255, 44, 44, 46),
    new MyColor("YELLOW", 255, 255, 255, 000),
    new MyColor("ORANGE", 255, 255, 165, 000),
    new MyColor("DEEPSKYBLUE", 255, 30, 144, 255),
    new MyColor("GOLD2", 255, 238, 201, 0),
    new MyColor("SQUA", 255, 0, 255, 255),
    new MyColor("DARKGREY", 255, 62, 62, 62),
    //... not all is here ;)
};

我添加了一些静态方法来按名称查找颜色并将 alpha 设置为它,如下所示:

static MyColor* findColor(string colorName, int ALPHA)
{
    for (MyColor* COLOR : colorArray)
    {
        if (COLOR->name == colorName)
        {

            COLOR->a = ALPHA;
            COLOR->color = D3DCOLOR_ARGB(COLOR->a, COLOR->r, COLOR->g, COLOR->b);
            return COLOR;

        }
    }
    return new  MyColor("ElectricLime", 255, 204, 255, 000);
}

最后在我的 classes 中我需要使用它们的地方我可以这样称呼它们

//button constructor
    Button::Button(string text, string name, XMFLOAT2 position, XMFLOAT2 size, CallbackFunction callback)
    {
        this->position = position;
        this->bounds = size;
        this->text = text;
        this->name = name;
        this->callback = callback;
        this->backColor = findColor("BLACK", 255);

        this->textColor = findColor("BLUE", 255);

        this->borderColor = findColor("BLUE", 255);
    }

所以我真的很想问这是否是一个好的解决方案,但现在它完成了工作,我可以循环浏览我的颜色...