def dither(self): """Return an Atkinson-dithered version of `bitmap`. Each pixel in `bitmap` may take a value in [0, 255]. Based on code by Michal Migurski: http://mike.teczno.com/notes/atkinson.html """ threshold = 128 * [0] + 128 * [255] for y in xrange(self._height): for x in xrange(self._width): old = self.pixels[y * self._width + x] new = threshold[old] err = (old - new) // 8 self.pixels[y * self._width + x] = new for nxy in [(x + 1, y), (x + 2, y), (x - 1, y + 1), (x, y + 1), (x + 1, y + 1), (x, y + 2)]: try: px = nxy[1] * self._width + nxy[0] self.pixels[px] = commons.clamp( self.pixels[px] + err, 0, 255) except IndexError: pass
def render_progressbar(surface, x, y, w, h, progress): """Draw a progress bar widget into the given surface.""" surface.strokerect(x, y, w, h) bar_width = int((w - 4) * commons.clamp(progress, 0, 1)) surface.fillrect(x + 2, y + 2, bar_width, h - 4)
def down_pressed(self): self.cy += 1 self.cy = commons.clamp(self.cy, 0, len(self.stations) - 1) self.set_needs_repaint()