【问题标题】:Issue with RGB color namesRGB 颜色名称问题
【发布时间】:2016-11-28 15:11:12
【问题描述】:

有什么方法可以使用颜色名称而不是十六进制值或 RGB 三元组来更改我的绘图颜色?我需要的是similar to the first answer in this post。我的输入数据(input.dat)如下:

1 1 2 0 magenta
1 2 2 0 red
1 3 2 0 green
1 4 2 0 black

简单测试:

set xrange [0:10]
set yrange [0:10]
plot "input.dat" using 1:2:3:4:5 with vectors lw 3 lc rgb variable

【问题讨论】:

    标签: file input colors gnuplot rgb


    【解决方案1】:

    lc rgb variable 将在将任何字符串评估为颜色之前将其转换为整数,这不会将字符串“red”转换为数字 0xff0000。所以我们必须定义自己的转换函数。

    您可以使用答案https://stackoverflow.com/a/35729052/7010554 中的想法。这是一个可能的实现:

    colorSet(name, rgb) = sprintf("color_%s = %d", name, rgb)
    colorGet(name) = value(sprintf("color_%s", name))
    
    eval colorSet("magenta", 0xff00ff)
    eval colorSet("red",     0xff0000)
    eval colorSet("green",   0x00ff00)
    eval colorSet("black",   0x000000)
    eval colorSet("dark_yellow",   0xc8c800)
    
    print colorGet("green")
    print color_green
    
    set xrange [0:10]
    set yrange [0:10]
    plot "input.dat" using 1:2:3:4:(colorGet(stringcolumn(5))) with vectors lw 3 lc rgb variable
    

    函数colorSet(name, rgb) 和 `colorGet(name) 将创建和访问名称格式为 color_green、color_red、...的变量。这些变量的值是对应的 rgb 值。

    您可以根据需要定义颜色,名称不得包含连字符。 gnuplot 内部颜色定义由命令show colors 显示。

    【讨论】:

    • 我没想到会定义自己的转换函数。作为定义的颜色名称,我认为它会更简单。但是谢谢@maij,这解决了我的问题。
    猜你喜欢
    • 2018-12-23
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 2011-10-08
    • 2016-04-06
    • 2012-03-02
    相关资源
    最近更新 更多