Search code examples
openglcolorsshaderrgbhsv

Render Points in HSV color space instead of RGB with OpenGL


I'm currently trying to render my points with the HSV color space. To do this, I use a function in my fragment shader, with produces the correct conversion vector. Now my question is how to tell OpenGL that it should interpret this vector as an HSV color instead of an RGB color.

My fragment shader looks like this:

 const GLchar *fragment_shader2 =
 "#version 330\n"
 ""
 "/* Output Variables */"
 "out vec4 out_color;"
 ""
 "/* Function Prototypes */"
 "vec3 convertRGBtoHSV(vec3 rgbColorVector);"
 ""
 "void main()"
 "{"
 "  vec3 rgbColor = vec3(1.0, 0.0, 0.55);"
 "  "
 "  out_color = vec4(convertRGBtoHSV(rgbColor), 1.0);"
 "}"
 ""
 "vec3 convertRGBtoHSV(vec3 rgbColorVector)"
 "{"
 "  vec3 hsvColorVector = vec3(0.0, 0.0, 0.0);"
 "  float maxValue = max(rgbColorVector.r, max(rgbColorVector.g, rgbColorVector.b));"
 "  float minValue = min(rgbColorVector.r, min(rgbColorVector.g, rgbColorVector.b));"
 "  "
 "  /* Set Value */"
 "  hsvColorVector[2] = maxValue;"
 "  "
 "  /* Set Saturation */"
 "  if(maxValue != 0.0)"
 "  {"
 "      hsvColorVector[1] = (maxValue - minValue) / maxValue;"
 "  }"
 "  "
 "  /* Set Hue */"
 "  float hue = 0.0;"
 "  if(maxValue == rgbColorVector.r)"
 "  {"
 "      hue = (0.0 + (rgbColorVector.g - rgbColorVector.b) / (maxValue - minValue)) * 60.0;"
 "  }"
 "  else if(maxValue == rgbColorVector.g)"
 "  {"
 "      hue = (2.0 + (rgbColorVector.b - rgbColorVector.r) / (maxValue - minValue)) * 60.0;"
 "  }"
 "  else if(maxValue == rgbColorVector.b)"
 "  {"
 "      hue = (4.0 + (rgbColorVector.r - rgbColorVector.g) / (maxValue - minValue)) * 60.0;"
 "  }"
 "  "
 "  if(hue < 0.0)"
 "  {"
 "      hue = hue + 360.0;"
 "  }"
 "  "
 "  hsvColorVector[0] = hue / 360.0;"
 "  "
 "  /* Return converted color vector */"
 "  return hsvColorVector;"
 "}"
 "\0";

In this example, the output is vec3(0.908, 1.0, 1.0) which is displayed as white instead of pink.


Solution

  • OpenGL cannot display colors using HSV color space. You can use only RGBA format (or some variations like SRGBA)

    but it is not a problem to use HSV color space internally and at the end produce RGBA value