将定义的调色板转换为 rgb 变量

translate palette defined to rgb variable

使用调色板可以轻松创建颜色渐变

set view map
set samp 50,50

set palette defined (0 "blue", 1 "green", 2 "red")
spl "++" us 1:2:1 palette pt 5

现在我想在垂直方向应用透明度。选项 lc rbg variable 通过 alpha 通道支持透明度(另见 ):

spl "++" us 1:2:1:(int((+5)/10*255)<<24) lc rgb var pt 5

但是如何将 palette 颜色转换为 rgb 颜色?

第二个问题:尽管我在 samp 中指定了 50,但为什么我只得到 10 个水平行?

还要检查这个:

首先,您可以检查您定义的调色板在做什么:

set palette defined (0 "blue", 1 "green", 2 "red")
test palette

你会得到这个:

每个通道(R,G,B)都有一个输入范围[0:1]和输出范围[0:1]的函数。在这种情况下,它是线性渐变。

因此,您必须定义这样一个函数,并使用位移将通道与透明度 (alpha) 通道放在一起(参见 help operators binary)。

调色板的好处在于 gnuplot 会注意范围。在这里,您必须提前知道最小值和最大值并相应地缩放颜色。您可以为此使用 stats

代码:

### your own palette with transparency
reset session

r(x) = x < 0.5 ? 0 : 2*x -1
g(x) = x < 0.5 ? 2*x : 2-2*x
b(x) = x < 0.5 ? 1-2*x : 0
a(y) = y

myColor(x,y) = (int(a((y-yMin)/(yMax-yMin))*0xff)<<24) + \
               (int(r((x-xMin)/(xMax-xMin))*0xff)<<16) + \
               (int(g((x-xMin)/(xMax-xMin))*0xff)<<8) + \
                int(b((x-xMin)/(xMax-xMin))*0xff)

set samples 50
set isosamples 50
set size square

xMin=-5; xMax=5
yMin=-5; yMax=5

plot '++' u 1:2::(myColor(,)) w p pt 5 ps 0.5 lc rgb var notitle

### end of code

结果:

首先简单回答:当存在二维采样时,无论是自动从 splot 还是明确地从 plot '++',第一维的样本数量由 set sample 控制并且第二个维度的样本数由set isosample.

控制

现在更难了。在当前 5.2.8 之前的 gnuplot 版本中,您不能将透明度直接添加到调色板。但是,您可以通过一个多步骤过程将调色板保存到文件或数据块中,然后将其作为 RGB 颜色数组读回。一旦你有了这个数组,你就可以添加一个 alpha 通道值,这样它也能表达透明度。我将使用命令 test palette 创建的数据块来展示这个过程。在旧版本的 gnuplot 中,您可能不得不使用 set print "palette.save"; show palette palette 256;.

创建的文件
# save current palette to a datablock as a list of 256 RGB colors, one per line
set palette defined (0 "blue", 1 "green", 2 "red")
test palette
# print one line to show the format (cbval R G B NTSCval)
print $PALETTE[4]
# Create an array of packed RGB values
array RGB[256]
do for [i=1:256] {
    Red = int(255. * word($PALETTE[i],2))
    Green = int(255. * word($PALETTE[i],3))
    Blue = int(255. * word($PALETTE[i],4))
    RGB[i] = Red << 16 | Green << 8 | Blue
}

# Sample from '++' are generated to span ranges on the u and v axes
# I choose 1:256 so that the y coordinates match the range of array indices
set sample 50
set isosample 50
set urange [1:256]
set vrange [1:256]
set xrange [*:*] noextend
set yrange [*:*] noextend

# Now you can use colors stored in the array via colorspec `rgb variable`
# which will also accept an alpha channel in the high bits
plot "++" using 1:2:(RGB[int()]) with points pt 5 lc rgb variable

# The final step is to add an alpha channel as a function of y
# Here I go from opaque (Alpha = 0) to 50% transparent (Alpha = 127)
# This works because I know y will run from 1-256 
ARGB(y) = RGB[int(y)] + (int(y/2)<<24)
plot "++" using 1:2:(ARGB()) with points pt 5 lc rgb variable

输出如下所示。

如您所见,所需的命令序列很乱。 在下一个 gnuplot 版本 (5.4) 中,它会变得 容易。新版本将提供一个函数 palette(z) 将当前调色板直接转换为打包的 RGB 值。请注意,palette() 函数不在 -rc1 测试版本中,但会在 -rc2 中。所以在 5.4 版中,所有 palette/array/RGB 操作都可以替换为

  plot '++' using 1:2:(palette() + (int()<<24)) with points pt 5 lc rgb variable