3

我在范围(0.0,100.0)中有大约 1000 个浮点值,我想将这些值映射到颜色(RGB)中。到目前为止,我所做的是创建一个具有 1000 个颜色(RGB)值的颜色图,使用浮点值来索引颜色图并获取 RGB 值。

但问题是,我失去了精度,因为我在将浮点值用作我的颜色图的索引之前将它们转换为 int。将浮点数转换为 rgb 的最佳方法是什么?

编辑:

color color_list[100];
float float_values[1000]
for(i = 0 to 999)
{
   int colormap_idx = float_values[i];  // Note that the float is converted into an int
   color current_color = color_list[colormap_idx];
}
4

2 回答 2

3

The total number of RGB values you can have is 256^3. It would be nice if you could utilize all of them, but sometimes it can be hard to come up with a nice intuitive mapping. Since there are a total possible of 256^4 floats (more than possible RGB values) you will lose precision no matter what you do, but you can still do much, much better than what you currently.

I don't know exactly what you are doing with the pre-defined color map, but consider defining only a few intermediate colors that correspond to a few intermediate floating values and interpolating each input floating point value. In the code below, fsample and csample are your corresponding points. For example:

fsample[0] = 0.0   -> csample[0] = (0, 0, 0)
fsample[1] = 0.25  -> csample[1] = (0, 0, 100)
fsample[2] = 0.5   -> csample[2] = (0, 170, 170)
fsample[3] = 0.75  -> csample[3] = (170, 170, 0)
fsample[4] = 1.0   -> csample[4] = (255, 255, 255)

This will allow you to cover a lot more ground in RGB space with floats, allowing a higher precision conversion, while still giving you some power to flexibly define intermediate colors. This is a fairly common method to convert grayscale to color.

There are a few optimizations and error checks you can apply to this code, but I left it unoptimized for the sake of clarity:

int N = float_values.size();
color colormap[N];
for(i = 0 to N)
{
    colormap[i] = RGBFromFloat(float_values[i], fsample, csample, num_samples);
}



color RGBFromFloat(float in, float fsample[], float csample[], num_samples)
{
    color out;

    // find the interval that the input 'in' lies in
    // this is a simple search on an ordered array...
    // consider replacing with a better algorithm for a large number of samples
    for(i = 0 to num_samples-1)
    {
        if(fsample[i] =< in && in < fsample[i+1])
        {
             out = interpolate(fsample[i], fsample[i+1], csample[i], csample[i+1], in);    
             break;
        }
    }
    return color;
}

color interpolate(float flow, float fhigh, color clow, color chigh, float in)
{
    float t = (in-flow)/(fhigh-flow);
    return clow*(1 - t) + chigh*t
}
于 2013-08-24T02:38:23.337 回答
1

我不知道这是否是最好的方法(因为您没有给我们提供最优标准),但是如果“我正在失去精度”是指一旦转换为 int,您最多只能有 100 种不同的颜色组合,那么你可以这样做:

// this code is C99

#define MAX_FLOAT_VAL 100.0
#define N_COLORS 2000
#define N_FLOAT_SAMPLES 1000

color color_list[N_COLORS];
float float_values[N_FLOAT_SAMPLES];

// the following loop must be placed in some function
for( int i = 0; i < N_FLOAT_SAMPLES; i++ )
{
    // the following assignment will map 
    // linearly a float in the range [0 ... MAX_FLOAT_VAL]
    // into an int in the range [0 ... (N_COLORS-1)]
    int colormap_idx = (float_values[i] / MAX_FLOAT_VAL) * (N_COLORS - 1);
    color current_color = color_list[colormap_idx];
    // ... do something with current_color ...
}

当然,您仍然必须color_list使用合适的算法生成条目(我建议不要手动这样做:-)。这是一个完全不同的问题,因为它涉及更多“自由度”,因为您尝试将 1-D 空间( 的值colormap_idx)映射到 3-D 空间(所有可能的 RGB 三元组的集合)。

PS:您的要求似乎让我想起了为像曼德布罗集的图形表示一样的分形着色所需的计算。

希望这可以帮助。

于 2013-08-24T01:31:29.390 回答