rjs
rjs

Reputation: 437

Opengl glubyte array to integer value

I've got RGB values stored in a GLubyte array of size 3 and wondering how can I represent the RGB color values as a single integer, so what i want is to combine all 3 values to create an integer representation of the color, can someone explain how to do this?

Upvotes: 0

Views: 4537

Answers (3)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234654

You can use bit shifts to produce the integer:

GLuint get_number(GLubyte const* ptr) {
    // magic numbers for shift sizes are safe because GL types have fixed sizes
    // (8 for GLubyte and 32 for GLuint)
    return ptr[0] << 16 + ptr[1] << 8 + ptr[2];
}

And to fill an array back you can use bitmasks:

void get_bytes(GLuint number, GLubyte* out) {
    out[0] = (number & 0xFF0000) >> 16;
    out[1] = (number & 0x00FF00) >> 8;
    out[2] = number & 0x0000FF;
}

Upvotes: 1

Kromster
Kromster

Reputation: 7437

YourInteger = R + G*256 + B*65536

Is that what you are looking for?

Upvotes: 0

Ville Krumlinde
Ville Krumlinde

Reputation: 7131

Something like this should do the trick:

int color=(ar[0] << 16) | (ar[1] << 8) | ar[2];

Upvotes: 1

Related Questions