Advertisement
here2share

# Tk_subtractive_color_mixing.py

Sep 5th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. # Tk_subtractive_color_mixing.py
  2.  
  3. from Tkinter import *
  4. import time
  5.  
  6. t = Tk()
  7. t.title("Subtractive Color Mixing")
  8.  
  9. frame1 = Frame(t)
  10. frame1.pack()
  11. frame2 = LabelFrame(t,text='L Side')
  12. frame2.pack()
  13. frame3 = LabelFrame(t,text='R Side')
  14. frame3.pack()
  15.  
  16. Lcolor = (0,0,0)
  17. Rcolor = (0,0,0)
  18. subcolor = (0,0,0)
  19. init = 0
  20.  
  21. canvas = Canvas(frame1, bg="gray", width=120*4+32, height=120*2)
  22. canvas.pack()
  23.  
  24. def hx(rgb):
  25.     return '#%02x%02x%02x' % rgb
  26. def create():
  27.     if init:
  28.         try:
  29.             canvas.create_rectangle((0, 0, 120, 120*2), fill=hx(Lcolor))
  30.             canvas.create_rectangle((120, 0, 120*2, 120*2), fill=hx(subcolor))
  31.             canvas.create_rectangle((120*2, 0, 120*3, 120*2), fill=hx(Rcolor))
  32.         except:
  33.             print subcolor
  34.         canvas.create_rectangle((120*3, 0, 120*4+32, 120*2), fill='gray')
  35.         y=10
  36.         LMR=[Lcolor,subcolor,Rcolor]
  37.         for i in list('LMR'):
  38.             canvas.create_text(380, y, anchor=NW, text=i)
  39.             canvas.create_text(400, y, anchor=NW, text='=    %s'% str(LMR.pop(0))); y+=20  
  40.  
  41. def r1(event): global Lcolor; Lcolor = (int(event), Lcolor[1], Lcolor[2])
  42. def g1(event): global Lcolor; Lcolor = (Lcolor[0], int(event), Lcolor[2])
  43. def b1(event): global Lcolor; Lcolor = (Lcolor[0], Lcolor[1], int(event))
  44. def r2(event): global Rcolor; Rcolor = (int(event), Rcolor[1], Rcolor[2])
  45. def g2(event): global Rcolor; Rcolor = (Rcolor[0], int(event), Rcolor[2])
  46. def b2(event): global Rcolor; Rcolor = (Rcolor[0], Rcolor[1], int(event))
  47.  
  48. LS = LabelFrame(t,text="L Side",height=20).pack()
  49.  
  50. s=Scale(frame2, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=r1)
  51. s.set(255); s.pack()
  52. s=Scale(frame2, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=g1)
  53. s.set(255); s.pack()
  54. Scale(frame2, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=b1).pack()
  55. Scale(frame3, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=r2).pack()
  56. Scale(frame3, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=g2).pack()
  57. s=Scale(frame3, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=b2)
  58. s.set(255); s.pack()
  59.    
  60. def mixColors(): ### ZZZ yellow + blue = (0,128,0)
  61.     def rgbfix(R):
  62.         aaa=(LC[R]*-1)+(RC[R])
  63.         tmp=[0,1,2]; tmp.pop(R)
  64.         bbb=LC[tmp[0]]+LC[tmp[1]]
  65.         ccc=int((RC[tmp[0]]+RC[tmp[1]]))*-1
  66.         tmp=abs(aaa+bbb+ccc)-637
  67.         if tmp > 0: return tmp
  68.         return 0
  69.    
  70.     LC,RC = Lcolor,Rcolor
  71.     rgb = [(LC[0]+1)/2, (LC[1]+1)/2, (LC[2]+1)/2] # L
  72.     rgb = [rgb[0]+((RC[0]+1)/2), rgb[1]+((RC[1]+1)/2), rgb[2]+((RC[2]+1)/2)] # R
  73.    
  74.     # color fix for mixing yellow and blue to correspondingly make green
  75.     rgb = [rgb[0]-rgbfix(2),rgb[1],rgb[2]-rgbfix(2)]
  76.     #print rgbfix(2)
  77.     for i in range(3):
  78.         if rgb[i] > 255: rgb[i] = 255
  79.         elif rgb[i] < 0: rgb[i] = 0
  80.     return rgb
  81.  
  82. SidesLR = ''
  83. def run():
  84.     global SidesLR, subcolor, init
  85.     if SidesLR != Lcolor+Rcolor:
  86.         subcolor = tuple(mixColors())
  87.         SidesLR = Lcolor+Rcolor; create(); t.update()
  88.         init = 1
  89.     t.after(10, run)
  90. #
  91. run()
  92. t.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement