def __init__(self, parent, hue, color=None, cr=COLORCOR_MAX, cg=int(69 * COLORCOR_MAX / 100), cb=int(94 * COLORCOR_MAX / 100), height=256, width=256, **kwargs): """ Create a ColorSquare. Keyword arguments: * parent: parent window * hue: color square gradient for given hue (color in top right corner is (hue, 100, 100) in HSV * color: initially selected color given in HSV * width, height and any keyword option accepted by a tkinter Canvas """ tk.Canvas.__init__(self, parent, height=height, width=width, **kwargs) self.bg = tk.PhotoImage(width=width, height=height, master=self) self._hue = hue if not color: color = hue2col(self._hue) self.cr = cr self.cg = cg self.cb = cb self.bind('<Configure>', lambda e: self._draw(color)) self.bind('<ButtonPress-1>', self._on_click) self.bind('<B1-Motion>', self._on_move)
def correct_hue(self, hue): r, g, b = hue2col(hue) crf = COLORCOR_MAX/int(self.cr) cgf = COLORCOR_MAX/int(self.cg) cbf = COLORCOR_MAX/int(self.cb) rcor = int(r*crf) gcor = int(g*cgf) bcor = int(b*cbf) if rcor > 255: rcor = 255 if gcor > 255: gcor = 255 if bcor > 255: bcor = 255 args = (rcor,gcor, bcor) h,s,v = rgb_to_hsv(*args) h= hue return h
def _fill(self): """Create the gradient.""" r, g, b = hue2col(self._hue) width = self.winfo_width() height = self.winfo_height() h = float(height - 1) w = float(width - 1) if height: data = [] brightness = int(self._hue) #int(self._hue/255*100) radius = min(height, width) / 2.0 centre = height / 2, width / 2 crf = COLORCOR_MAX / self.cr cgf = COLORCOR_MAX / self.cg cbf = COLORCOR_MAX / self.cb for y in range(height): line = [] ry = y - centre[1] y2 = ry * ry for x in range(width): rx = x - centre[0] x2 = rx * rx s = int((math.sqrt(x2 + y2) / radius) * 100) # s = ((x2 + (y - centre[1])**2.0)**0.5 / radius if s <= 100: h = int(((math.atan2(ry, rx) / math.pi) + 1.0) * 180) h = self._correct_hue_for_disp(h) rxy, gxy, bxy = hsv_to_rgb(h, s, 100) rxy = int(rxy * crf) gxy = int(gxy * cgf) bxy = int(bxy * cbf) if rxy > 255: rxy = 255 if gxy > 255: gxy = 255 if bxy > 255: bxy = 255 color = rgb_to_hexa(rxy, gxy, bxy) else: color = rgb_to_hexa(255, 255, 255) line.append(color) data.append("{" + " ".join(line) + "}") self.bg.put(" ".join(data))
def _fill(self): """Create the gradient.""" r, g, b = hue2col(self._hue) width = self.winfo_width() height = self.winfo_height() h = float(height - 1) w = float(width - 1) if height: c = [(r + i / h * (255 - r), g + i / h * (255 - g), b + i / h * (255 - b)) for i in range(height)] data = [] for i in range(height): line = [] for j in range(width): rij = round2(j / w * c[i][0]) gij = round2(j / w * c[i][1]) bij = round2(j / w * c[i][2]) color = rgb_to_hexa(rij, gij, bij) line.append(color) data.append("{" + " ".join(line) + "}") self.bg.put(" ".join(data))
def _draw_gradient(self, hue): """Draw the gradient and put the cursor on hue.""" self.delete("gradient") self.delete("cursor") del self.gradient width = self.winfo_width() height = self.winfo_height() self.gradient = tk.PhotoImage(master=self, width=width, height=height) line = [] for i in range(width): line.append(rgb_to_hexa(*hue2col(float(i) / width * 360))) line = "{" + " ".join(line) + "}" self.gradient.put(" ".join([line for j in range(height)])) self.create_image(0, 0, anchor="nw", tags="gradient", image=self.gradient) self.lower("gradient") x = hue / 360. * width self.create_line(x, 0, x, height, width=2, tags='cursor')
def test_hue2col(self): self.assertEqual(tkf.hue2col(0), (255, 0, 0)) self.assertRaises(ValueError, tkf.hue2col, 365) self.assertRaises(ValueError, tkf.hue2col, -20)