繁体   English   中英

遍历常量枚举#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宏和enum这是完全不同的东西,并且您的代码等于-

// 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 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);
}

最后在需要使用它们的班级上,我可以像这样称呼他们

//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);
    }

所以我真的想问一下这是否是一个好的解决方案,但是现在它可以工作了,我可以循环显示我的颜色...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM