云图的颜色映射算法Jet
davies 发表于 2007 年 08 月 18 日
在绘制云图等时,需要用颜色来表征一维数值的大小,如果用灰度表示,那映射非常简单,可以是线性映射。如果用彩色来表示,则需要一个从一维数值到三维RGB颜色空间的映射,希望该映射能够覆盖大部分颜色空间,并且在不同区间都能比较明显地反映数值变化。
Matlab中绘制云图时,默认采用的是jet映射算法,随着数值的增大,颜色从蓝到红,即由冷色到暖色。它是让三原色RGB分别由小到大,保持一段之后再由大表小,并且将三原色的这种变化错开,否则就成了灰度了。
在Matlab中该算法描述实现:
function J = jet(m) %JET Variant of HSV. % JET(M), a variant of HSV(M), is an M-by-3 matrix containing % the default colormap used by CONTOUR, SURF and PCOLOR. % The colors begin with dark blue, range through shades of % blue, cyan, green, yellow and red, and end with dark red. % JET, with no arguments, is the same length as the current colormap. % Use COLORMAP(JET). % % See also HSV, HOT, PINK, FLAG, COLORMAP, RGBPLOT. % Copyright 1984-2002 The MathWorks, Inc. % $Revision: 5.7 $ $Date: 2002/04/01 21:01:50 $ if nargin < 1 m = size(get(gcf,'colormap'),1); end n = ceil(m/4); u = [(1:1:n)/n ones(1,n-1) (n:-1:1)/n]'; g = ceil(n/2) - (mod(m,4)==1) + (1:length(u))'; r = g + n; b = g - n; g(g>m) = []; r(r>m) = []; b(b<1) = []; J = zeros(m,3); J(r,1) = u(1:length(r)); J(g,2) = u(1:length(g)); J(b,3) = u(end-length(b)+1:end)
对应的C++实现为:
该代码可以在我的代码仓库中下载。
1. 发表于 2007 年 09 月 11 日 1:02 a.m.