Alex M
Alex M

Reputation: 29

Convert a floating point number to RGB vector

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.

Upvotes: 1

Views: 3894

Answers (2)

Rabbid76
Rabbid76

Reputation: 210968

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;

Upvotes: 3

datenwolf
datenwolf

Reputation: 162164

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.

Upvotes: 0

Related Questions