From: Andrew Dalke (dalke_at_ks.uiuc.edu)
Date: Fri Jan 24 1997 - 11:44:45 CST

Hello,

  We were sitting around here talking about the different possible
ways to use the mailing list and decided that one helpful way would be
to have a short column describing some aspect of VMD that is of
general interest. I hope to present one of these every week or two.
If you like to give me feedback on the usefulness or appropriateness
of this column, please feel free to send me email.

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  For this first one, I'll discuss how VMD does color and how you can
tweak their definitions using the script language. Since most of this
is in the manual, I'll cover just a brief overview then describe what
you can do with this knowledge.

====== Background

  VMD's definition of colors comes from GL. A "color" (actually, a
"material property") has 5 properties: rgb color, ambient (also known
as diffuse), alpha, shininess, specular. Frankly, unless you know
what all these are already, the only ones you'll need to worry about
are the RGB color (for red/green/blue) and the alpha value. The
latter specifies the amount of transparancy, from 1 (opaque) to 0
(invisible). The RGB color is often used synonomously for material
property, and I'll be doing that below quite often. BTW, the data is
stored in ColorList.colorData, for those looking at code (is there
anyone here doing that?).

  There are 98 defined materials in VMD. The first 17 (yes, 17) are
the ones listed in the color form. The next 17 are the transparent
versions of the first set, with all the material characteristics
identical to their corresponding opaque version, except the
transparancy is set to 0.3. These are used when the "Transparent"
button is used in the graphics form.
  After these are the material definitions for the color scale. There
are again two sets of colors. The first set contains 32 opaque
definitions and the next 32 contain the transparent versions.
  There is, alas, no way to add new color definitions (okay, "material
definitions" :) to VMD without recompiling the code, but the exisiting
definitions can be changed a great deal. The next nearly hundred
lines(!) will tell you some ways to do this.

====== Querying VMD for Color Information

  You can use the "colorinfo" command to get information about each of
the colors, for instance, red is defined as:

vmd > foreach attrib {rgb alpha shininess ambient specular} {
 puts "$attrib [colorinfo $attrib red]"
 }

rgb 1.0 0.0 0.0
alpha 1.0
shininess 40.0
ambient 0.0 0.0 0.0
specular 1.0 1.0 1.0

In this case I could use the color name, red. The list of color names
is available as "colorinfo colors". For the transparent versions, I
can prefix the string "trans_", so:

vmd > colorinfo alpha trans_red
Info) 0.3

shows the alpha value of the transparent version of red. I could also
have used the color index (red is index 1, with blue starting the list
at 0, and trans_red is 1+17 = 18). By using the index I can also
access the color definitions for the color scale, like this:

vmd > colorinfo rgb 38
Info) 0.841936 0.358065 0.1

====== Changing Color Definitions (w/ a transparent example)

The color definitions can be changed with the 'color' command. To
make the red less intense you can use the sliders from the color menu,
or use the command 'color change rgb red 0.5 0 0' to set a new value.
All of the material attributes can be read or changed, which answers a
question I've been asked a few times -- how can you change the degree
of transparency for the transparent colors? The color menu does not
let you change that value using the mouse (that will be available in
the final release of 1.2), but you can write a script to do it for
you, like this:

proc change_transparency {new_alpha} {
  ## This will always get the correct colors even if VMD
  ## gains new definitions in the future
  set color_start [colorinfo num]
  set color_end [expr $color_start * 2]
  # Go through the list of colors (by index) and change their transp. value
  for {set color $color_start} {$color < $color_end} {incr color} {
    color change alpha $color $new_alpha
  }
}

If you run this with enough colors on the screen, you can actually see
the color definitions change one by one, though you will have to have
the "Transparent" button pressed to see it work. I'll discuss how to
speed things up someday if I get to talking about tuning scripts.

====== Your Own Color Scale

  Another question I've been asked is -- how can you change the color
scale definition? VMD has ten or so types of gradient, but you might
want to use some other method. For instance, suppose of the 32 colors
the first 15 should be red, then 2 whites, then 15 blues. You can use
the color command to modify the color scale values accordingly. The
only big tricky point is that you must update the transparent
defitions as well or things might look strange.

proc tricolor_scale {} {
  set color_start [expr [colorinfo num] * 2]
  for {set i 0} {$i < 32} {incr i} {
    if {$i == 0} {
      set r 1; set g 0; set b 0
    }
    if {$i == 15} {
      set r 1; set g 1; set b 1
    }
    if {$i == 17} {
      set r 0; set g 0; set b 1
    }
    color change rgb [expr $i + $color_start ] $r $g $b
    # get the transparent version as well
    color change rgb [expr $i + $color_start + 32] $r $g $b
  }
}

After you've copied this to the VMD console, run 'tricolor_scale' to
set the new definition. Actually, here's a fun way to see it work:
  1) load a molecule
  2) use the graphics form to color by beta
  3) enter the following two lines, which sets the beta value equal to
the atom index:
        set sel [atomselect top all]
        $sel set beta [$sel get index]
  5) run "tricolor_scale"

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  Well, this proved to be a bit longer than I thought it would, but I
hope it proves helpful for you,

                                                Andrew
                                                dalke_at_ks.uiuc.edu

P.S.
  There are now 51 people on the list (and names of list members will
not be given out to others).