| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | /* 
 | 
					
						
							| 
									
										
										
										
											2007-12-17 20:21:06 +00:00
										 |  |  |  * $Id: rgbTuple.c 11713 2007-08-19 19:06:42Z campbellbarton $ | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. The Blender | 
					
						
							|  |  |  |  * Foundation also sells licenses for use in proprietary software under | 
					
						
							|  |  |  |  * the Blender License.  See http://www.blender.org/BL/ for information
 | 
					
						
							|  |  |  |  * about this. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This is a new part of Blender. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Willian P. Germano | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL/BL DUAL LICENSE BLOCK ***** | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-18 03:50:37 +00:00
										 |  |  | #include "rgbTuple.h" /*This must come first */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "gen_utils.h"
 | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* This file is heavily based on the old bpython Constant object code in
 | 
					
						
							|  |  |  |    Blender */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							|  |  |  | /* Python rgbTuple_Type callback function prototypes:                        */ | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *rgbTuple_getAttr( BPy_rgbTuple * self, char *name ); | 
					
						
							|  |  |  | static int rgbTuple_setAttr( BPy_rgbTuple * self, char *name, PyObject * v ); | 
					
						
							|  |  |  | static PyObject *rgbTuple_repr( BPy_rgbTuple * self ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-21 19:48:40 +00:00
										 |  |  | static int rgbTupleLength( void ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *rgbTupleSubscript( BPy_rgbTuple * self, PyObject * key ); | 
					
						
							|  |  |  | static int rgbTupleAssSubscript( BPy_rgbTuple * self, PyObject * who, | 
					
						
							|  |  |  | 				 PyObject * cares ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *rgbTupleItem( BPy_rgbTuple * self, int i ); | 
					
						
							|  |  |  | static int rgbTupleAssItem( BPy_rgbTuple * self, int i, PyObject * ob ); | 
					
						
							|  |  |  | static PyObject *rgbTupleSlice( BPy_rgbTuple * self, int begin, int end ); | 
					
						
							|  |  |  | static int rgbTupleAssSlice( BPy_rgbTuple * self, int begin, int end, | 
					
						
							|  |  |  | 			     PyObject * seq ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							|  |  |  | /* Python rgbTuple_Type Mapping Methods table:                               */ | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyMappingMethods rgbTupleAsMapping = { | 
					
						
							|  |  |  | 	( inquiry ) rgbTupleLength,	/* mp_length        */ | 
					
						
							|  |  |  | 	( binaryfunc ) rgbTupleSubscript,	/* mp_subscript     */ | 
					
						
							|  |  |  | 	( objobjargproc ) rgbTupleAssSubscript,	/* mp_ass_subscript */ | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							|  |  |  | /* Python rgbTuple_Type Sequence Methods table:                              */ | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PySequenceMethods rgbTupleAsSequence = { | 
					
						
							|  |  |  | 	( inquiry ) rgbTupleLength,	/* sq_length */ | 
					
						
							|  |  |  | 	( binaryfunc ) 0,	/* sq_concat */ | 
					
						
							|  |  |  | 	( intargfunc ) 0,	/* sq_repeat */ | 
					
						
							|  |  |  | 	( intargfunc ) rgbTupleItem,	/* sq_item */ | 
					
						
							|  |  |  | 	( intintargfunc ) rgbTupleSlice,	/* sq_slice */ | 
					
						
							|  |  |  | 	( intobjargproc ) rgbTupleAssItem,	/* sq_ass_item */ | 
					
						
							|  |  |  | 	( intintobjargproc ) rgbTupleAssSlice,	/* sq_ass_slice       */ | 
					
						
							| 
									
										
										
										
											2005-10-03 22:06:16 +00:00
										 |  |  | 	0,0,0 | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							|  |  |  | /* Python rgbTuple_Type structure definition:                                */ | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyTypeObject rgbTuple_Type = { | 
					
						
							|  |  |  | 	PyObject_HEAD_INIT( NULL )  | 
					
						
							|  |  |  | 	0,	/* ob_size */ | 
					
						
							|  |  |  | 	"rgbTuple",		/* tp_name */ | 
					
						
							| 
									
										
										
										
											2007-08-19 19:06:42 +00:00
										 |  |  | 	sizeof( BPy_rgbTuple ),			/* tp_basicsize */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	0,			/* tp_itemsize */ | 
					
						
							|  |  |  | 	/* methods */ | 
					
						
							| 
									
										
										
										
											2007-08-19 19:06:42 +00:00
										 |  |  | 	0,			/* tp_dealloc */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	0,			/* tp_print */ | 
					
						
							|  |  |  | 	( getattrfunc ) rgbTuple_getAttr,	/* tp_getattr */ | 
					
						
							|  |  |  | 	( setattrfunc ) rgbTuple_setAttr,	/* tp_setattr */ | 
					
						
							|  |  |  | 	0,			/* tp_compare */ | 
					
						
							|  |  |  | 	( reprfunc ) rgbTuple_repr,	/* tp_repr */ | 
					
						
							|  |  |  | 	0,			/* tp_as_number */ | 
					
						
							|  |  |  | 	&rgbTupleAsSequence,	/* tp_as_sequence */ | 
					
						
							|  |  |  | 	&rgbTupleAsMapping,	/* tp_as_mapping */ | 
					
						
							|  |  |  | 	0,			/* tp_as_hash */ | 
					
						
							| 
									
										
										
										
											2007-08-19 19:06:42 +00:00
										 |  |  | 	0, 0, 0, 0, 0, Py_TPFLAGS_DEFAULT, | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	0,			/* tp_doc */ | 
					
						
							|  |  |  | 	0, 0, 0, 0, 0, 0, | 
					
						
							|  |  |  | 	0,			/* tp_methods */ | 
					
						
							|  |  |  | 	0,			/* tp_members */ | 
					
						
							| 
									
										
										
										
											2007-08-19 19:06:42 +00:00
										 |  |  | 	0,     /* struct PyGetSetDef *tp_getset; */ | 
					
						
							|  |  |  | 	0,                       /* struct _typeobject *tp_base; */ | 
					
						
							|  |  |  | 	0,                       /* PyObject *tp_dict; */ | 
					
						
							|  |  |  | 	0,                       /* descrgetfunc tp_descr_get; */ | 
					
						
							|  |  |  | 	0,                       /* descrsetfunc tp_descr_set; */ | 
					
						
							|  |  |  | 	0,                          /* long tp_dictoffset; */ | 
					
						
							|  |  |  | 	0,                       /* initproc tp_init; */ | 
					
						
							|  |  |  | 	0,                       /* allocfunc tp_alloc; */ | 
					
						
							|  |  |  | 	0,                       /* newfunc tp_new; */ | 
					
						
							|  |  |  | 	/*  Low-level free-memory routine */ | 
					
						
							|  |  |  | 	0,                       /* freefunc tp_free;  */ | 
					
						
							|  |  |  | 	/* For PyObject_IS_GC */ | 
					
						
							|  |  |  | 	0,                       /* inquiry tp_is_gc;  */ | 
					
						
							|  |  |  | 	0,                       /* PyObject *tp_bases; */ | 
					
						
							|  |  |  | 	/* method resolution order */ | 
					
						
							|  |  |  | 	0,                       /* PyObject *tp_mro;  */ | 
					
						
							|  |  |  | 	0,                       /* PyObject *tp_cache; */ | 
					
						
							|  |  |  | 	0,                       /* PyObject *tp_subclasses; */ | 
					
						
							|  |  |  | 	0,                       /* PyObject *tp_weaklist; */ | 
					
						
							|  |  |  | 	0 | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							|  |  |  | /* Function:              rgbTuple_New                                       */ | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *rgbTuple_New( float *rgb[3] ) | 
					
						
							| 
									
										
										
										
											2003-05-28 04:36:18 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-08-19 19:06:42 +00:00
										 |  |  | 	BPy_rgbTuple *rgbTuple = PyObject_NEW( BPy_rgbTuple, &rgbTuple_Type ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( rgbTuple == NULL ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 					      "couldn't create rgbTuple object" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	rgbTuple->rgb[0] = rgb[0]; | 
					
						
							|  |  |  | 	rgbTuple->rgb[1] = rgb[1]; | 
					
						
							|  |  |  | 	rgbTuple->rgb[2] = rgb[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return ( PyObject * ) rgbTuple; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							|  |  |  | /* Functions:      rgbTuple_getCol and rgbTuple_setCol                       */ | 
					
						
							|  |  |  | /* Description:    These functions get/set rgb color triplet values.  The    */ | 
					
						
							|  |  |  | /*                 get function returns a tuple, the set one accepts three   */ | 
					
						
							|  |  |  | /*                 floats (separated or in a tuple) as arguments.            */ | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *rgbTuple_getCol( BPy_rgbTuple * self ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-06-29 08:59:26 +00:00
										 |  |  | 	return Py_BuildValue( "[fff]", *(self->rgb[0]), | 
					
						
							| 
									
										
										
										
											2005-09-21 19:48:40 +00:00
										 |  |  | 			 			*(self->rgb[1]), *(self->rgb[2])); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-21 19:48:40 +00:00
										 |  |  | int rgbTuple_setCol( BPy_rgbTuple * self, PyObject * args ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-09-21 19:48:40 +00:00
										 |  |  | 	int ok = 0; | 
					
						
							| 
									
										
										
										
											2005-10-03 22:06:16 +00:00
										 |  |  | 	int i; | 
					
						
							|  |  |  | 	float num[3]={0,0,0}; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-21 19:48:40 +00:00
										 |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * since rgbTuple_getCol() returns a list, be sure we accept a list | 
					
						
							|  |  |  | 	 * as valid input | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( PyObject_Length( args ) == 3 ) { | 
					
						
							| 
									
										
										
										
											2005-10-03 22:06:16 +00:00
										 |  |  | 		if ( PyList_Check ( args ) ) { | 
					
						
							| 
									
										
										
										
											2005-09-21 19:48:40 +00:00
										 |  |  | 			ok = 1; | 
					
						
							| 
									
										
										
										
											2005-10-03 22:06:16 +00:00
										 |  |  | 			for( i = 0; i < 3; ++i ) { | 
					
						
							|  |  |  | 				PyObject *tmp = PyList_GET_ITEM( args, i ); | 
					
						
							|  |  |  | 				if( !PyNumber_Check ( tmp ) ) { | 
					
						
							|  |  |  | 					ok = 0; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				num[i] = (float)PyFloat_AsDouble( tmp ); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-09-21 19:48:40 +00:00
										 |  |  | 		} else | 
					
						
							| 
									
										
										
										
											2005-10-03 22:06:16 +00:00
										 |  |  | 			ok = PyArg_ParseTuple( args, "fff", &num[0], &num[1], &num[2] ); | 
					
						
							| 
									
										
										
										
											2005-09-21 19:48:40 +00:00
										 |  |  | 	} else | 
					
						
							| 
									
										
										
										
											2005-10-03 22:06:16 +00:00
										 |  |  | 		ok = PyArg_ParseTuple( args, "|(fff)", &num[0], &num[1], &num[2] ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !ok ) | 
					
						
							| 
									
										
										
										
											2005-09-21 19:48:40 +00:00
										 |  |  | 		return EXPP_ReturnIntError( PyExc_TypeError, | 
					
						
							|  |  |  | 					      "expected [f,f,f], (f,f,f) or f,f,f as arguments (or nothing)" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-03 22:06:16 +00:00
										 |  |  | 	for( i = 0; i < 3; ++i ) | 
					
						
							|  |  |  | 		*( self->rgb[i] ) = EXPP_ClampFloat( num[i], 0.0, 1.0 ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-21 19:48:40 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2003-07-05 01:18:41 +00:00
										 |  |  | /* Function:    rgbTuple_getAttr                                             */ | 
					
						
							| 
									
										
										
										
											2003-06-24 07:21:17 +00:00
										 |  |  | /* Description: This is a callback function for the BPy_rgbTuple type. It is */ | 
					
						
							|  |  |  | /*              the function that accesses BPy_rgbTuple member variables and */ | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | /*              methods.                                                     */ | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *rgbTuple_getAttr( BPy_rgbTuple * self, char *name ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( strcmp( name, "__members__" ) == 0 ) | 
					
						
							|  |  |  | 		return Py_BuildValue( "[s,s,s]", "R", "G", "B" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	else if( !strcmp( name, "R" ) || !strcmp( name, "r" ) ) | 
					
						
							|  |  |  | 		i = 0; | 
					
						
							|  |  |  | 	else if( !strcmp( name, "G" ) || !strcmp( name, "g" ) ) | 
					
						
							|  |  |  | 		i = 1; | 
					
						
							|  |  |  | 	else if( !strcmp( name, "B" ) || !strcmp( name, "b" ) ) | 
					
						
							|  |  |  | 		i = 2; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 						"attribute not found" ) ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-26 04:39:31 +00:00
										 |  |  | 	return PyFloat_FromDouble( (double)(*( self->rgb[i] )) ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2003-07-05 01:18:41 +00:00
										 |  |  | /* Function:    rgbTuple_setAttr                                             */ | 
					
						
							| 
									
										
										
										
											2003-06-24 07:21:17 +00:00
										 |  |  | /* Description: This is a callback function for the BPy_rgbTuple type. It is */ | 
					
						
							|  |  |  | /*              the function that changes BPy_rgbTuple member variables.     */ | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static int rgbTuple_setAttr( BPy_rgbTuple * self, char *name, PyObject * v ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	float value; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !PyArg_Parse( v, "f", &value ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnIntError( PyExc_TypeError, | 
					
						
							|  |  |  | 					    "expected float argument" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	value = EXPP_ClampFloat( value, 0.0, 1.0 ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !strcmp( name, "R" ) || !strcmp( name, "r" ) ) | 
					
						
							|  |  |  | 		*( self->rgb[0] ) = value; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	else if( !strcmp( name, "G" ) || !strcmp( name, "g" ) ) | 
					
						
							|  |  |  | 		*( self->rgb[1] ) = value; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	else if( !strcmp( name, "B" ) || !strcmp( name, "b" ) ) | 
					
						
							|  |  |  | 		*( self->rgb[2] ) = value; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	else | 
					
						
							|  |  |  | 		return ( EXPP_ReturnIntError( PyExc_AttributeError, | 
					
						
							|  |  |  | 					      "attribute not found" ) ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							|  |  |  | /* Section:    rgbTuple as Mapping                                           */ | 
					
						
							|  |  |  | /*             These functions provide code to access rgbTuple objects as    */ | 
					
						
							|  |  |  | /*             mappings.                                                     */ | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2005-09-21 19:48:40 +00:00
										 |  |  | static int rgbTupleLength( void ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return 3; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *rgbTupleSubscript( BPy_rgbTuple * self, PyObject * key ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *name = NULL; | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( PyNumber_Check( key ) ) | 
					
						
							|  |  |  | 		return rgbTupleItem( self, ( int ) PyInt_AsLong( key ) ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !PyArg_ParseTuple( key, "s", &name ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_TypeError, | 
					
						
							|  |  |  | 					      "expected int or string argument" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !strcmp( name, "R" ) || !strcmp( name, "r" ) ) | 
					
						
							|  |  |  | 		i = 0; | 
					
						
							|  |  |  | 	else if( !strcmp( name, "G" ) || !strcmp( name, "g" ) ) | 
					
						
							|  |  |  | 		i = 1; | 
					
						
							|  |  |  | 	else if( !strcmp( name, "B" ) || !strcmp( name, "b" ) ) | 
					
						
							|  |  |  | 		i = 2; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		return EXPP_ReturnPyObjError( PyExc_AttributeError, name ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-26 04:39:31 +00:00
										 |  |  | 	return PyFloat_FromDouble( (double)(*( self->rgb[i] )) ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static int rgbTupleAssSubscript( BPy_rgbTuple * self, PyObject * key, | 
					
						
							|  |  |  | 				 PyObject * v ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	char *name = NULL; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !PyNumber_Check( v ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnIntError( PyExc_TypeError, | 
					
						
							|  |  |  | 					    "value to assign must be a number" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( PyNumber_Check( key ) ) | 
					
						
							|  |  |  | 		return rgbTupleAssItem( self, ( int ) PyInt_AsLong( key ), v ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !PyArg_Parse( key, "s", &name ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnIntError( PyExc_TypeError, | 
					
						
							|  |  |  | 					    "expected int or string argument" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !strcmp( name, "R" ) || !strcmp( name, "r" ) ) | 
					
						
							|  |  |  | 		i = 0; | 
					
						
							|  |  |  | 	else if( !strcmp( name, "G" ) || !strcmp( name, "g" ) ) | 
					
						
							|  |  |  | 		i = 1; | 
					
						
							|  |  |  | 	else if( !strcmp( name, "B" ) || !strcmp( name, "b" ) ) | 
					
						
							|  |  |  | 		i = 2; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		return EXPP_ReturnIntError( PyExc_AttributeError, name ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-18 03:50:37 +00:00
										 |  |  | 	*( self->rgb[i] ) = EXPP_ClampFloat( (float)PyFloat_AsDouble( v ), 0.0, 1.0 ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							|  |  |  | /* Section:    rgbTuple as Sequence                                          */ | 
					
						
							|  |  |  | /*             These functions provide code to access rgbTuple objects as    */ | 
					
						
							|  |  |  | /*             sequences.                                                    */ | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *rgbTupleItem( BPy_rgbTuple * self, int i ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( i < 0 || i >= 3 ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_IndexError, | 
					
						
							|  |  |  | 					      "array index out of range" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-26 04:39:31 +00:00
										 |  |  | 	return PyFloat_FromDouble( (long)(*( self->rgb[i] )) ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *rgbTupleSlice( BPy_rgbTuple * self, int begin, int end ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *list; | 
					
						
							|  |  |  | 	int count; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( begin < 0 ) | 
					
						
							|  |  |  | 		begin = 0; | 
					
						
							|  |  |  | 	if( end > 3 ) | 
					
						
							|  |  |  | 		end = 3; | 
					
						
							|  |  |  | 	if( begin > end ) | 
					
						
							|  |  |  | 		begin = end; | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	list = PyList_New( end - begin ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( count = begin; count < end; count++ ) | 
					
						
							|  |  |  | 		PyList_SetItem( list, count - begin, | 
					
						
							|  |  |  | 				PyFloat_FromDouble( *( self->rgb[count] ) ) ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return list; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static int rgbTupleAssItem( BPy_rgbTuple * self, int i, PyObject * ob ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( i < 0 || i >= 3 ) | 
					
						
							|  |  |  | 		return EXPP_ReturnIntError( PyExc_IndexError, | 
					
						
							|  |  |  | 					    "array assignment index out of range" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !PyNumber_Check( ob ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnIntError( PyExc_IndexError, | 
					
						
							|  |  |  | 					    "color component must be a number" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | /* XXX this check above is probably ... */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	*( self->rgb[i] ) = | 
					
						
							| 
									
										
										
										
											2005-07-18 03:50:37 +00:00
										 |  |  | 		EXPP_ClampFloat( (float)PyFloat_AsDouble( ob ), 0.0, 1.0 ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static int rgbTupleAssSlice( BPy_rgbTuple * self, int begin, int end, | 
					
						
							|  |  |  | 			     PyObject * seq ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int count; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( begin < 0 ) | 
					
						
							|  |  |  | 		begin = 0; | 
					
						
							|  |  |  | 	if( end > 3 ) | 
					
						
							|  |  |  | 		end = 3; | 
					
						
							|  |  |  | 	if( begin > end ) | 
					
						
							|  |  |  | 		begin = end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( !PySequence_Check( seq ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnIntError( PyExc_TypeError, | 
					
						
							|  |  |  | 					    "illegal argument type for built-in operation" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( PySequence_Length( seq ) != ( end - begin ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnIntError( PyExc_TypeError, | 
					
						
							|  |  |  | 					    "size mismatch in slice assignment" ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( count = begin; count < end; count++ ) { | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 		float value; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		PyObject *ob = PySequence_GetItem( seq, count ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		if( !PyArg_Parse( ob, "f", &value ) ) { | 
					
						
							|  |  |  | 			Py_DECREF( ob ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		*( self->rgb[count] ) = EXPP_ClampFloat( value, 0.0, 1.0 ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		Py_DECREF( ob ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2003-07-05 01:18:41 +00:00
										 |  |  | /* Function:    rgbTuple_repr                                                */ | 
					
						
							| 
									
										
										
										
											2003-06-24 07:21:17 +00:00
										 |  |  | /* Description: This is a callback function for the BPy_rgbTuple type. It    */ | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | /*              builds a meaninful string to represent rgbTuple objects.     */ | 
					
						
							|  |  |  | /*****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *rgbTuple_repr( BPy_rgbTuple * self ) | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	float r, g, b; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	r = *( self->rgb[0] ); | 
					
						
							|  |  |  | 	g = *( self->rgb[1] ); | 
					
						
							|  |  |  | 	b = *( self->rgb[2] ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return PyString_FromFormat( "[%f, %f, %f]", r, g, b ); | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | } |