Commits

pygame  committed a59dccb

added pygame.display.set_palette()

  • Participants
  • Parent commits c0e4e5e

Comments (0)

Files changed (9)

 At some point it would be nice to add support for more external
 python libraries, here are some ideas...
 
-- Support for OpenGL (initial testing not so good)
-- more drawing primitives in the draw module
-- Interface with PIL images
-- GUI library. There's a few out there
-- Image transforms like flip, rotate, scale
-- MPEG video playback with smpeg
-- Macintosh support (just needs to compile)
+ - more drawing primitives in the draw module
+ - GUI library. There's a few out there
+ - Image transforms like flip, rotate, scale
+ - MPEG video playback with smpeg
 
 
 
 - And our dutiful bug hunters; Angus Young and Guillaume Proux
 
 There's many more folks out there who've submitted helpful ideas,
-kept this project going, and basically made my life easer; thanks!
+kept this project going, and basically made my life easer, Thanks!
 
 
 
 # BREAK = change breaks existing code
 # BUG   = fixed a bug that was crashing
 
+May 29, 2001
+	set display physical palette with display.set_palette()
+
 May 26, 2001
 	code compiles with macos now
 

File docs/index.html

 <a href=ref/pygame_display.html#set_caption>pygame.display.set_caption</a> - changes the title of the window<br>
 <a href=ref/pygame_display.html#set_gamma>pygame.display.set_gamma</a> - change the brightness of the display<br>
 <a href=ref/pygame_display.html#set_mode>pygame.display.set_mode</a> - set the display mode<br>
+<a href=ref/pygame_display.html#set_palette>pygame.display.set_palette</a> - set the palette<br>
 <a href=ref/pygame_display.html#toggle_fullscreen>pygame.display.toggle_fullscreen</a> - switch the display fullscreen mode<br>
 <a href=ref/pygame_display.html#update>pygame.display.update</a> - update an area of the display<br>
 <a href=ref/pygame_draw.html#line>pygame.draw.line</a> - draw a line on a surface<br>

File docs/ref/Surface.html

 </tt></font><ul>
 This will replace the entire palette with color
 information you provide.
+<br>&nbsp;<br>
+You can pass an incomplete list of RGB values, and
+this will only change the first colors in the palette.
 </ul><br>&nbsp;<br>
 
 <a name=set_palette_at><font size=+2><b>set_palette_at

File docs/ref/pygame_display.html

 set the display mode</td></tr>
 
 
+<tr><td><a href=#set_palette>set_palette</a></td><td> -
+set the palette</td></tr>
+
+
 <tr><td><a href=#toggle_fullscreen>toggle_fullscreen</a></td><td> -
 switch the display fullscreen mode</td></tr>
 
 blitting.
 </ul><br>&nbsp;<br>
 
+<a name=set_palette><font size=+2><b>set_palette
+</b></font><br><font size=+1><tt>
+pygame.display.set_palette([[r, g, b], ...]) -> None
+</tt></font><ul>
+Displays with a HWPALETTE have two palettes. The display Surface
+palette and the visible 'onscreen' palette.
+<br>&nbsp;<br>
+This will change the video display's visible colormap. It does
+not effect the display Surface's base palette, only how it is
+displayed. Setting the palette for the display Surface will
+override this visible palette. Also passing no args will reset
+the display palette back to the Surface's palette.
+<br>&nbsp;<br>
+You can pass an incomplete list of RGB values, and
+this will only change the first colors in the palette.
+</ul><br>&nbsp;<br>
+
 <a name=toggle_fullscreen><font size=+2><b>toggle_fullscreen
 </b></font><br><font size=+1><tt>
 pygame.display.toggle_fullscreen() -> bool

File docs/ref/pygame_image.html

 <br>
 <h2 align=center>pygame.image</h2>
 This module contains functions to transfer images in and out
-of Surfaces. At the minimum the included <a href=pygame_mixer_music.html#load>load()</a> function will
+of Surfaces. At the minimum the included <a href=#load>load()</a> function will
 support BMP files. If SDL_image is properly installed when
 pygame is installed, it will support all the formats included
 with SDL_image. You can call the <a href=#get_extended>get_extended()</a> function to test
 if the SDL_image support is available.
 <br>&nbsp;<br>
 Some functions that communicate with other libraries will require
-that those libraries are properly installed. For example, the <a href=Surface.html#save>save()</a>
+that those libraries are properly installed. For example, the <a href=#save>save()</a>
 function can only save OPENGL surfaces if pyopengl is available.
 
 <hr>

File docs/ref/pygame_mixer_music.html

 <br>&nbsp;<br>
 The music module has many of the same types of functions as the
 Sound objects. The main difference is only one music object can
