Advertisement
here2share

# s60_subtractive_color_mixing.py

May 1st, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # s60_subtractive_color_mixing.py
  2.  
  3. from appuifw import *
  4. import e32,audio,appuifw,os,sys,key_codes,time,math,random,graphics,sensor,copy
  5. from graphics import *
  6.  
  7.  
  8. z_Colors =  ((u'RGB_BLACK',(0,0,0)),
  9.             (u'RGB_WHITE',(255,255,255)),
  10.             (u'RGB_GRAYLIGHT',(211,211,211)),
  11.             (u'RGB_GRAY',(128,128,128)),
  12.             (u'RGB_GRAYDARK',(169,169,169)),
  13.             (u'RGB_RED',(255,0,0)),
  14.             (u'RGB_ORANGE',(255,165,0)),
  15.             (u'RGB_YELLOW',(255,255,0)),
  16.             (u'RGB_GREEN',(0,128,0)),
  17.             (u'RGB_BLUE',(0,0,255)),
  18.             (u'RGB_CYAN',(0,255,255)),
  19.             (u'RGB_PURPLE',(128,0,128)),
  20.             (u'RGB_PINK',(255,148,238)),
  21.  
  22.             (u'RGB_DARKBLUE',(0,0,139)),
  23.             (u'RGB_DARKGREEN',(0,100,0)),
  24.             (u'RGB_DEEPPINK',(255,20,147)),
  25.             (u'RGB_INDIGO',(75,0,130)),
  26.             (u'RGB_LIGHTBLUE',(173,216,230)),
  27.             (u'RGB_LIME',(0,255,0)),
  28.             (u'RGB_OLIVE',(107,142,35)),
  29.  
  30.             (u'RGB_BROWN',(139,69,19)),
  31.             (u'RGB_GOLD',(255,215,0)),
  32.             (u'RGB_SILVER',(192,192,192)))
  33. #
  34. rgb_menu='rgb_menu=['
  35. for rgb in z_Colors:
  36.     i,ii=rgb[0].replace('RGB_',''),str(rgb[1])
  37.     rgb_menu += "(u'"+i+"',c_"+i+"),"
  38.     exec 'def c_'+i+'(): cv.color='+ii
  39.     exec rgb[0]+'='+ii
  40. exec rgb_menu[:-1]+']'
  41. appuifw.app.menu=rgb_menu
  42.  
  43. class cv():
  44.     go=1
  45.     pixel=(1,1)
  46.     incr=0
  47.     msg=''
  48.     t=0
  49.     Lcolor = (0,0,255)
  50.     Rcolor = (255,255,0)
  51.     btn=1
  52.     color=(0,0,0)
  53. cv=cv()
  54.  
  55. def quit():
  56.     timer.cancel()
  57.     cv.go = 0
  58. app.exit_key_handler = quit
  59.  
  60. def draw(rect):
  61.     if img:
  62.         canvas.blit(img)
  63.  
  64. def e_callback(event):
  65.     cv.pixel = event['pos']
  66.     try: timer.cancel()
  67.     except: pass
  68.     if cv.pixel[0] > 340:
  69.         cv.incr=increment[int(cv.pixel[1]*sVy)]
  70.     timer.after(1,zero)
  71.  
  72. img=None
  73. app.screen = 'large'
  74. app.orientation = 'portrait'
  75. app.directional_pad = False
  76. app.body = canvas = Canvas(redraw_callback=draw, event_callback=e_callback) #
  77. img = Image.new(canvas.size)
  78. res_x, res_y = canvas.size
  79.  
  80. timer = e32.Ao_timer()
  81.  
  82. res_x, res_y = 360,576
  83. sVy = 9.0/res_y
  84.  
  85. increment=[100,50,10,1,0,-1,-10,-50,-100]
  86.  
  87. img.clear(RGB_GRAY)
  88. BW=[RGB_WHITE,RGB_BLACK]
  89. yy2=res_y/9
  90. for ssVyy in xrange(9):
  91.     yy=yy2*ssVyy+2
  92.     WvB=BW[0]
  93.     BW[0],BW[1]=BW[1],BW[0]
  94.     img.rectangle(((350,yy,358,yy+yy2)), fill=WvB)
  95.     draw(())
  96.  
  97. def rgb_btns(x,y,x2=360,y2=50):
  98.     xx=x2/3+1
  99.     rgb=RGB_RED,RGB_GREEN,RGB_BLUE
  100.     for i in xrange(3):
  101.         xy2=(x+xx*i,y,x+xx*(i+1),y+y2)
  102.         if cv.pixel:
  103.             xv,yv=cv.pixel
  104.             if yv > y and yv < y+y2:
  105.                 if xv > xy2[0] and xv < xy2[2]:
  106.                     cv.active_palette=palref[palette+i]
  107.                     cv.RGB_mark=(x+xx*i,y,x+xx*i+10,y+10)
  108.         img.rectangle((xy2), fill=rgb[i])
  109.  
  110. def hx(rgb):
  111.     return '#%02x%02x%02x' % rgb
  112.  
  113. palref='''
  114. cv.Lcolor = (hh(cv.Lcolor[0]), cv.Lcolor[1], cv.Lcolor[2])
  115. cv.Lcolor = (cv.Lcolor[0], hh(cv.Lcolor[1]), cv.Lcolor[2])
  116. cv.Lcolor = (cv.Lcolor[0], cv.Lcolor[1], hh(cv.Lcolor[2]))
  117. cv.Rcolor = (hh(cv.Rcolor[0]), cv.Rcolor[1], cv.Rcolor[2])
  118. cv.Rcolor = (cv.Rcolor[0], hh(cv.Rcolor[1]), cv.Rcolor[2])
  119. cv.Rcolor = (cv.Rcolor[0], cv.Rcolor[1], hh(cv.Rcolor[2]))'''.split('\n')[1:]
  120. cv.active_palette=palref[0]
  121.  
  122. #
  123. def mixColors(): ### ZZZ yellow + blue = (0,128,0)
  124.     def rgbfix(R):
  125.         aaa=(LC[R]*-1)+(RC[R])
  126.         tmp=[0,1,2]; tmp.pop(R)
  127.         bbb=LC[tmp[0]]+LC[tmp[1]]
  128.         ccc=int((RC[tmp[0]]+RC[tmp[1]]))*-1
  129.         tmp=abs(aaa+bbb+ccc)-637
  130.         if tmp > 0: return tmp
  131.         return 0
  132.    
  133.     LC,RC = cv.Lcolor,cv.Rcolor
  134.     rgb = [(LC[0]+1)/2, (LC[1]+1)/2, (LC[2]+1)/2] # L
  135.     rgb = [rgb[0]+((RC[0]+1)/2), rgb[1]+((RC[1]+1)/2), rgb[2]+((RC[2]+1)/2)] # R
  136.    
  137.     # color fix for mixing yellow and blue to correspondingly make green
  138.     rgb = [rgb[0]-rgbfix(2),rgb[1],rgb[2]-rgbfix(2)]
  139.     for i in range(3):
  140.         if rgb[i] > 255: rgb[i] = 255
  141.         elif rgb[i] < 0: rgb[i] = 0
  142.     return tuple(rgb)
  143. #
  144. def zero():
  145.     cv.incr=0
  146. #
  147. def rgb_box(box,x=10,y=210,w=60,h=60):
  148.     i='LRM'.find(box)
  149.     return ((x,y,350-x-w,y+h*3),(x+w,y+h,350-x,y+h*4),(x+w,y+h,350-x-w,y+h*3))[i]
  150. #
  151. def hh(limit):
  152.     limit+=cv.incr
  153.     if limit < 0: limit=0
  154.     if limit > 255: limit=255
  155.     return limit
  156. #
  157.  
  158. prev_color=cv.color
  159. cv.RGB_mark=(10,140,20,150)
  160. while cv.go:
  161.     if prev_color <> cv.color:
  162.         prev_color=cv.color
  163.         exec (cv.active_palette[:9]+'='+str(cv.color))
  164.         cv.pixel=(1,0)
  165.     if time.time() > cv.t and cv.pixel:
  166.         exec (cv.active_palette)
  167.         cv.subcolor = mixColors()
  168.         img.rectangle(((0,0,350,640)), fill=RGB_GRAY)
  169.         img.rectangle(((0,0,340,92)), fill=RGB_WHITE)
  170.         palette=0
  171.         rgb_btns(10,140,280)
  172.         palette=3
  173.         rgb_btns(10,500,280)
  174.         img.rectangle((cv.RGB_mark), fill=RGB_WHITE)
  175.         img.rectangle((rgb_box('L')), fill=cv.Lcolor)
  176.         img.rectangle((rgb_box('R')), fill=cv.Rcolor)
  177.         img.rectangle((rgb_box('L')), width=3, outline=RGB_BLACK)
  178.         img.rectangle((rgb_box('R')), width=3, outline=RGB_BLACK)
  179.         img.rectangle((rgb_box('M')), fill=cv.subcolor)
  180.         img.text((10,36), u"RGB Result Of A And B:", fill = RGB_BLACK,font=(u'Nokia Hindi S60',29,STYLE_BOLD))
  181.         img.text((10,68), u""+str(cv.subcolor), fill = RGB_BLACK,font=(u'Nokia Hindi S60',29,STYLE_BOLD))
  182.         img.text((10,128), u"RGB Of A: "+str(cv.Lcolor), fill = RGB_BLACK,font=(u'Nokia Hindi S60',29,STYLE_BOLD))
  183.         img.text((10,488), u"RGB Of B: "+str(cv.Rcolor), fill = RGB_BLACK,font=(u'Nokia Hindi S60',29,STYLE_BOLD))
  184.         cv.t=time.time()+0.3
  185.         draw(())
  186.     cv.incr=0
  187.     cv.pixel=None
  188.     e32.ao_yield()
  189. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement