Attachment 'color_picker.py'

Download

   1 #!/usr/bin/env python
   2 # coding: utf
   3 '''
   4 В примитивной программе «рисование мышью»:
   5 
   6     реализовать рисование мышью :) с помощью fill() квадратами небольшого размера
   7     реализовать т. н. «color picker»: окно выбора цвета (например, при нажатии на клавиатуру)
   8     реализовать «рисование штампом» при помощи картинки с прозрачностью (например, правой кнопкой мыши)
   9 '''
  10 
  11 from pygame import *
  12 W,H = 800, 600
  13 timeout = 100
  14 dotsize = 4
  15 
  16 init()
  17 screen = display.set_mode((W,H))
  18 color = Color(255,0,0)
  19 
  20 def intn(seq): return [int(s) for s in seq]
  21 
  22 def Dot(pos, color):
  23     screen.fill(color, Rect(pos[0]-dotsize/2, pos[1]-dotsize/2, dotsize, dotsize))
  24 
  25 def picker_draw(window, grad, color):
  26     window.fill(color)
  27     c, (h, s, v, a) = window.get_at((0,0)), color.hsva
  28     wrec = window.get_rect()
  29     for i in xrange(grad):
  30         for j in xrange(grad):
  31             h, s = i*360/grad, j*100/grad
  32             c.hsva = h, s, v, a
  33             r = Rect(wrec.w*i/grad, wrec.h*j/grad, wrec.w/grad-1, wrec.h/grad-1)
  34             window.fill(c, r)
  35 
  36 def picker_vchange(color,dv):
  37     h, s, v, a = color.hsva
  38     v = (100 + v + dv) % 100
  39     color.hsva = intn((h, s, v, a))
  40     return color
  41 
  42 def picker(color):
  43     rect = Rect(W/4, H/4, W/2, H/2)
  44     grad, diffv = 16, 5
  45     window = screen.subsurface(rect)
  46     backing = window.copy()
  47     again = True
  48     picker_draw(window, grad, color)
  49     while again:
  50         ev = event.wait()
  51         if ev.type == MOUSEMOTION:
  52             if not rect.collidepoint(ev.pos):
  53                 again = False
  54         elif ev.type == KEYDOWN:
  55             if ev.key == 27:
  56                 again = False
  57         elif ev.type == MOUSEBUTTONDOWN:
  58             if ev.button == 1:
  59                 color = screen.get_at(ev.pos)
  60             elif ev.button == 5:
  61                 color = picker_vchange(color, diffv)
  62             elif ev.button == 4:
  63                 color = picker_vchange(color, -diffv)
  64         picker_draw(window, grad, color)
  65         display.flip()
  66     window.blit(backing, (0,0))
  67     return color
  68 
  69 again = True
  70 while again:
  71     ev = event.wait()
  72     if ev.type == MOUSEBUTTONDOWN:
  73         if ev.button == 1:
  74             Dot(ev.pos, color)
  75         elif ev.button == 3:
  76             color = picker(color)
  77         elif ev.button == 4:
  78             dotsize += 1
  79         elif ev.button == 5:
  80             dotsize = max(1, dotsize-1)
  81             print dotsize
  82     if ev.type == MOUSEMOTION:
  83         if ev.buttons[0]:
  84             Dot(ev.pos, color)
  85     elif ev.type == KEYDOWN:
  86         if ev.key == 27:
  87             again = False
  88     elif ev.type == QUIT:
  89         again = False
  90     display.flip()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.