-be loaded at a time, with the <a href=#load>load()</a> function. Music
+be loaded at a time, with the <a href=pygame_image.html#load>load()</a> function. Music
 must be stored in an individual file on the system, it cannot be
 loaded from special file-like objects through python.
 

File src/display.c

 	RETURN_NONE
 }
 
+    /*DOC*/ static char doc_set_palette[] =
+    /*DOC*/    "pygame.display.set_palette([[r, g, b], ...]) -> None\n"
+    /*DOC*/    "set the palette\n"
+    /*DOC*/    "\n"
+    /*DOC*/    "Displays with a HWPALETTE have two palettes. The display Surface\n"
+    /*DOC*/    "palette and the visible 'onscreen' palette.\n"
+    /*DOC*/    "\n"
+    /*DOC*/    "This will change the video display's visible colormap. It does\n"
+    /*DOC*/    "not effect the display Surface's base palette, only how it is\n"
+    /*DOC*/    "displayed. Setting the palette for the display Surface will\n"
+    /*DOC*/    "override this visible palette. Also passing no args will reset\n"
+    /*DOC*/    "the display palette back to the Surface's palette.\n"
+    /*DOC*/    "\n"
+    /*DOC*/    "You can pass an incomplete list of RGB values, and\n"
+    /*DOC*/    "this will only change the first colors in the palette.\n"
+    /*DOC*/ ;
+
+static PyObject* set_palette(PyObject* self, PyObject* args)
+{
+	SDL_Surface* surf;
+	SDL_Palette* pal;
+	SDL_Color* colors;
+	PyObject* list, *item = NULL;
+	int i, len;
+	short r, g, b;
+	
+	VIDEO_INIT_CHECK();
+	if(!PyArg_ParseTuple(args, "|O", &list))
+		return NULL;
+	surf = SDL_GetVideoSurface();
+	if(!surf)
+		return RAISE(PyExc_SDLError, "No display mode is set");
+	pal = surf->format->palette;
+	if(surf->format->BytesPerPixel != 1 || !pal)
+		return RAISE(PyExc_SDLError, "Display mode is not colormapped");
+
+	if(!list)
+	{
+		colors = pal->colors;
+		len = pal->ncolors;
+		SDL_SetPalette(surf, SDL_PHYSPAL, colors, 0, len);
+		RETURN_NONE
+	}
+	
+	
+	if(!PySequence_Check(list))
+		return RAISE(PyExc_ValueError, "Argument must be a sequence type");
+
+	len = min(pal->ncolors, PySequence_Length(list));
+
+	colors = (SDL_Color*)malloc(len * sizeof(SDL_Color));
+	if(!colors)
+		return NULL;
+	
+	for(i = 0; i < len; i++)
+	{
+		item = PySequence_GetItem(list, i);
+
+		if(!PySequence_Check(item) || PySequence_Length(item) != 3)
+		{
+			Py_DECREF(item);
+			free((char*)colors);
+			return RAISE(PyExc_TypeError, "takes a sequence of sequence of RGB");
+		}
+		if(!ShortFromObjIndex(item, 0, &r) || !ShortFromObjIndex(item, 1, &g) || !ShortFromObjIndex(item, 2, &b))
+			return RAISE(PyExc_TypeError, "RGB sequence must contain numeric values");
+
+		colors[i].r = (unsigned char)r;
+		colors[i].g = (unsigned char)g;
+		colors[i].b = (unsigned char)b;
+	
+		Py_DECREF(item);
+	}
+
+	SDL_SetPalette(surf, SDL_PHYSPAL, colors, 0, len);
+
+	free((char*)colors);
+	RETURN_NONE
+}
+
 
     /*DOC*/ static char doc_set_gamma[] =
     /*DOC*/    "pygame.display.set_gamma(r, [g, b]) -> bool\n"
 	{ "flip", flip, 1, doc_flip },
 	{ "update", update, 1, doc_update },
 
+	{ "set_palette", set_palette, 1, doc_set_palette },
 	{ "set_gamma", set_gamma, 1, doc_set_gamma },
 	/*gammaramp support will be added later, if needed?*/
 

File src/surface.c

     /*DOC*/    "\n"
     /*DOC*/    "This will replace the entire palette with color\n"
     /*DOC*/    "information you provide.\n"
+    /*DOC*/    "\n"
+    /*DOC*/    "You can pass an incomplete list of RGB values, and\n"
+    /*DOC*/    "this will only change the first colors in the palette.\n"
     /*DOC*/ ;
 
 static PyObject* surf_set_palette(PyObject* self, PyObject* args)