Menu

#509 Function converts RGB-string into RGB-integer

None
closed-accepted
nobody
None
5
2020-09-11
2020-09-02
No

Function converts RGB-string into RGB-integer

I think it would be very useful for using "lc rgb variable" to specify color from data, if there is a function that returns an RGB integer value when an RGB string or color name is given. For example, it is difficult to use the "lc rgb variable" in situations where you want to specify a color name in a data file to be painted, because "lc rgb variable" accepts only RGB integer value.

Assuming the name of function is 'rgb2int', it behaves like the following.

  • rgb2int("red") -> 16711680 (0x00ff0000)
  • rgb2int("0xff0000") -> 16711680 (0x00ff0000)
  • rgb2int("0x00ff0000") -> 16711680 (0x00ff0000)
  • rgb2int("#ff0000") -> 16711680 (0x00ff0000)
  • rgb2int("#00ff0000") -> 16711680 (0x00ff0000)

Here is a simple example to show the usage,

#####################################################
# function definition for PoC 
#    which recognizes only "red", "green", "blue"
#
rgb2int(color) = ( color eq "red" ) ? 16711680 : \
                 ( color eq "green" ) ? 65280 : \
                 ( color eq "blue" ) ? 255 : 0

$data <<EOT
1 1 3 red
2 2 2 green
3 3 1 blue
4 1 2 red
5 2 3 green
EOT

unset key
set xrange [0:6]
set yrange [0:4]

# Using data column to specify color name
plot $data using 1:2:(rgb2int(strcol(4))) with points lc rgb variable pt 7 ps 3, \
     $data using 1:2:4:(rgb2int(strcol(4))) with labels offset 5,0 tc rgb variable

pause -1

# Using array to specify color name
array color[3] = ["red", "green", "blue"]
plot $data using 1:3:(rgb2int(color[$3])) with points lc rgb variable pt 7 ps 3, \
     $data using 1:3:(color[$3]):(rgb2int(color[$3])) with labels offset 5,0 tc rgb variable

pause -1

Thanks.

Discussion

  • Ethan Merritt

    Ethan Merritt - 2020-09-10
    • status: open --> closed-accepted
    • Group: -->
     
  • Ethan Merritt

    Ethan Merritt - 2020-09-10

    Good idea.
    The development branch now has an initial implementation of a function rgbcolor("string") documented as follows:

    gnuplot> help rgbcolor
    
     `rgbcolor("name")` returns an integer containing the 32 bit alpha + RGB color
     value of a named color or a string of the form "0xAARRGGBB" or "#AARRGGBB".
     If the string is not recognized as a color description the function returns 0.
     This can be used to read a color name from a data file or to add an alpha
     channel to a named color in the upper byte of the returned value. 
     See `colorspec`.
    
     
  • Hiroki Motoyoshi

    Thank you for incorporating the feature request.
    I’ll give it a try!

     

Log in to post a comment.

Auth0 Logo