Maaz Azeem
Maaz Azeem

Reputation: 49

Storing RGB values within a struct c++

so i have a struct named color my main objective is to create a pallet of colors for my program and access them through a single variable rather than the RGB values or three different struct variables.

function declaration

WINGDIAPI void APIENTRY glColor3f(GLfloat red,GLfloat green,GLfloat blue);

struct color
{
    GLfloat r;
    GLfloat g;
    GLfloat b;

};
color blue={0.0,0.0,255.0};

glColor3f(blue);

I am able to access the values by blue.r, blue.g, blue.b. But instead i want them to be all in one variable so when I want to access it I can just call on the variable blue.

Upvotes: 0

Views: 4227

Answers (2)

zett42
zett42

Reputation: 27756

You can't pass only a single variable to glColor3f() because that function has three parameters.

That being said there is a similar function glColor3fv() that expects an array of three color values. This function only has a single parameter, so you only need to pass a single argument to it.

So for use with glColor3fv() and similar functions that expect a color array, we can actually declare a type like this...

using color3f = std::array<GLfloat, 3>;

... which could be used like this:

color3f blue={0.0,0.0,255.0};
glColor3fv( blue.data() );

The call to the data() member is necessary to convert std::array into a pointer.

Why am I not just declaring a plain array type like using color3f = GLfloat[3] which wouldn't require the call to the data() member? Because plain arrays have some shortcomings, for instance you can't simply assign one to another like blue2 = blue;.

Upvotes: 3

Richard
Richard

Reputation: 61239

glColor3f does not take your struct color type as an argument. So you cannot use glColor3f(blue).

However, you could define an overloaded function like so:

void glColor3f(struct color &c) {
  glColor3f(c.r, c.g, c.b);
}

Upvotes: 2

Related Questions