static PyObject* py_set_colorid(PyObject *self, PyObject *args, PyObject *kwargs) { const char *kwnames[] = {"id", "rgb", NULL}; float rgb[3]; int colorid; VMDApp *app; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i(fff):color.set_colorid", (char**) kwnames, &colorid, &rgb[0], &rgb[1], &rgb[2])) return NULL; if (!(app = get_vmdapp())) return NULL; if (colorid >= app->num_regular_colors() || colorid < 0) { PyErr_Format(PyExc_ValueError, "color index '%d' out of range", colorid); return NULL; } app->color_change_rgb(app->color_name(colorid), rgb[0], rgb[1], rgb[2]); Py_INCREF(Py_None); return Py_None; }
static PyObject *set_colorid(PyObject *self, PyObject *args) { int colorid; float rgb[3]; if (!PyArg_ParseTuple(args, (char *)"i(fff)", &colorid, &rgb[0], &rgb[1], &rgb[2])) { return NULL; } VMDApp *app = get_vmdapp(); if (colorid >= app->num_regular_colors() || colorid < 0) { PyErr_SetString(PyExc_ValueError, (char *) "color index out of range"); return NULL; } const char *name = app->color_name(colorid); app->color_changevalue(name, rgb[0], rgb[1], rgb[2]); //We declare that we are returning a pyobject, so lets actually return one! Py_INCREF(Py_None); return Py_None; }
static PyObject* py_get_colorlist(PyObject *self, PyObject *args) { PyObject *newlist = NULL, *newtuple = NULL; int i, j, listlen; const char *name; float col[3]; VMDApp *app; if (!(app = get_vmdapp())) return NULL; listlen = app->num_regular_colors(); if (!(newlist = PyList_New(listlen))) goto failure; for (i = 0; i < listlen; i++) { name = app->color_name(i); if (!app->color_value(name, col, col+1, col+2)) goto failure; if (!(newtuple = PyTuple_New(3))) goto failure; for (j = 0; j < 3; j++) { PyTuple_SET_ITEM(newtuple, j, PyFloat_FromDouble(col[j])); if (PyErr_Occurred()) goto failure; } PyList_SET_ITEM(newlist, i, newtuple); if (PyErr_Occurred()) goto failure; } return newlist; failure: PyErr_SetString(PyExc_RuntimeError, "Problem getting color list"); Py_XDECREF(newlist); Py_XDECREF(newtuple); return NULL; }
static PyObject *get_colors(PyObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, (char *)"")) return NULL; VMDApp *app = get_vmdapp(); PyObject *newdict = PyDict_New(); for (int i=0; i<app->num_regular_colors(); i++) { float col[3]; const char *name = app->color_name(i); if (!app->color_value(name, col, col+1, col+2)) { PyErr_SetString(PyExc_ValueError, (char *) "Unable to get color definition"); return NULL; } PyObject *newtuple = PyTuple_New(3); for (int j=0; j<3; j++) PyTuple_SET_ITEM(newtuple, j, PyFloat_FromDouble(col[j])); PyDict_SetItemString(newdict, (char *)name, newtuple); } return newdict; }