如果需要使用查找表,並且在處理浮點數據,你必須quantise浮點數據成獨立範圍,然後查找表中的每個範圍。
但是,在這裏使用查找表似乎不合適;爲什麼不定義一個將浮點值作爲輸入並返回RGB值的映射函數?我用它來着色分形(參見http://jk.ozlabs.org/projects/lca2008-hackfest/中的'着色'部分)。
基本上,我的方法是做一個簡化的HSV到RSB的轉換,使用用於飽和度和數值,而對於色相浮點輸入數據的恆定值。這會給你的價值觀以下RGB輸出:
使用此映射功能有色一些分形見http://jk.ozlabs.org/blog/post/65/hackfest08-solution-2/。
我有一些C代碼來做到這一點,可以很容易地轉換爲Python。請注意,這個假設0 < =我< = 1,而你可能想-1 < =我< = 1:
/* for a value x (which is between x_min and x_max), interpolate a y value
* (between y_min and y_max) of the same proportion.
*/
static float interpolate(float x, float x_min, float x_max,
float y_min, float y_max)
{
x = (x - x_min)/(x_max - x_min);
return x * (y_max - y_min) + y_min;
}
/*
* given a the i and i_max values from a point in our (x,y) coordinates,
* compute the colour of the pixel at that point.
*
* This function does a simplified Hue,Saturation,Value transformation to
* RGB. We take i/i_max as the Hue, and keep the saturation and value
* components fixed.
*/
void colour_map(struct pixel *pix, float i, float i_max)
{
const float saturation = 0.8;
const float value = 0.8;
float v_min, hue;
hue = i/(i_max + 1);
v_min = value * (1 - saturation);
/* create two linear curves, between value and v_min, of the
* proportion of a colour to include in the rgb output. One
* is ascending over the 60 degrees, the other descending
*/
if (hue < 0.25) {
pix->r = value * 255;
pix->g = interpolate(hue, 0.0, 0.25, v_min, value) * 255;
pix->b = v_min * 255;
} else if (hue < 0.5) {
pix->r = interpolate(hue, 0.25, 0.5, value, v_min) * 255;
pix->g = value * 255;
pix->b = v_min * 255;
} else if (hue < 0.75) {
pix->r = v_min * 255;
pix->g = value * 255;
pix->b = interpolate(hue, 0.5, 0.75, v_min, value) * 255;
} else {
pix->r = v_min * 255;
pix->g = interpolate(hue, 0.75, 1.0, value, v_min) * 255;
pix->b = value * 255;
}
pix->a = 255;
}
你能否解釋一下這句話「在我的應用程序已經-1.0之間浮點值1.0需要映射到RGB空間。「請? – Neutralizer 2010-08-11 14:04:12
是的 - 我有-1.0到1.0之間的數字列表,我需要通過將數字映射到RGB值將這些數字列表變成彩色圖片。這就像做一個顏色分形而不是逃逸時間值我有這些計算的數字。 – Johnny 2010-08-11 15:03:51
通道最大值是255的字節,因此浮點數的分辨率將爲1/255 = 0.004並且因爲您的區間爲[-1,1],所以我們需要加倍。所以最後的答案 - 一步R/G/B值浮點的分辨率爲0.008左右。更高的精度沒有任何意義,因爲具有更高精度的兩個浮點值將映射到相同的顏色。 – 2010-08-28 21:36:46