Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:param parent: parent widget
:type parent: widget
:param hue: hue (between 0 and 360) of the color square gradient
(color in top right corner is (hue, 100, 100) in HSV)
:type hue: int
:param color: initially selected color given in HSV format
:type color: tuple[int]
:param height: height of the widget in pixels
:type height: int
:param width: width of the widget in pixels
:type width: int
:param kwargs: options to be passed on to the :class:`tk.Canvas` initializer
"""
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.bind('', lambda e: self._draw(color))
self.bind('', self._on_click)
self.bind('', self._on_move)
def _draw(self, color):
"""Draw the gradient and the selection cross on the canvas."""
width = self.winfo_width()
height = self.winfo_height()
self.delete("bg")
self.delete("cross_h")
self.delete("cross_v")
del self.bg
self.bg = tk.PhotoImage(width=width, height=height, master=self)
self._fill()
self.create_image(0, 0, image=self.bg, anchor="nw", tags="bg")
self.tag_lower("bg")
h, s, v = color
x = v / 100.
y = (1 - s / 100.)
self.create_line(0, y * height, width, y * height, tags="cross_h",
fill="#C2C2C2")
self.create_line(x * width, 0, x * width, height, tags="cross_v",
fill="#C2C2C2")
hue = int(variable.get())
except Exception:
pass
else:
self._variable = tk.IntVar(self)
if hue > 360:
hue = 360
elif hue < 0:
hue = 0
self._variable.set(hue)
try:
self._variable.trace_add("write", self._update_hue)
except Exception:
self._variable.trace("w", self._update_hue)
self.gradient = tk.PhotoImage(master=self, width=width, height=height)
self.bind('', lambda e: self._draw_gradient(hue))
self.bind('', self._on_click)
self.bind('', self._on_move)
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="gardient",
image=self.gradient)
self.lower("gradient")
x = hue / 360. * width
self.create_line(x, 0, x, height, width=2, tags='cursor')
:param parent: parent widget
:type parent: widget
:param alpha: initially selected alpha value (between 0 and 255)
:type alpha: int
:param color: gradient color in RGB format
:type color: tuple[int]
:param variable: variable linked to the alpha value
:type variable: IntVar
:param height: height of the widget in pixels
:type height: int
:param width: width of the widget in pixels
:type width: int
:param kwargs: options to be passed on to the :class:`tk.Canvas` initializer
"""
tk.Canvas.__init__(self, parent, width=width, height=height, **kwargs)
self.gradient = tk.PhotoImage(master=self, width=width, height=height)
self._variable = variable
if variable is not None:
try:
alpha = int(variable.get())
except Exception:
pass
else:
self._variable = tk.IntVar(self)
if alpha > 255:
alpha = 255
elif alpha < 0:
alpha = 0
self._variable.set(alpha)
try:
self._variable.trace_add("write", self._update_alpha)