0

i have a const enum in my c++ code and i wanna know if i could loop through those colors with by example an integer reference to each member of this enum

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

this list is not complete -> i have lot of colors in my enum

so i really wonder if i can loop through all the colors with a keyboard shortcut that can loop through this enum...

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

i tried that but it is not possible...

3
  • 3
    #define makes preprocessor macros. I don't think that's what you want. Commented Feb 11, 2015 at 0:39
  • with this i can call my colors like draw(x,y,w,h,BLUE(255)) is that a wrong way to doit? Commented Feb 11, 2015 at 0:40
  • don't mix macros and enums. maybe you want a global array of color to enumerate over ? Commented Feb 11, 2015 at 0:41

2 Answers 2

1

First, you shouldn't mix #define macros and enums - these are totally different thing, and your code is equal to -

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

So, from now, I'll ignore enum Colors and talking about those macros.


I suggest you to store values into array.

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

Notice that alpha is zero. Since array can store just values (not macro functions), I should fix alpha with something.

Then, you can use it like this:

color[42] & (alpha << 24)

If you feel mess, you can make another macro

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

or an inline function (..which is recommended)

inline COLOR_TYPE GetColor(int n, uint8_t alpha)
{
    return color[n] & (alpha << 24);
}
Sign up to request clarification or add additional context in comments.

Comments

0

i have found a way to doit but i dont know if that good :

firstly i have made a color class as following:

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

then i copied all my defines in a vector declared as following :

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

i added some static method to find a color by name and set the alpha to it like this :

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

and finally in my classes where i need to use them i can call them like it

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

so i really ask if that's a good solution, but for now it do the work and i can loop through my colors...

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.