prototype for auto-generated soup user icons


/ Published in: Python
Save to your folder(s)

See link for screenshot


Copy this code and paste it in your HTML
  1. colormode(HSB)
  2.  
  3. class Icon(object):
  4. def __init__(self):
  5. self.width = 5
  6. self.height = 5
  7. self.pixels = [random(2) \
  8. for i in xrange(self.width * self.height)]
  9. self._symmetrize()
  10. self.pixelsize = 8
  11. self.fg_col = (random(1.0), random(0.4, 1.0), 0.25)
  12. self.bg_col = ((self.fg_col[0] + 0.2) % 1.0, self.fg_col[1], 1.0)
  13.  
  14. def paint(self, x_pos=0, y_pos=0):
  15. nostroke()
  16. for y in xrange(self.height):
  17. for x in xrange(self.width):
  18. if self.pixels[x + self.width * y]:
  19. col = self.fg_col
  20. else:
  21. col = self.bg_col
  22. fill(color(col))
  23. rect(x * self.pixelsize + x_pos, y * self.pixelsize + y_pos,
  24. self.pixelsize, self.pixelsize)
  25.  
  26. def _symmetrize(self):
  27. for y in xrange(self.height):
  28. for x in xrange(int(self.width / 2), self.width):
  29. self.pixels[x + self.width * y] = \
  30. self.pixels[(self.width - (x + 1)) + self.width * y]
  31.  
  32.  
  33. class IconGrid(object):
  34. def __init__(self, width, height):
  35. self.width = width
  36. self.height = height
  37. self.icons = [Icon() for x in xrange(self.width) \
  38. for y in xrange(self.height)]
  39.  
  40. def paint(self):
  41. for y in xrange(self.height):
  42. for x in xrange(self.width):
  43. icon = self.icons[x + y * self.width]
  44. pw = icon.width * (icon.pixelsize + 1)
  45. ph = icon.height * (icon.pixelsize + 1)
  46. icon.paint(x * pw, y * ph)
  47.  
  48. grid = IconGrid(8, 13)
  49. grid.paint()

URL: http://dev.soup.io/post/281433

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.