Color Objects: Difference between revisions

From PyMOLWiki
Jump to navigation Jump to search
No edit summary
(fix (imports, get_names))
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{Infobox script-repo
|type    = script
|author  = Gareth Stockwell
}}
<source lang="python">
<source lang="python">
#####################################################################
#####################################################################
Line 5: Line 10:
#
#
#####################################################################
#####################################################################
from pymol import cmd


def color_obj(rainbow=0):
def color_obj(rainbow=0):
 
         """
         """
       
AUTHOR  
AUTHOR  
 
         Gareth Stockwell
         Gareth Stockwell
 
USAGE
USAGE
 
         color_obj(rainbow=0)
         color_obj(rainbow=0)
        This function colours each object currently in the PyMOL heirarchy
        with a different colour.  Colours used are either the 22 named
        colours used by PyMOL (in which case the 23rd object, if it exists,
        gets the same colour as the first), or are the colours of the rainbow
SEE ALSO


This function colours each object currently in the PyMOL heirarchy
        util.color_objs()
with a different colour. Colours used are either the 22 named
colours used by PyMOL (in which case the 23rd object, if it exists,
gets the same colour as the first), or are the colours of the rainbow
 
         """
         """
 
# Process arguments
        # Process arguments
rainbow = int(rainbow)
        rainbow = int(rainbow)
 
         # Get names of all PyMOL objects
         # Get names of all PyMOL objects
obj_list = cmd.get_names('models')
        obj_list = cmd.get_names('objects')
 
if rainbow:
        if rainbow:
 
           print "\nColouring objects as rainbow\n"
           print "\nColouring objects as rainbow\n"
 
  nobj = len(obj_list)
          nobj = len(obj_list)
 
  # Create colours starting at blue(240) to red(0), using intervals
          # Create colours starting at blue(240) to red(0), using intervals
  # of 240/(nobj-1)
          # of 240/(nobj-1)
  for j in range(nobj):
          for j in range(nobj):
      hsv = (240-j*240/(nobj-1), 1, 1)
              hsv = (240-j*240/(nobj-1), 1, 1)
      # Convert to RGB
              # Convert to RGB
      rgb = hsv_to_rgb(hsv)
              rgb = hsv_to_rgb(hsv)
      # Define the new colour
              # Define the new colour
      cmd.set_color("col" + str(j), rgb)
              cmd.set_color("col" + str(j), rgb)
      print obj_list[j], rgb
              print obj_list[j], rgb
      # Colour the object
              # Colour the object
      cmd.color("col" + str(j), obj_list[j])
              cmd.color("col" + str(j), obj_list[j])
 
else:
        else:
 
  print "\nColouring objects using PyMOL defined colours\n"
          print "\nColouring objects using PyMOL defined colours\n"
 
  # List of available colours
          # List of available colours
  colours = ['red', 'green', 'blue', 'yellow', 'violet', 'cyan',    \
          colours = ['red', 'green', 'blue', 'yellow', 'violet', 'cyan',    \
           'salmon', 'lime', 'pink', 'slate', 'magenta', 'orange', 'marine', \
           'salmon', 'lime', 'pink', 'slate', 'magenta', 'orange', 'marine', \
  'olive', 'purple', 'teal', 'forest', 'firebrick', 'chocolate',    \
          'olive', 'purple', 'teal', 'forest', 'firebrick', 'chocolate',    \
  'wheat', 'white', 'grey' ]
          'wheat', 'white', 'grey' ]
  ncolours = len(colours)
          ncolours = len(colours)
 
  # Loop over objects
          # Loop over objects
  i = 0
          i = 0
  for obj in obj_list:
          for obj in obj_list:
      print "  ", obj, colours[i]
              print "  ", obj, colours[i]
      cmd.color(colours[i], obj)
              cmd.color(colours[i], obj)
      i = i+1
              i = i+1
      if(i == ncolours):
              if(i == ncolours):
        i = 0
                i = 0
 
 
# HSV to RGB routine taken from Robert L. Campbell's color_b.py script
# HSV to RGB routine taken from Robert L. Campbell's color_b.py script
#  See http://pldserver1.biochem.queensu.ca/~rlc/work/pymol/
#  See http://pldserver1.biochem.queensu.ca/~rlc/work/pymol/
# Original algorithm from: http://www.cs.rit.edu/~ncs/color/t_convert.html
# Original algorithm from: http://www.cs.rit.edu/~ncs/color/t_convert.html
def hsv_to_rgb(hsv):
def hsv_to_rgb(hsv):
 
         h = float(hsv[0])
         h = float(hsv[0])
         s = float(hsv[1])
         s = float(hsv[1])
         v = float(hsv[2])
         v = float(hsv[2])
 
         if( s == 0 ) :
         if( s == 0 ) :
                 #achromatic (grey)
                 #achromatic (grey)
                 r = g = b = v
                 r = g = b = v
 
         else:
         else:
                 # sector 0 to 5
                 # sector 0 to 5
