简体   繁体   English

简单的PHP函数将数字转换为热图HTML背景颜色?

[英]Simple PHP function to convert a number to a heatmap HTML background color?

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. 我的问题与将任何正整数转换为RGB值的算法有关但实际上它不是同一个问题 - 那个人主要是数据规范化问题,实际上我有更多的审美颜色选择问题。

I have a bunch of numbers between -1.0 and +1.0. 我有一堆介于-1.0和+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 (ie 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? 使用PHP将每个数字转换为HTML颜色(#rrggbb)的最简单方法是什么,这样产生的颜色不仅与温度直观相关(即最冷最热,最热最热,有些光滑)两者之间的过渡)但它也适合作为黑色文本的背景颜色?

I would implement it as a simple linear gradient between the red and blue components, using the sprintf function to encode to a hex value: 我将它实现为红色和蓝色组件之间的简单线性渐变,使用sprintf函数编码为十六进制值:

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

You can see how the range of colors looks at http://jsfiddle.net/9QQkU/ . 您可以看到http://jsfiddle.net/9QQkU/的颜色范围。 The corresponding values are -1, -0.75, 0, 0.75, and 1. 相应的值为-1,-0.75,0,0.75和1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM