Understanding glClearColor values

Viewed 14377

I am trying to create a single OpenGL window with some background color picked in RGB color picker but it looks like I am missing something important in this function. When I want to set color to standard, for example red color, it works.

glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

But when I want to set some exotic color like 184, 213, 238, 1 then it shows only white.

glClearColor(184.0f, 213.0f, 238.0f, 1.0f);

What am I doing wrong? And what are the decimal values for and also the f ?

2 Answers

See the Khronos documentation page for glClearColor, which clearly says:

void glClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);

glClearColor specifies the red, green, blue, and alpha values used by glClear to clear the color buffers. Values specified by glClearColor are clamped to the range [0,1].

This means the parameters are floating point values in the range from 0.0 to 1.0. Adapt your code like this:

glClearColor(184.0f/255.0f, 213.0f/255.0f, 238.0f/255.0f, 1.0f);

Here, rValue, gValue and bValue always between 0.0 f to 1.0 f. So if you want to apply color between 0 to 225.and alpha is also between 0.0 f to 1.0 f. (0 for full transparent).

rValue= 184.0 / 255.0

gValue= 213.0 / 255.0

bValue= 238.0 / 255.0

glClearColor(rvalue, gValue, bValue, alpha);
Related