2

Is there any way I can use a color name instead of its hexadecimal value or RGB triple to change my plot color? What I need is similar to the first answer in this post. My input data (input.dat) is as follows:

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

Simple Test:

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

1 Answers1

1

The lc rgb variable will convert any strings to integers before evaluating them to a color, and this will not transform the string "red" into the number 0xff0000. So we have to define our own conversion function.

You can use the idea from answer https://stackoverflow.com/a/35729052/7010554. Here is a possible implementation:

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

arrows with color names read from file

The functions colorSet(name, rgb) and `colorGet(name) will create and access variables with names of the form color_green, color_red, ... The values of these variables are the corresponding rgb values.

You can define colors as you like, the name must not contain hyphens. The gnuplot internal color definitions are shown by the command show colors.

maij
  • 4,094
  • 2
  • 12
  • 28
  • I wasn't expecting to define my own conversion function. Being the color names defined I thought it would be simpler. But thank you @maij, that solved my problem. – ACR Nov 29 '16 at 20:29