illume avatar illume committed 098cc70

Fix up Color slicing, add a bunch of tests for Color[:] slicing.

Comments (0)

Files changed (2)

 
 
 static PyObject *
-_color_slice(register PyColor *a, register Py_ssize_t ilow, 
-           register Py_ssize_t ihigh)
+_color_slice(register PyColor *a, 
+             register Py_ssize_t ilow, 
+             register Py_ssize_t ihigh)
 {
-/*
-        register PyColor *np;
-        PyObject **src, **dest;
-        register Py_ssize_t i;
+
         Py_ssize_t len;
-        Uint8 anrgba[4];
-        anrgba[0] = 0;
-        anrgba[1] = 0;
-        anrgba[2] = 0;
-        anrgba[3] = 0;
+        Py_ssize_t c1, c2, c3, c4;
+        c1=0;c2=0;c3=0;c4=0;
 
-        if (ilow < 0)
-                ilow = 0;
-        if (ihigh > a->ob_size)
-                ihigh = a->ob_size;
-        if (ihigh < ilow)
-                ihigh = ilow;
-        if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckExact(a)) {
-                Py_INCREF(a);
-                return (PyObject *)a;
-        }
-        len = ihigh - ilow;
-        np = (PyColor *)PyColor_New(anrgba);
-        if (np == NULL)
-                return NULL;
-        src = a->ob_item + ilow;
-        dest = np->ob_item;
-        for (i = 0; i < len; i++) {
-                PyObject *v = src[i];
-                Py_INCREF(v);
-                dest[i] = v;
-        }
-        return (PyObject *)np;
-
-*/
-
-        register PyTupleObject *np;
-        PyObject **src, **dest;
-        register Py_ssize_t i;
-        Py_ssize_t len;
-        PyColor *color;
-
-        color = a;
+        /* printf("ilow :%d:, ihigh:%d:\n", ilow, ihigh); */
 
         if (ilow < 0)
                 ilow = 0;
                 ihigh = 4;
         if (ihigh < ilow)
                 ihigh = ilow;
-        if (ilow == 0 && ihigh == 4 && PyTuple_CheckExact(a)) {
-                Py_INCREF(a);
-                return (PyObject *)a;
-        }
+
         len = ihigh - ilow;
+        /* printf("2 ilow :%d:, ihigh:%d: len:%d:\n", ilow, ihigh, len); */
         
-        /* TODO: this isn't right... need to take into account high and low
-        */
-        if(len == 4) {
-            return Py_BuildValue ("(iiii)",color->r,color->g,color->b,color->a);
-        } else if(len == 3) {
-            return Py_BuildValue ("(iii)",color->r,color->g,color->b);
-        } else if(len == 2) {
-            return Py_BuildValue ("(ii)",color->r,color->g);
-        } else if(len == 1) {
-            return Py_BuildValue ("(i)",color->r);
-        } else {
-            return Py_BuildValue ("(i)",color->r);
+        if(ilow == 0) {
+            c1 = a->r;
+            c2 = a->g;
+            c3 = a->b;
+            c4 = a->a;
+        } else if(ilow == 1) {
+            c1 = a->g;
+            c2 = a->b;
+            c3 = a->a;
+
+        } else if(ilow == 2) {
+            c1 = a->b;
+            c2 = a->a;
+
+        } else if(ilow == 3) {
+            c1 = a->a;
         }
 
 
+
+        /* return a tuple depending on which elements are wanted.  */
+        if(len == 4) {
+            return Py_BuildValue ("(iiii)",c1,c2,c3,c4);
+        } else if(len == 3) {
+            return Py_BuildValue ("(iii)",c1,c2,c3);
+        } else if(len == 2) {
+            return Py_BuildValue ("(ii)",c1,c2);
+        } else if(len == 1) {
+            return Py_BuildValue ("(i)",c1);
+        } else {
+            return Py_BuildValue ("()");
+        }
 }
 
 

test/color_test.py

         self.assertEquals(pygame.color.Color('red'), pygame.color.Color(' r e d '))
 
     def test_slice(self):
-        #TODO: do all sorts of slice combinations.
+        # slicing a color gives you back a tuple.
+        # do all sorts of slice combinations.
         c = pygame.Color(1,2,3,4)
-        parts = c[:-1]
-        self.assertEquals((1,2,3), parts)
-
+        
+        self.assertEquals((1,2,3,4), c[:])
+        self.assertEquals((1,2,3), c[:-1])
+        
+        self.assertEquals((), c[:-5])
+        
+        self.assertEquals((1,2,3,4), c[:4])
+        self.assertEquals((1,2,3,4), c[:5])
+        self.assertEquals((1,2), c[:2])
+        self.assertEquals((1,), c[:1])
+        self.assertEquals((), c[:0])
+        
+        
+        self.assertEquals((2,), c[1:-2])
+        self.assertEquals((3, 4), c[-2:])
+        self.assertEquals((4,), c[-1:])
+        
+        
+        # NOTE: assigning to a slice is currently unsupported.
+        
+        
+        
+        
     def test_case_insensitivity_of_string_args(self):
         self.assertEquals(pygame.color.Color('red'), pygame.color.Color('Red'))
     
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.