Line 92: Line 102:
                 q = v * ( 1 - s * f )
                 q = v * ( 1 - s * f )
                 t = v * ( 1 - s * ( 1 - f ) )
                 t = v * ( 1 - s * ( 1 - f ) )
 
                 if i == 0:
                 if i == 0:
                         (r,g,b) = (v,t,p)
                         (r,g,b) = (v,t,p)
Line 108: Line 118:
                         (r,g,b) = (v,v,v)
                         (r,g,b) = (v,v,v)
                         print "error, i not equal 1-5"
                         print "error, i not equal 1-5"
 
         return [r,g,b]
         return [r,g,b]
   
   
 
# Add color_obj to the PyMOL command list  
# Add color_obj to the PyMOL command list  
cmd.extend("color_obj",color_obj)
cmd.extend("color_obj",color_obj)
</source>
</source>



Latest revision as of 03:34, 19 December 2011

Type Python Script
Download
Author(s) Gareth Stockwell
License
#####################################################################
#
# Colour by object
#
#####################################################################
 
from pymol import cmd

def color_obj(rainbow=0):
 
        """
 
AUTHOR 
 
        Gareth Stockwell
 
USAGE
 
        color_obj(rainbow=0)
 
        This function colours each object currently in the PyMOL heirarchy
        with a different colour.  Colours used are either the 22 named
        colours used by PyMOL (in which case the 23rd object, if it exists,
        gets the same colour as the first), or are the colours of the rainbow
 
SEE ALSO

        util.color_objs()
        """
 
        # Process arguments
        rainbow = int(rainbow)
 
        # Get names of all PyMOL objects
        obj_list = cmd.get_names('objects')
 
        if rainbow:
 
           print "\nColouring objects as rainbow\n"
 
           nobj = len(obj_list)
 
           # Create colours starting at blue(240) to red(0), using intervals
           # of 240/(nobj-1)
           for j in range(nobj):
              hsv = (240-j*240/(nobj-1), 1, 1)
              # Convert to RGB
              rgb = hsv_to_rgb(hsv)
              # Define the new colour
              cmd.set_color("col" + str(j), rgb)
              print obj_list[j], rgb
              # Colour the object
              cmd.color("col" + str(j), obj_list[j])
 
        else:
 
           print "\nColouring objects using PyMOL defined colours\n"
 
           # List of available colours
           colours = ['red', 'green', 'blue', 'yellow', 'violet', 'cyan',    \
           'salmon', 'lime', 'pink', 'slate', 'magenta', 'orange', 'marine', \
           'olive', 'purple', 'teal', 'forest', 'firebrick', 'chocolate',    \
           'wheat', 'white', 'grey' ]
           ncolours = len(colours)
 
           # Loop over objects
           i = 0
           for obj in obj_list:
              print "  ", obj, colours[i]
              cmd.color(colours[i], obj)
              i = i+1
              if(i == ncolours):
                 i = 0
 
 
# HSV to RGB routine taken from Robert L. Campbell's color_b.py script
#   See http://pldserver1.biochem.queensu.ca/~rlc/work/pymol/
# Original algorithm from: http://www.cs.rit.edu/~ncs/color/t_convert.html
def hsv_to_rgb(hsv):
 
        h = float(hsv[0])
        s = float(hsv[1])
        v = float(hsv[2])
 
        if( s == 0 ) :
                #achromatic (grey)
                r = g = b = v
 
        else:
                # sector 0 to 5
                h = h/60.            
                i = int(h)
                f = h - i                       # factorial part of h
                #print h,i,f
                p = v * ( 1 - s )
                q = v * ( 1 - s * f )
                t = v * ( 1 - s * ( 1 - f ) )
 
                if i == 0:
                        (r,g,b) = (v,t,p)
                elif i == 1:
                        (r,g,b) = (q,v,p)
                elif i == 2:
                        (r,g,b) = (p,v,t)
                elif i == 3:
                        (r,g,b) = (p,q,v)
                elif i == 4:
                        (r,g,b) = (t,p,v)
                elif i == 5:
                        (r,g,b) = (v,p,q)
                else:
                        (r,g,b) = (v,v,v)
                        print "error, i not equal 1-5"
 
        return [r,g,b]
 
 
 
# Add color_obj to the PyMOL command list 
cmd.extend("color_obj",color_obj)