def related(self, y, i, q): lumas = [0, 20, 40, 60, 80, 100] for ix, luma in enumerate(lumas): relcol = rgb2hash(*yiq_to_rgb(luma, i, q)) self.rccans[ix]['background'] = relcol self.rccans[ix].background = relcol self.rcls[ix]['text'] = relcol comp_col = rgb2hash(*yiq_to_rgb(100 - y, -i, -q)) self.cccan['background'] = comp_col self.cccan.background = comp_col self.ccl['text'] = comp_col
def checksyiq(self, evt): """Procedure called by yiq spinboxes. Parameters ---------- evt : str bind handles """ y = float(self.yvar.get()) i = float(self.ivar.get()) q = float(self.qvar.get()) from_colour = yiq_to_rgb(*(0, i, q)) to_colour = yiq_to_rgb(*(100, i, q)) draw_gradient(self.cans[0], from_colour, to_colour, width=self.canvas_w) from_colour = yiq_to_rgb(*(y, -100, q)) to_colour = yiq_to_rgb(*(y, 100, q)) draw_gradient(self.cans[1], from_colour, to_colour, width=self.canvas_w) from_colour = yiq_to_rgb(*(y, i, -100)) to_colour = yiq_to_rgb(*(y, i, 100)) draw_gradient(self.cans[2], from_colour, to_colour, width=self.canvas_w)
def door_bell(self, ring): """Bind callback from cursor. Parameters ---------- ring : tuple of int y, i, q values """ # calls from bind i, q = ring y = float(self.yvar.get()) from_colour = yiq_to_rgb(*(0, i, q)) to_colour = yiq_to_rgb(*(100, i, q)) draw_gradient(self.cans[0], from_colour, to_colour, width=self.canvas_w) from_colour = yiq_to_rgb(*(y, 0, q)) to_colour = yiq_to_rgb(*(y, 100, q)) draw_gradient(self.cans[1], from_colour, to_colour, width=self.canvas_w) from_colour = yiq_to_rgb(*(y, i, 0)) to_colour = yiq_to_rgb(*(y, i, 100)) draw_gradient(self.cans[2], from_colour, to_colour, width=self.canvas_w) self.overlord(yiq=(y, i, q))
def yhandle(self, *evt): """Command callback for y. Parameters ---------- evt : str command handle """ y = float(self.yvar.get()) i = float(self.ivar.get()) q = float(self.qvar.get()) from_colour = yiq_to_rgb(*(0, i, q)) to_colour = yiq_to_rgb(*(100, i, q)) draw_gradient(self.cans[0], from_colour, to_colour, width=self.canvas_w) from_colour = yiq_to_rgb(*(y, -100, q)) to_colour = yiq_to_rgb(*(y, 100, q)) draw_gradient(self.cans[1], from_colour, to_colour, width=self.canvas_w) from_colour = yiq_to_rgb(*(y, i, -100)) to_colour = yiq_to_rgb(*(y, i, 100)) draw_gradient(self.cans[2], from_colour, to_colour, width=self.canvas_w) self.overlord(yiq=(y, i, q))
def resize_yiq(self, event, can=''): W = event.width self.canvas_w = W y = float(self.yvar.get()) i = float(self.ivar.get()) q = float(self.qvar.get()) if can == self.cans[0]: from_colour = yiq_to_rgb(*(0, i, q)) to_colour = yiq_to_rgb(*(100, i, q)) draw_gradient(self.cans[0], from_colour, to_colour, width=W) if can == self.cans[1]: from_colour = yiq_to_rgb(*(y, -100, q)) to_colour = yiq_to_rgb(*(y, 100, q)) draw_gradient(self.cans[1], from_colour, to_colour, width=W) if can == self.cans[2]: from_colour = yiq_to_rgb(*(y, i, -100)) to_colour = yiq_to_rgb(*(y, i, 100)) draw_gradient(self.cans[2], from_colour, to_colour, width=W)
def iqhandle(self, *evt): """Command callback for i. Parameters ---------- evt : str command handle """ y = float(self.yvar.get()) i = float(self.ivar.get()) q = float(self.qvar.get()) from_colour = yiq_to_rgb(*(0, i, q)) to_colour = yiq_to_rgb(*(100, i, q)) draw_gradient(self.cans[0], from_colour, to_colour, width=self.canvas_w) from_colour = yiq_to_rgb(*(y, -100, q)) to_colour = yiq_to_rgb(*(y, 100, q)) draw_gradient(self.cans[1], from_colour, to_colour, width=self.canvas_w) from_colour = yiq_to_rgb(*(y, i, -100)) to_colour = yiq_to_rgb(*(y, i, 100)) draw_gradient(self.cans[2], from_colour, to_colour, width=self.canvas_w) X = i * 3 / 2 + 150 Y = q * 3 / 2 + 150 ring_radius = self.ring_radius for s in self.canYiq.find_withtag("ring"): self.canYiq.coords(s, X - ring_radius, Y - ring_radius, X + ring_radius, Y + ring_radius) self.overlord(yiq=(y, i, q))
def build(self): """Widget construction.""" lf1 = LabelFrame(self.fr0, text='rgb') lf1.grid(column=0, row=0, sticky='new') lf1.columnconfigure(1, weight=1) vcmdsb = root.register(sb_okay) self.rgbcans = [] rgbsboxes = [] rgbcomps = ['r', 'g', 'b', 'a'] rgbnames = ['red ', 'green', 'blue ', 'alpha'] rgbvars = [self.rvar, self.gvar, self.bvar, self.avar] rgbhandles = [ self.rgbhandle, self.rgbhandle, self.rgbhandle, self.ahandle ] binds = [self.checksb, self.checksb, self.checksb, self.checksba] for ix, comp in enumerate(rgbcomps): Label(lf1, text=rgbnames[ix]).grid(row=3 * ix, column=0) Label(lf1).grid(row=2 + 3 * ix, column=2) self.rgbcans.append( Canvas(lf1, height=self.canvas_h, width=self.canvas_w, bd=0, highlightthickness=0)) self.rgbcans[ix].grid(row=3 * ix, column=1, sticky='ew', padx=self.sliderlength // 2) def handler_rgb(event, self=self, ix=ix): return self, self.resize(event, can=self.rgbcans[ix]) self.rgbcans[ix].bind("<Configure>", handler_rgb) #can= self.rgbcans[ix])) TtkScale(lf1, self.scale_l, from_=0, to=255, variable=rgbvars[ix], orient='horizontal', command=rgbhandles[ix], tickinterval=20).grid(row=1 + 3 * ix, column=1, sticky='new') rgbsboxes.append( Spinbox(lf1, from_=0, to=255, textvariable=rgbvars[ix], validate='key', validatecommand=(vcmdsb, '%d', '%P', '%S', 0, 255), command=rgbhandles[ix], width=5)) rgbsboxes[ix].grid(row=1 + 3 * ix, column=2, sticky='nw') rgbsboxes[ix].bind('<KeyRelease>', binds[ix]) lf3 = LabelFrame(self.fr0, text='colour mix') lf3.grid(column=1, row=0, sticky='nw') self.cmcan = cmcan = Canvas(lf3, width=30, height=30, bd=0, highlightthickness=0) cmcan.grid(column=0, row=0, sticky='n', columnspan=2) cmcan.grid_propagate(0) vdraw_gradient(self.cmcan, (255, 0, 0), alpha=255) cml = Label(lf3, text='hash\nvalue') cml.grid(column=0, row=1) vcmd = root.register(is_okay) self.en = en = Entry(lf3, width=8, validate='key', textvariable=self.evar, validatecommand=(vcmd, '%i', '%P', '%S')) en.grid(column=1, row=1) en.bind('<KeyRelease>', self.checkhash) lf5 = LabelFrame(lf3, text='related colours') lf5.grid(column=0, row=2, sticky='nw', columnspan=2) self.rcls = [] self.rccans = [] relateds = [0, 20, 40, 60, 80, 100] rtexts = ['0% Y', '20% Y', '40% Y', '60% Y', '80% Y', '100% Y'] for ix, rel in enumerate(relateds): Label(lf5, text=rtexts[ix]).grid(row=1 + 2 * ix, column=0, sticky='n') self.rcls.append(Label(lf5)) self.rcls[ix].grid(row=1 + 2 * ix, column=1, sticky='n') self.rccans.append( Canvas(lf5, width=30, height=30, bd=0, highlightthickness=0)) self.rccans[ix].grid(row=2 * ix, column=0, sticky='n', columnspan=2) self.cccan = Canvas(lf5, width=30, height=30, bd=0, highlightthickness=0) self.cccan.grid(column=0, row=12, sticky='n', columnspan=2) self.ccla = Label(lf5, text="comp'y") self.ccla.grid(column=0, row=13, sticky='n') self.ccl = Label(lf5, text="") self.ccl.grid(column=1, row=13, sticky='n') draw_gradient(self.rgbcans[0], (0, 0, 0), (255, 0, 0), width=self.canvas_w) draw_gradient(self.rgbcans[1], (255, 0, 0), (255, 255, 0), width=self.canvas_w) draw_gradient(self.rgbcans[2], (255, 0, 0), (255, 0, 255), width=self.canvas_w) draw_agradient(self.rgbcans[3], (127, 127, 127), (255, 0, 0), width=self.canvas_w) lf4 = LabelFrame(self.fr0, text='yiq') lf4.grid(column=2, row=0, sticky='ew') lf4.columnconfigure(1, weight=1) vcmdyiq = root.register(yiq_okay) self.cans = [] sboxes = [] comps = ['y', 'i', 'q'] names = ['luma', 'i hue', 'q hue'] tkvars = [self.yvar, self.ivar, self.qvar] handles = [self.yhandle, self.iqhandle, self.iqhandle] froms = [0, -100, -100] ticks = [10, 20, 20] for ix, comp in enumerate(comps): Label(lf4, text=names[ix]).grid(row=3 * ix, column=0) Label(lf4).grid(row=2 + 3 * ix, column=2) self.cans.append( Canvas(lf4, width=self.canvas_w, height=self.canvas_h, bd=0, highlightthickness=0)) self.cans[ix].grid(row=3 * ix, column=1, sticky='ew', padx=self.sliderlength // 2) def handler_yiq(event, self=self, ix=ix): return self, self.resize_yiq(event, can=self.cans[ix]) self.cans[ix].bind("<Configure>", handler_yiq) #can=self.cans[ix])) TtkScale(lf4, from_=froms[ix], to=100, variable=tkvars[ix], orient='horizontal', length=self.scale_l, command=handles[ix], tickinterval=ticks[ix]).grid(row=1 + 3 * ix, column=1, sticky='new') sboxes.append( Spinbox(lf4, from_=froms[ix], to=100, textvariable=tkvars[ix], validatecommand=(vcmdyiq, '%d', '%P', '%S', froms[ix], 100), validate='key', command=handles[ix], width=5, increment=1)) sboxes[ix].grid(row=1 + 3 * ix, column=2, sticky='nw') sboxes[ix].bind('<KeyRelease>', self.checksyiq) # assume initial setting 30,100.0,40.56 yiq to_colour = yiq_to_rgb(*(30, 100.0, 40.56)) draw_gradient(self.cans[0], yiq_to_rgb(0.0, 100.0, 40.56), yiq_to_rgb(100, 100, 40.56), width=self.canvas_w) draw_gradient(self.cans[1], yiq_to_rgb(30, -100.0, 40.56), to_colour, width=self.canvas_w) draw_gradient(self.cans[2], yiq_to_rgb(30, 100, -100), yiq_to_rgb(30, 100, 100), width=self.canvas_w) self.related(30, 100.0, 40.56) self.canYiq = canYiq = Canvas(lf4, width=self.space, height=self.space) canYiq.grid(column=0, row=9, columnspan=3, pady=25, sticky='n') self.yiqGamut = PhotoImage(file='../figures/colour_space.png') canYiq.create_image(0, 0, anchor='nw', image=self.yiqGamut) self.ring = circle(canYiq, 300.0, 210.84, self.ring_radius, width=self.ring_width, activeoutline='#555555', tags='ring') canYiq.bind('<Button-1>', self.move_ring) canYiq.tag_bind('ring', '<B1-Motion>', self.move_ring)
def overlord(self, rgb=None, yiq=None): """Supervisory procedure to control calls from handles. Parameters ---------- rgb : tuple of integers rgb yiq : tuple of integers yiq Returns ------- draws gradients and sets other colour system """ if rgb: red, green, blue = rgb[0], rgb[1], rgb[2] y, i, q = rgb_to_yiq(red, green, blue) from_colour = yiq_to_rgb(*(0, i, q)) to_colour = yiq_to_rgb(*(100, i, q)) draw_gradient(self.cans[0], from_colour, to_colour, width=self.canvas_w) from_colour = yiq_to_rgb(*(y, -100, q)) to_colour = yiq_to_rgb(*(y, 100, q)) draw_gradient(self.cans[1], from_colour, to_colour, width=self.canvas_w) from_colour = yiq_to_rgb(*(y, i, -100)) to_colour = yiq_to_rgb(*(y, i, 100)) draw_gradient(self.cans[2], from_colour, to_colour, width=self.canvas_w) self.yvar.set(y) self.ivar.set(i) self.qvar.set(q) X = i * 3 / 2 + 150 Y = q * 3 / 2 + 150 ring_radius = self.ring_radius for s in self.canYiq.find_withtag("ring"): self.canYiq.coords(s, X - ring_radius, Y - ring_radius, X + ring_radius, Y + ring_radius) self.related(y, i, q) if yiq: y, i, q = yiq[0], yiq[1], yiq[2] red, green, blue = yiq_to_rgb(y, i, q) draw_agradient(self.rgbcans[3], (127, 127, 127), (red, green, blue), width=self.canvas_w) alpha = self.avar.get() vdraw_gradient(self.cmcan, (red, green, blue), alpha=alpha) draw_gradient(self.rgbcans[0], (0, green, blue), (255, green, blue), width=self.canvas_w) draw_gradient(self.rgbcans[1], (red, 0, blue), (red, 255, blue), width=self.canvas_w) draw_gradient(self.rgbcans[2], (red, green, 0), (red, green, 255), width=self.canvas_w) self.evar.set(rgb2hash(red, green, blue)) self.rvar.set(red) self.gvar.set(green) self.bvar.set(blue) self.related(y, i, q)