将浮点数转换为 RGB 向量 [英] Convert a floating point number to RGB vector
问题描述
我需要用opengl渲染几个立方体,每个立方体的颜色取决于立方体中心的磁场,但我不知道如何将浮点数转换成QVector3D
或 glm::vec3 (RGB)
.
而且我可以在0到1之间转换float数组的范围,我需要知道如何将磁场数组转换为RGB来定义颜色数组.
如果我理解正确,那么您想用 RGB 颜色表示 [0.0, 1.0] 范围内的浮点值.
我建议将该值转换为
对于完整范围,转换为:
float value = ...;//范围 [0.0, 1.0] 内的值浮动 H = 值;浮动 R = fabs(H * 6.0f - 3.0f) - 1.0f;浮动 G = 2.0f - 晶圆厂(H * 6.0f - 2.0f);浮动 B = 2.0f - 晶圆厂(H * 6.0f - 4.0f);glm::vec3 颜色(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)));
如果你不想要完整的范围,例如,如果你想使用从红色到蓝色的范围,那么value
必须被缩放:
float H = value * 2.0f/3.0f;
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.
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;
这篇关于将浮点数转换为 RGB 向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!