Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_subtractive_color_mixing.py
- from Tkinter import *
- import time
- t = Tk()
- t.title("Subtractive Color Mixing")
- frame1 = Frame(t)
- frame1.pack()
- frame2 = LabelFrame(t,text='L Side')
- frame2.pack()
- frame3 = LabelFrame(t,text='R Side')
- frame3.pack()
- Lcolor = (0,0,0)
- Rcolor = (0,0,0)
- subcolor = (0,0,0)
- init = 0
- canvas = Canvas(frame1, bg="gray", width=120*4+32, height=120*2)
- canvas.pack()
- def hx(rgb):
- return '#%02x%02x%02x' % rgb
- def create():
- if init:
- try:
- canvas.create_rectangle((0, 0, 120, 120*2), fill=hx(Lcolor))
- canvas.create_rectangle((120, 0, 120*2, 120*2), fill=hx(subcolor))
- canvas.create_rectangle((120*2, 0, 120*3, 120*2), fill=hx(Rcolor))
- except:
- print subcolor
- canvas.create_rectangle((120*3, 0, 120*4+32, 120*2), fill='gray')
- y=10
- LMR=[Lcolor,subcolor,Rcolor]
- for i in list('LMR'):
- canvas.create_text(380, y, anchor=NW, text=i)
- canvas.create_text(400, y, anchor=NW, text='= %s'% str(LMR.pop(0))); y+=20
- def r1(event): global Lcolor; Lcolor = (int(event), Lcolor[1], Lcolor[2])
- def g1(event): global Lcolor; Lcolor = (Lcolor[0], int(event), Lcolor[2])
- def b1(event): global Lcolor; Lcolor = (Lcolor[0], Lcolor[1], int(event))
- def r2(event): global Rcolor; Rcolor = (int(event), Rcolor[1], Rcolor[2])
- def g2(event): global Rcolor; Rcolor = (Rcolor[0], int(event), Rcolor[2])
- def b2(event): global Rcolor; Rcolor = (Rcolor[0], Rcolor[1], int(event))
- LS = LabelFrame(t,text="L Side",height=20).pack()
- s=Scale(frame2, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=r1)
- s.set(255); s.pack()
- s=Scale(frame2, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=g1)
- s.set(255); s.pack()
- Scale(frame2, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=b1).pack()
- Scale(frame3, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=r2).pack()
- Scale(frame3, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=g2).pack()
- s=Scale(frame3, from_=0, to=255, length=255*2, orient=HORIZONTAL, command=b2)
- s.set(255); s.pack()
- def mixColors(): ### ZZZ yellow + blue = (0,128,0)
- def rgbfix(R):
- aaa=(LC[R]*-1)+(RC[R])
- tmp=[0,1,2]; tmp.pop(R)
- bbb=LC[tmp[0]]+LC[tmp[1]]
- ccc=int((RC[tmp[0]]+RC[tmp[1]]))*-1
- tmp=abs(aaa+bbb+ccc)-637
- if tmp > 0: return tmp
- return 0
- LC,RC = Lcolor,Rcolor
- rgb = [(LC[0]+1)/2, (LC[1]+1)/2, (LC[2]+1)/2] # L
- rgb = [rgb[0]+((RC[0]+1)/2), rgb[1]+((RC[1]+1)/2), rgb[2]+((RC[2]+1)/2)] # R
- # color fix for mixing yellow and blue to correspondingly make green
- rgb = [rgb[0]-rgbfix(2),rgb[1],rgb[2]-rgbfix(2)]
- #print rgbfix(2)
- for i in range(3):
- if rgb[i] > 255: rgb[i] = 255
- elif rgb[i] < 0: rgb[i] = 0
- return rgb
- SidesLR = ''
- def run():
- global SidesLR, subcolor, init
- if SidesLR != Lcolor+Rcolor:
- subcolor = tuple(mixColors())
- SidesLR = Lcolor+Rcolor; create(); t.update()
- init = 1
- t.after(10, run)
- #
- run()
- t.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement