1

I need to render a couple of cubes by opengl, and the color of every cube depends on the the magnetic field at the center of cube, but I don't know how to convert the float number into the QVector3D or glm::vec3 (RGB).

And I can convert the range of the float array between 0 and 1, I need to know how to convert the magnetic field array to RGB to define the color array.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Alex M
  • 29
  • 1
  • 5
  • you can try this [RGB values of visible spectrum](https://stackoverflow.com/a/22681410/2521214) just change (scale) your floating point parameters to match visible light wavelengths ... – Spektre Mar 15 '19 at 17:48

2 Answers2

3

If I understand correctly, then you want to represent a floating point value in the range [0.0, 1.0], by a RGB color.
I recommend to transform the value to the HSV color range.

For the full range the conversion is:

float value = ...; // value in range [0.0, 1.0]

float H = value;
float R = fabs(H * 6.0f - 3.0f) - 1.0f;
float G = 2.0f - fabs(H * 6.0f - 2.0f);
float B = 2.0f - fabs(H * 6.0f - 4.0f);  

glm::vec3 color( 
    std::max(0.0, std::min(1.0, R))
    std::max(0.0, std::min(1.0, G))
    std::max(0.0, std::min(1.0, B)));

If you don't want the full range, for example, if you want to use the range from red to blue, then value has to be scaled:

float H = value * 2.0f/3.0f;
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

You need some form of lookup table (LUT). A RGB vector has 3 dimensions, a single float just one. There's literally an infinite number of ways how to map between those two.

datenwolf
  • 159,371
  • 13
  • 185
  • 298