在不使用十六进制代码的情况下在 gnuplot 中设置线条不透明度

Posted

技术标签:

【中文标题】在不使用十六进制代码的情况下在 gnuplot 中设置线条不透明度【英文标题】:Setting line opacity in gnuplot without using a hex code 【发布时间】:2017-12-02 11:42:06 【问题描述】:

我正在使用 gnuplot 绘制一些线条,我想让其中一些线条透明。我知道这在某些终端(例如 Mac 上的 aquaterm)中是可能的,使用如下命令:

plot x lw 10 lc rgb "#77000000"

这将产生一条半透明的黑线(即灰色),十六进制颜色为黑色 - '000000',alpha 值为 '77'。但是,使用它需要我知道我想要的颜色的十六进制代码。是否可以以更标准的方式指定颜色,例如使用颜色名称或编号,然后还单独指定不透明度级别?

我要查找的命令类型类似于:

plot x lw 10 lc rgb 'black' transparency '#77'

这不起作用。有任何想法吗?我正在使用 gnuplot 5。

【问题讨论】:

不,您不能单独设置线条的透明度。值得提出功能要求。只为填写你可以做set style fill transparent 0.2 【参考方案1】:

即使对于 gnuplot 5.0,也有一种解决方法。在 gnuplot 5.2 中,您还可以使用数组。从定义的颜色列表中获取 RGB 颜色值并添加透明度 (alpha)。当然,您必须从某个地方获取颜色代码。 在 gnuplot 控制台中输入 show colors 并查看 gnuplot 中所有 111 种预定义颜色。基本上,您也可以定义自己的颜色。

在此处显示 gnuplot 标准颜色:https://***.com/a/54659829/7295599

代码:

### add colors by name with transparency
reset session

ColorNames = "white black dark-grey red web-green web-blue dark-magenta dark-cyan dark-orange dark-yellow royalblue goldenrod dark-spring-green purple steelblue dark-red dark-chartreuse orchid aquamarine brown yellow turquoise grey0 grey10 grey20 grey30 grey40 grey50 grey60 grey70 grey grey80 grey90 grey100 light-red light-green light-blue light-magenta light-cyan light-goldenrod light-pink light-turquoise gold green dark-green spring-green forest-green sea-green blue dark-blue midnight-blue navy medium-blue skyblue cyan magenta dark-turquoise dark-pink coral light-coral orange-red salmon dark-salmon khaki dark-khaki dark-goldenrod beige olive orange violet dark-violet plum dark-plum dark-olivegreen orangered4 brown4 sienna4 orchid4 mediumpurple3 slateblue1 yellow4 sienna1 tan1 sandybrown light-salmon pink khaki1 lemonchiffon bisque honeydew slategrey seagreen antiquewhite chartreuse greenyellow gray light-gray light-grey dark-gray slategray gray0 gray10 gray20 gray30 gray40 gray50 gray60 gray70 gray80 gray90 gray100"

ColorValues = "0xffffff 0x000000 0xa0a0a0 0xff0000 0x00c000 0x0080ff 0xc000ff 0x00eeee 0xc04000 0xc8c800 0x4169e1 0xffc020 0x008040 0xc080ff 0x306080 0x8b0000 0x408000 0xff80ff 0x7fffd4 0xa52a2a 0xffff00 0x40e0d0 0x000000 0x1a1a1a 0x333333 0x4d4d4d 0x666666 0x7f7f7f 0x999999 0xb3b3b3 0xc0c0c0 0xcccccc 0xe5e5e5 0xffffff 0xf03232 0x90ee90 0xadd8e6 0xf055f0 0xe0ffff 0xeedd82 0xffb6c1 0xafeeee 0xffd700 0x00ff00 0x006400 0x00ff7f 0x228b22 0x2e8b57 0x0000ff 0x00008b 0x191970 0x000080 0x0000cd 0x87ceeb 0x00ffff 0xff00ff 0x00ced1 0xff1493 0xff7f50 0xf08080 0xff4500 0xfa8072 0xe9967a 0xf0e68c 0xbdb76b 0xb8860b 0xf5f5dc 0xa08020 0xffa500 0xee82ee 0x9400d3 0xdda0dd 0x905040 0x556b2f 0x801400 0x801414 0x804014 0x804080 0x8060c0 0x8060ff 0x808000 0xff8040 0xffa040 0xffa060 0xffa070 0xffc0c0 0xffff80 0xffffc0 0xcdb79e 0xf0fff0 0xa0b6cd 0xc1ffc1 0xcdc0b0 0x7cff40 0xa0ff20 0xbebebe 0xd3d3d3 0xd3d3d3 0xa0a0a0 0xa0b6cd 0x000000 0x1a1a1a 0x333333 0x4d4d4d 0x666666 0x7f7f7f 0x999999 0xb3b3b3 0xcccccc 0xe5e5e5 0xffffff"

