Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

cmd_color.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2011 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: cmd_color.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.32 $       $Date: 2011/02/17 17:00:14 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *   text commands for color controls
00019  ***************************************************************************/
00020 
00021 #include <tcl.h>
00022 #include <stdlib.h>
00023 #include "VMDApp.h"
00024 #include "TclCommands.h"
00025 #include "config.h"
00026 
00027 int text_cmd_color(ClientData cd, Tcl_Interp *interp, int argc, const char *argv[]) {
00028 
00029   VMDApp *app = (VMDApp *)cd;
00030 
00031   if (argc < 2) {
00032     // might as well return some useful information 
00033     Tcl_SetResult(interp, 
00034       (char *)
00035       "color change rgb <color> [<grayscale> | <r g b>]\n"
00036       "   (when no value is specified, the color is reset to its default value)\n"
00037       "color scale [method|midpoint|min|max] <value>\n"
00038       "color scale colors <method> [<mincolor> <midcolor> <maxcolor>]\n"
00039       "color <category> <name> [new color]",
00040       TCL_STATIC);
00041     return TCL_ERROR;
00042   }
00043 
00044   if (!strupncmp(argv[1], "change", CMDLEN) && argc > 3) {
00045     float r = 0.5, g = 0.5, b = 0.5;
00046     if (app->color_index(argv[3]) < 0) { 
00047       Tcl_SetResult(interp,  (char *) "color change: invalid color specified", TCL_STATIC);
00048       return TCL_ERROR;
00049     }
00050     if (argc == 4) {
00051       // Get the default values for the color
00052       if (!app->color_default_value(argv[3], &r, &g, &b)) {
00053         Tcl_SetResult(interp, (char *) "Unable to get default values for color", TCL_STATIC);
00054         return TCL_ERROR;
00055       } 
00056       app->color_changevalue(argv[3], r, g, b);
00057       return TCL_OK;
00058     } else {
00059       double rr;
00060       if (Tcl_GetDouble(interp, argv[4], &rr) != TCL_OK) {
00061         Tcl_AppendResult(interp,  " in color change", NULL);
00062         return TCL_ERROR;
00063       }
00064       r = (float) rr;
00065       if (argc == 5) {
00066         if (!app->color_changevalue(argv[3], r, r, r)) {
00067           Tcl_SetResult(interp, (char *) "Unable to change color", TCL_STATIC);
00068           return TCL_ERROR;
00069         }
00070       } else if (argc == 7) {
00071         double gg, bb;
00072         if (Tcl_GetDouble(interp, argv[5], &gg) != TCL_OK ||
00073           Tcl_GetDouble(interp, argv[6], &bb) != TCL_OK) {
00074           Tcl_AppendResult(interp, " in color change", NULL);
00075           return TCL_ERROR;
00076         }
00077         g = (float) gg;
00078         b = (float) bb;
00079         if (!app->color_changevalue(argv[3], r, g, b)) {
00080           Tcl_SetResult(interp, (char *) "Unable to change color", TCL_STATIC);
00081           return TCL_ERROR;
00082         }
00083       } else {
00084         Tcl_SetResult(interp, (char *) "color change needs 1 (or 3) parameters", TCL_STATIC);
00085         return TCL_ERROR;
00086       }
00087       return TCL_OK;
00088     }
00089   } else if (!strupncmp(argv[1], "scale", CMDLEN)) {
00091     if (argc >= 4 && !strupncmp(argv[2], "colors", CMDLEN)) {
00092       if (argc == 4) {
00093         int ind = app->colorscale_method_index(argv[3]);
00094         float vals[3][3];
00095         if (!app->get_colorscale_colors(ind, vals[0], vals[1], vals[2])) {
00096           Tcl_AppendResult(interp, "no colors available for method '", argv[3], "'.", NULL);
00097           return TCL_ERROR;
00098         }
00099         Tcl_Obj *result = Tcl_NewListObj(0,NULL);
00100         for (int i=0; i<3; i++) {
00101           Tcl_Obj *elem = Tcl_NewListObj(0, NULL);
00102           for (int j=0; j<3; j++) {
00103             Tcl_ListObjAppendElement(interp,elem,Tcl_NewDoubleObj(vals[i][j]));
00104           }
00105           Tcl_ListObjAppendElement(interp, result, elem);
00106         }
00107         Tcl_SetObjResult(interp, result);
00108         return TCL_OK;
00109       } else if (argc == 7) {
00110         int ind = app->colorscale_method_index(argv[3]);
00111         float vals[3][3];
00112         for (int i=0; i<3; i++) {
00113           // first interpret as vector
00114           int rc = tcl_get_vector(argv[4+i], vals[i], interp);
00115           if (rc != TCL_OK) {
00116             if (!app->color_value(argv[4+i], vals[i]+0, vals[i]+1, vals[i]+2)) {
00117               return TCL_ERROR;
00118             }
00119             // clear error since lookup by color name was successful.
00120             Tcl_ResetResult(interp);
00121           }
00122         }
00123         if (!app->set_colorscale_colors(ind, vals[0], vals[1], vals[2])) {
00124           Tcl_AppendResult(interp, "Unable to set colorscale colors for method '", argv[3], "'.", NULL);
00125           return TCL_ERROR;
00126         }
00127         return TCL_OK;
00128       }
00129     }
00130     if (argc == 4) {
00131       if (!strupncmp(argv[2], "method", CMDLEN)) {
00132         int ind = app->colorscale_method_index(argv[3]);
00133         if (ind < 0) {
00134           char tmpstring[1024];
00135           sprintf(tmpstring, "color scale method '%s' not recognized", argv[3]);
00136           Tcl_SetResult(interp, tmpstring, TCL_VOLATILE);
00137           return TCL_ERROR;
00138         }
00139         app->colorscale_setmethod(ind); 
00140       } else {
00141         float mid=0, min=0, max=0;
00142         app->colorscale_info(&mid, &min, &max); 
00143         float newval = (float) atof(argv[3]);
00144         if (!strupncmp(argv[2], "midpoint", CMDLEN)) mid = newval;
00145         else if (!strupncmp(argv[2], "min", CMDLEN)) min = newval; 
00146         else if (!strupncmp(argv[2], "max", CMDLEN)) max = newval;
00147         else {
00148           char tmpstring[1024]; 
00149           sprintf(tmpstring, "color scale option '%s' not recognized", argv[2]);
00150           Tcl_SetResult(interp, tmpstring, TCL_VOLATILE);
00151           return TCL_ERROR;
00152         }
00153         app->colorscale_setvalues(mid, min, max);
00154       }
00155     } else {
00156       Tcl_SetResult(interp, (char *) "color scale [method|midpoint|min|max] <value>", TCL_STATIC);
00157       return TCL_ERROR;
00158     }  
00159   } else if ((argc == 3 || argc == 4) && !strupncmp(argv[1], "restype", CMDLEN)) {
00160     if (argc == 3) {
00161       const char *result = app->color_get_restype(argv[2]);
00162       if (!result) {
00163         Tcl_AppendResult(interp, "No restype for residue '", argv[2], "'",
00164             NULL);
00165         return TCL_ERROR;
00166       }
00167       Tcl_SetResult(interp, (char *)result, TCL_STATIC);
00168     } else { //argc==4
00169       if (!app->color_set_restype(argv[2], argv[3])) {
00170         if (!app->color_changename(argv[1], argv[2], argv[3])) {
00171           Tcl_AppendResult(interp, "Unable to set restype: invalid restype '",
00172                            argv[3], 
00173                            "' specified -- or unable to change color name", 
00174                            NULL);
00175           return TCL_ERROR;
00176         }
00177       }
00178     }
00179   } else if(argc == 3) {
00180     // Return the color string for the specified color category/name
00181     const char *colorname;
00182     if (app->color_get_from_name(argv[1], argv[2], &colorname)) {
00183       Tcl_SetResult(interp, (char*) colorname, TCL_STATIC); 
00184       return TCL_OK;
00185     } else {
00186       Tcl_SetResult(interp, (char *) "Unable to get color name", TCL_STATIC); 
00187       return TCL_ERROR;
00188     }
00189   } else if(argc == 4) {
00190     if (!app->color_changename(argv[1], argv[2], argv[3])) {
00191       Tcl_SetResult(interp, (char *) "Unable to change color name", TCL_STATIC); 
00192       return TCL_ERROR;
00193     }
00194   } else if (argc == 6 && !strupncmp(argv[1], "add", CMDLEN) 
00195                        && !strupncmp(argv[2], "item", CMDLEN)) {
00196     if (!app->color_add_item(argv[3], argv[4], argv[5])) {
00197       Tcl_SetResult(interp, "Error adding color item.", TCL_STATIC);
00198       return TCL_ERROR;
00199     }
00200   } else {
00201     return TCL_ERROR;
00202   }
00203   
00204   // if here, everything worked out ok
00205   return TCL_OK;
00206 }
00207 

Generated on Sat May 26 01:47:45 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002