本文介绍了将值映射到颜色标度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值列表,应该绘制到具有特定颜色的地图。
绘制到地图已经完成,但我需要找出一种方法将值 n 映射到表示其值的颜色。

I have a list of values which should be plotted to a map with a certain color.The plotting to the map is already done, but I need to figure out a way to map the value n to a color that represents its value.

到目前为止,我的解决方案的一个例子是根据 min max ,然后将它们分配给十六进制颜色 0 为最低值, 255 最高。这当然限制了我的自我到灰度。这是代码:

An example and my solution so far is to normalize the values based on the min and max and then assign them to hex color 0 for the lowest and 255 for the highest. This of course limits my self to the grey scale. Here is the code:

$color = ($value / $max) * 255    // (min is zero)

但是,如果值应该从蓝色变为红色,怎么办呢?
是否有任何常见的库或工具可以解决这个问题?到目前为止我还没有找到任何。

But how to do this if the values should go from blue to red for instance?Is there any common libraries or tools that can solve this? So far I haven't been able to locate any.

推荐答案

可能有libs。然而,让我们得到一个简短的热身,一般的原则。一般来说,您有以下选项:

There might be libs to do that. However let's get a short warm up into the general principles. In general you have following options:


  1. 预设颜色索引 $ coloridx = array(0 =>'#FFFFFF',1 =>'#FFEE00',...);

  2. 任何算法,例如线性梯度,其基本上是基于所有三个RGB通道(R =红色,G =绿色,B =蓝色)的基于迭代的适应。

  3. 两者的组合,

如果在您的注意事项中包含算法,您必须理解, no true false 。这一切都取决于你想实现什么。在某些情况下,将绿色的变化转换为 n = 0..10 是有意义的,然后在 n> 10 。帽子和乘数有助于设置口音。这样的事情。

If you include algorithms in your considerations you must understand that there is no true or false. It all depends on what you would like to implement. There might be occasions where it makes sense to render variations of green into n=0..10 and then have red to black in everything beyond n>10. Caps and multipliers help to set accents. Things like that.

实现线性渐变的一种方法是:

One way of implementing a linear gradient would be:

function lineargradient($ra,$ga,$ba,$rz,$gz,$bz,$iterationnr) {
  $colorindex = array();
  for($iterationc=1; $iterationc<=$iterationnr; $iterationc++) {
     $iterationdiff = $iterationnr-$iterationc;
     $colorindex[] = '#'.
        dechex(intval((($ra*$iterationc)+($rz*$iterationdiff))/$iterationnr)).
        dechex(intval((($ga*$iterationc)+($gz*$iterationdiff))/$iterationnr)).
        dechex(intval((($ba*$iterationc)+($bz*$iterationdiff))/$iterationnr));
  }
  return $colorindex;
}

$colorindex = lineargradient(
  100, 0, 0,   // rgb of the start color
  0, 255, 255, // rgb of the end color
  256          // number of colors in your linear gradient
);

$color = $colorindex[$value];

我UPDATED代码添加dechex,反馈给评论。

I UPDATED the code to add dechex, which feeds back on the comments.

这篇关于将值映射到颜色标度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 00:12
查看更多