myColor(c) = (idx=NaN, sum [i=1:words(ColorNames)] \
    (c eq word(ColorNames,i) ? idx=i : idx), word(ColorValues,idx))

# add transparency (alpha) a=0 to 255 or 0x00 to 0xff
myTColor(c,a) = sprintf("0x%x%s",a, myColor(c)[3:])
    
set xrange[0:2*pi]
set samples 200

plot sin(x)   w l lw 12 lc rgb myTColor("red",0xcc), \
     sin(2*x) w l lw 12 lc rgb myTColor("green",0xcc), \
     sin(3*x) w l lw 12 lc rgb myTColor("blue",0xcc)
### end of code     

结果:

加法:

如果您甚至不想在 gnuplot 脚本中使用长长的颜色名称和颜色十六进制代码列表,您可以使用以下方法:从虚拟调色板中自动提取值。为此,gnuplot 必须为每种颜色绘制test palette。您可以在循环之前将终端切换到set term unknown,然后再切换回您想要的终端。 我想从$PALETTE 获取值仅适用于 gnuplot >=5.2。

代码:(结果同上)

### add colors by name with transparency (without hex-color code list)
reset session

ColorNames = 'red green blue magenta yellow cyan'   # must be existing gnuplot color names

# get the color values from dummy palettes
ColorValues = ''
RGBComp(c) = int(word($PALETTE[256],c+1)*0xff)
do for [i=1:words(ColorNames)] 
    set palette defined (0 word(ColorNames,i))
    test palette
    RGB = sprintf("0x%02x%02x%02x",RGBComp(1),RGBComp(2),RGBComp(3))
    ColorValues = ColorValues." ".RGB


myColor(c) = (idx=NaN, sum [i=1:words(ColorNames)] \
    (c eq word(ColorNames,i) ? idx=i : idx), word(ColorValues,idx))

# add transparency (alpha) a=0 to 255 or 0x00 to 0xff
myTColor(c,a) = sprintf("0x%02x%s",a, myColor(c)[3:])
    
set xrange[0:2*pi]
set samples 200

plot sin(x)   w l lw 12 lc rgb myTColor("red",0xcc), \
     sin(2*x) w l lw 12 lc rgb myTColor("green",0xcc), \
     sin(3*x) w l lw 12 lc rgb myTColor("blue",0xcc)
### end of code

【讨论】:

@Mead,这能回答你的问题吗?

以上是关于在不使用十六进制代码的情况下在 gnuplot 中设置线条不透明度的主要内容,如果未能解决你的问题,请参考以下文章

如何在不更改构建系统的情况下在 C 项目中使用 rust 代码?

如何在不使用完成块的情况下在动画后执行代码?

如何在不使用转储的情况下在 python 中编写 json 文件

如何在不重复代码的情况下在所有控制器中显示警报?

如何在不使用存储过程的情况下在表函数中返回值 exec?

在不提示的情况下在 Powershell 中获取当前用户的凭据对象