Commits

illume committed b52bfc5

Surface.get_palette(_at) return Color with length 3.

Comments (0)

Files changed (7)

 # SVN revision numbers, and CVS tag names are placed before the date
 # BREAK = change breaks existing code
 # BUG    = fixed a bug that was (or could have been) crashing
-#
-#
 
-[SVN 2380] Jun 19, 2009
+[SVN 2380-2381] Jun 19, 2009
     Color.set_length method... to change Color length to 1,2,3 or 4.
+    Surface.get_palette(_at) return Color with length 3.
 
 [SVN 2366] Jun 17, 2009
     [BUG] Memory leak in a few modules.  Thanks Lorenz Quack.

docs/ref/surface.html

   <i>get the color index palette for an 8bit Surface</i><br>
   <tt>Surface.get_palette(): return [RGB, RGB, RGB, ...]</tt><br>
 <p>Return a list of up to 256 color elements that represent the indexed colors used in an 8bit Surface. The returned list is a copy of the palette, and changes will have no effect on the Surface. </p>
-<p>Returning a list of Color instances instead of tuples, New in pygame <tt>1.9.0</tt> </p>
+<p>Returning a list of Color(with length 3) instances instead of tuples, New in pygame <tt>1.9.0</tt> </p>
 <!--COMMENTS:Surface.get_palette--> &nbsp;<br> 
 <br></ul>
 
   <i>get the color for a single entry in a palette</i><br>
   <tt>Surface.get_palette_at(index): return RGB</tt><br>
 <p>Returns the red, green, and blue color values for a single index in a Surface palette. The index should be a value from 0 to 255. </p>
-<p>Returning Color instance instead of a tuple, New in pygame <tt>1.9.0</tt> </p>
+<p>Returning Color(with length 3) instance instead of a tuple, New in pygame <tt>1.9.0</tt> </p>
 <!--COMMENTS:Surface.get_palette_at--> &nbsp;<br> 
 <br></ul>
 
 static int _coerce_obj(PyObject *obj, Uint8 rgba[]);
 
 static PyColor* _color_new_internal (PyTypeObject *type, Uint8 rgba[]);
+static PyColor* _color_new_internal_length (PyTypeObject *type, Uint8 rgba[], Uint8 length);
+
 static PyObject* _color_new (PyTypeObject *type, PyObject *args,
     PyObject *kwds);
 static void _color_dealloc (PyColor *color);
 
 /* C API interfaces */
 static PyObject* PyColor_New (Uint8 rgba[]);
+static PyObject* PyColor_NewLength (Uint8 rgba[], Uint8 length);
 static int RGBAFromColorObj (PyObject *color, Uint8 rgba[]);
 
 /**
 static PyColor*
 _color_new_internal (PyTypeObject *type, Uint8 rgba[])
 {
+    /* default length of 4 - r,g,b,a. */
+    return _color_new_internal_length(type, rgba, 4);
+}
+
+
+static PyColor*
+_color_new_internal_length (PyTypeObject *type, Uint8 rgba[], Uint8 length)
+{
     PyColor *color = (PyColor *) type->tp_alloc (type, 0);
     if (!color)
         return NULL;
     color->g = rgba[1];
     color->b = rgba[2];
     color->a = rgba[3];
-    /* default length of 4 - r,g,b,a. */
-    color->len = 4;
+    color->len = length;
 
     return color;
 }
 
-
-
 /**
  * Creates a new PyColor.
  */
     return (PyObject *) _color_new_internal (&PyColor_Type, rgba);
 }
 
+static PyObject*
+PyColor_NewLength (Uint8 rgba[], Uint8 length)
+{
+    if(length < 1 || length > 4) {
+        return NULL;
+    }
+
+    return (PyObject *) _color_new_internal_length (&PyColor_Type, rgba, length);
+}
+
+
+
+
 static int
 RGBAFromColorObj (PyObject *color, Uint8 rgba[])
 {
     c_api[0] = &PyColor_Type;
     c_api[1] = PyColor_New;
     c_api[2] = RGBAFromColorObj;
+    c_api[3] = PyColor_NewLength;
 
     apiobj = PyCObject_FromVoidPtr (c_api, NULL);
     if (apiobj == NULL) {
 /* Color */
 #define PYGAMEAPI_COLOR_FIRSTSLOT                                       \
     (PYGAMEAPI_PIXELARRAY_FIRSTSLOT + PYGAMEAPI_PIXELARRAY_NUMSLOTS)
-#define PYGAMEAPI_COLOR_NUMSLOTS 3
+#define PYGAMEAPI_COLOR_NUMSLOTS 4
 #ifndef PYGAMEAPI_COLOR_INTERNAL
 #define PyColor_Check(x)                                                \
     ((x)->ob_type == (PyTypeObject*)                                    \
         PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 0])
 #define PyColor_New                                                     \
     (*(PyObject *(*)(Uint8*)) PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 1])
+#define PyColor_NewLength                                               \
+    (*(PyObject *(*)(Uint8*, Uint8)) PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 3])
+
 #define RGBAFromColorObj                                                \
     (*(int(*)(PyObject*, Uint8*)) PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 2])
 #define import_pygame_color()                                           \
         rgba[0] = c->r;
         rgba[1] = c->g;
         rgba[2] = c->b;
-        color = PyColor_New (rgba);
+        color = PyColor_NewLength (rgba, 3);
+
         if (!color)
         {
             Py_DECREF (list);
     rgba[1] = c->g;
     rgba[2] = c->b;
     rgba[3] = 255;
-    return PyColor_New (rgba);
+
+    return PyColor_NewLength (rgba, 3);
 }
 
 static PyObject*
 used in an 8bit Surface. The returned list is a copy of the palette, and 
 changes will have no effect on the Surface.
 
-Returning a list of Color instances instead of tuples, New in pygame 1.9.0
+Returning a list of Color(with length 3) instances instead of tuples, New in pygame 1.9.0
 <END>
 
 
 Returns the red, green, and blue color values for a single index in a Surface
 palette. The index should be a value from 0 to 255.
 
-Returning Color instance instead of a tuple, New in pygame 1.9.0
+Returning Color(with length 3) instance instead of a tuple, New in pygame 1.9.0
 <END>
 
 

test/surface_test.py

             surf = pygame.Surface((2, 2), 0, 8)
             surf.set_palette(palette)
             palette2 = surf.get_palette()
+            r,g,b = palette2[0]
+
             self.failUnlessEqual(len(palette2), len(palette))
             for c2, c in zip(palette2, palette):
                 self.failUnlessEqual(c2, c)