static PyObject * pygimp_rgb_parse_css(PyObject *self, PyObject *args, PyObject *kwargs) { char *css; int len; GimpRGB rgb; gboolean success, with_alpha = FALSE; static char *kwlist[] = { "css", "with_alpha", NULL }; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|i:rgb_parse_css", kwlist, &css, &len, &with_alpha)) return NULL; if (with_alpha) success = gimp_rgba_parse_css(&rgb, css, len); else { rgb.a = 1.0; success = gimp_rgb_parse_css(&rgb, css, len); } if (!success) { PyErr_SetString(PyExc_ValueError, "unable to parse CSS color"); return NULL; } return pygimp_rgb_new(&rgb); }
static PyObject * pygimp_rgb_list_names(PyObject *self) { int num_names, i; const char **names; GimpRGB *colors; PyObject *dict, *color; num_names = gimp_rgb_list_names(&names, &colors); dict = PyDict_New(); if (!dict) goto cleanup; for (i = 0; i < num_names; i++) { color = pygimp_rgb_new(&colors[i]); if (!color) goto bail; if (PyDict_SetItemString(dict, names[i], color) < 0) { Py_DECREF(color); goto bail; } Py_DECREF(color); } goto cleanup; bail: Py_DECREF(dict); dict = NULL; cleanup: g_free(names); g_free(colors); return dict; }
static PyObject * pygimp_bilinear_color(PyObject *self, PyObject *args, PyObject *kwargs, gboolean with_alpha) { gdouble x, y; GimpRGB values[4]; GimpRGB rgb; PyObject *py_values, *v; int i, success; static char *kwlist[] = { "x", "y", "values", NULL }; if (!PyArg_ParseTupleAndKeywords(args, kwargs, with_alpha ? "ddO:bilinear_rgba" : "ddO:bilinear_rgb", kwlist, &x, &y, &py_values)) return NULL; if (!PySequence_Check(py_values) || PySequence_Size(py_values) != 4) { PyErr_SetString(PyExc_TypeError, "values is not a sequence of 4 items"); return NULL; } for (i = 0; i < 4; i++) { v = PySequence_GetItem(py_values, i); success = pygimp_rgb_from_pyobject(v, &values[i]); Py_DECREF(v); if (!success) { PyErr_Format(PyExc_TypeError, "values[%d] is not a GimpRGB", i); return NULL; } } if (with_alpha) rgb = gimp_bilinear_rgba(x, y, values); else rgb = gimp_bilinear_rgb(x, y, values); return pygimp_rgb_new(&rgb); }
static PyObject * pygimp_rgb_parse_hex(PyObject *self, PyObject *args, PyObject *kwargs) { char *hex; int len; GimpRGB rgb; gboolean success; static char *kwlist[] = { "hex", NULL }; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#:rgb_parse_hex", kwlist, &hex, &len)) return NULL; rgb.a = 1.0; success = gimp_rgb_parse_hex(&rgb, hex, len); if (!success) { PyErr_SetString(PyExc_ValueError, "unable to parse hex value"); return NULL; } return pygimp_rgb_new(&rgb); }
PyObject * pygimp_param_to_tuple(int nparams, const GimpParam *params) { PyObject *args, *tmp; int i, j, n; args = PyTuple_New(nparams); for (i = 0; i < nparams && params[i].type != GIMP_PDB_END; i++) { PyObject *value = NULL; switch(params[i].type) { case GIMP_PDB_INT32: value = PyInt_FromLong(params[i].data.d_int32); break; case GIMP_PDB_INT16: value = PyInt_FromLong(params[i].data.d_int16); break; case GIMP_PDB_INT8: value = PyInt_FromLong(params[i].data.d_int8); break; case GIMP_PDB_FLOAT: value = PyFloat_FromDouble(params[i].data.d_float); break; case GIMP_PDB_STRING: if (params[i].data.d_string == NULL) { Py_INCREF(Py_None); value = Py_None; } else value = PyString_FromString(params[i].data.d_string); break; /* For these to work, the previous argument must have * been an integer */ case GIMP_PDB_INT32ARRAY: if (params[i].data.d_int32array == NULL) { value = PyTuple_New(0); break; } if ((tmp=PyTuple_GetItem(args, i-1)) == NULL) { Py_DECREF(args); return NULL; } if (!PyInt_Check(tmp)) { PyErr_SetString(PyExc_TypeError, "count type must be integer"); Py_DECREF(args); return NULL; } n = PyInt_AsLong(tmp); value = PyTuple_New(n); for (j = 0; j < n; j++) PyTuple_SetItem(value, j, PyInt_FromLong(params[i].data.d_int32array[j])); break; case GIMP_PDB_INT16ARRAY: if (params[i].data.d_int16array == NULL) { value = PyTuple_New(0); break; } if ((tmp=PyTuple_GetItem(args, i-1)) == NULL) { Py_DECREF(args); return NULL; } if (!PyInt_Check(tmp)) { PyErr_SetString(PyExc_TypeError, "count type must be integer"); Py_DECREF(args); return NULL; } n = PyInt_AsLong(tmp); value = PyTuple_New(n); for (j = 0; j < n; j++) PyTuple_SetItem(value, j, PyInt_FromLong(params[i].data.d_int16array[j])); break; case GIMP_PDB_INT8ARRAY: if (params[i].data.d_int8array == NULL) { value = PyTuple_New(0); break; } if ((tmp=PyTuple_GetItem(args, i-1)) == NULL) { Py_DECREF(args); return NULL; } if (!PyInt_Check(tmp)) { PyErr_SetString(PyExc_TypeError, "count type must be integer"); Py_DECREF(args); return NULL; } n = PyInt_AsLong(tmp); value = PyTuple_New(n); for (j = 0; j < n; j++) PyTuple_SetItem(value, j, PyInt_FromLong(params[i].data.d_int8array[j])); break; case GIMP_PDB_FLOATARRAY: if (params[i].data.d_floatarray == NULL) { value = PyTuple_New(0); break; } if ((tmp=PyTuple_GetItem(args, i-1)) == NULL) { Py_DECREF(args); return NULL; } if (!PyInt_Check(tmp)) { PyErr_SetString(PyExc_TypeError, "count type must be integer"); Py_DECREF(args); return NULL; } n = PyInt_AsLong(tmp); value = PyTuple_New(n); for (j = 0; j < n; j++) PyTuple_SetItem(value, j, PyFloat_FromDouble(params[i].data.d_floatarray[j])); break; case GIMP_PDB_STRINGARRAY: if (params[i].data.d_stringarray == NULL) { value = PyTuple_New(0); break; } if ((tmp=PyTuple_GetItem(args, i-1)) == NULL) { Py_DECREF(args); return NULL; } if (!PyInt_Check(tmp)) { PyErr_SetString(PyExc_TypeError, "count type must be integer"); Py_DECREF(args); return NULL; } n = PyInt_AsLong(tmp); value = PyTuple_New(n); for (j = 0; j < n; j++) PyTuple_SetItem(value, j, params[i].data.d_stringarray[j] ? PyString_FromString(params[i].data.d_stringarray[j]) : PyString_FromString("")); break; case GIMP_PDB_COLOR: value = pygimp_rgb_new(¶ms[i].data.d_color); break; case GIMP_PDB_REGION: value = Py_BuildValue("(iiii)", (int) params[i].data.d_region.x, (int) params[i].data.d_region.y, (int) params[i].data.d_region.width, (int) params[i].data.d_region.height); break; case GIMP_PDB_DISPLAY: value = pygimp_display_new(params[i].data.d_display); break; case GIMP_PDB_IMAGE: value = pygimp_image_new(params[i].data.d_image); break; case GIMP_PDB_LAYER: value = pygimp_layer_new(params[i].data.d_layer); break; case GIMP_PDB_CHANNEL: value = pygimp_channel_new(params[i].data.d_channel); break; case GIMP_PDB_DRAWABLE: value = pygimp_drawable_new(NULL, params[i].data.d_drawable); break; case GIMP_PDB_SELECTION: value = pygimp_layer_new(params[i].data.d_selection); break; case GIMP_PDB_BOUNDARY: value = PyInt_FromLong(params[i].data.d_boundary); break; case GIMP_PDB_VECTORS: value = pygimp_vectors_new(params[i].data.d_vectors); break; case GIMP_PDB_PARASITE: value = pygimp_parasite_new(gimp_parasite_copy( &(params[i].data.d_parasite))); break; case GIMP_PDB_STATUS: value = PyInt_FromLong(params[i].data.d_status); break; case GIMP_PDB_END: break; } PyTuple_SetItem(args, i, value); } return args; }
static PyObject * pf_get_bg_color(PyGimpPixelFetcher *self, void *closure) { return pygimp_rgb_new(&self->bg_color); }