将 int 转换为 RGB。 PHP 和 HTML/CSS 中的可视化
我正在尝试可视化网格上的数据,其中单元格值实际上由颜色表示。红色表示高,蓝色表示低。我太天真地认为 PHP 的 dechex() 会帮助我,只需获取 int 的十六进制等效值并将其用作背景颜色即可/code> 在 CSS
中(我确实为小值应用了必要的零填充)。
但这并不能完全得到我想要的东西。有没有一种算法可以让我正确地可视化这一点?红色表示高,蓝色表示低。
我当前的代码是这样的:
<?php
$dec = (int) $map[$y][$x]["total_score"];
$hex = dechex($dec);
$color = ($dec <= 65535) ? (($dec) ? "00$hex" : "ffffff") :
(($dec <= 1048575) ? ("0$hex") : $hex);
?>
注意它的作用:
十进制的 ff0000
小于 ff00ff
但在颜色上,第一个将显示红色,后者显示紫色。我希望红色代表非常高的小数点,蓝色代表非常低的小数点。
I'm trying to visualize a data on a grid with cell values actually represented by color. Red means high and blue means low. I was so naive in thinking that PHP
's dechex()
will help me by simply getting the hexadecimal equivalent of the int and using it as background-color
in CSS
(I did apply the necessary padding of zeros for small values).
But it doesn't quite get me what I want. Is there an algorithm that will let me visualize this properly? Red means high, blue means low.
My current code is this:
<?php
$dec = (int) $map[$y][$x]["total_score"];
$hex = dechex($dec);
$color = ($dec <= 65535) ? (($dec) ? "00$hex" : "ffffff") :
(($dec <= 1048575) ? ("0$hex") : $hex);
?>
Notice what it does:
ff0000
in decimal is smaller than ff00ff
but on color, the first will show red and the latter violet. I want red to represent very high decimals and blue very low decimals.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 RGB 并不是最好的颜色模型。我会选择 HSL - 现代浏览器支持:
color: hsl(0.5, 0.5, 0.5)
并且可以轻松转换为 RGB。HSL 让您可以轻松定义颜色的饱和度和亮度以及颜色本身。蓝色是 240 度,红色是 360 度,因此您所要做的就是将“低”映射到 240,将“高”映射到 360,将所有中值映射到 240-360 范围。
I think RGB is not the very best color model here. I'd go with HSL - supported by modern browsers:
color: hsl(0.5, 0.5, 0.5)
and easily to convert to RGB.HSL let's you define saturation and lightness of the color and the color itself quite easily. Blue is 240 deg, red is 360 deg so all you have to do is to map "low" to 240, "high" to 360 and all mid-value to 240-360 range.
将红色替换为绿色,并通过使用
0xFF00FF
进行屏蔽来删除绿色,以便仅保留红色和蓝色。使用上面的代码,如果
$a > $b
则$a
会比$b
更红。Replace Red with Green and Remove Green by masking with
0xFF00FF
in order to keep Red and Blue only.Using the code above, if
$a > $b
then$a
will be more red than$b
.