Skip to content

Storing RGB values within a struct c++

An answer to this question on Stack Overflow.

Question

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.

Answer

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