5

My question is related to Algorithm to convert any positive integer to an RGB value but really it's not the same question -- that guy has mostly a data normalization problem, I actually have more of an aesthetic color selection problem.

I have a bunch of numbers between -1.0 and +1.0. I need to create a heatmap overlaid with text.

What is the simplest way, using PHP, to convert each number into an HTML color (#rrggbb), in such way that the resulting color not only is intuitively related to temperature (i.e. bluest for coldest and reddest for the hottest, with some smooth transition in between) but also that it's suitable as a background color for black-color text?

4

1 回答 1

12

我会将它实现为红色和蓝色分量之间的简单线性渐变,使用sprintf函数编码为十六进制值:

function toHeatColor($full) {
    $positive = ($full + 1) / 2;
    return sprintf("#%02xcc%02x", $positive * 51 + 204, (1 - $positive) * 51 + 204);
}

您可以在http://jsfiddle.net/9QQkU/中查看颜色范围。对应的值为 -1、-0.75、0、0.75 和 1。

于 2010-11-21T00:34:31.910 回答