def _parse_color(self, value): try: val = self.winfo_rgb(value) except Exception: return "" val = tuple(map(lambda x: round((x / 65535) * 255), val)) return to_hex(val)
def get(self) -> str: # return the hex color string # remember s and v are percentages and underlying hsv converter works with values from 0 to 255 hsv = self.h.get(), self.s.get(), self.v.get() if any(i == "" for i in hsv): return self.initial return to_hex(from_hsv(hsv))
def get(self) -> str: # return the hex color string rgb = self.r.get(), self.g.get(), self.b.get() # we shield ourselves from errors raised when trying to parse empty values if any(i == "" for i in rgb): return self.initial return to_hex(rgb)
def update_panel(self, *_): hsv = self.hue.get_hsv()[0], *self.col.get_hsv()[1:] hex_string = color.to_hex(color.from_hsv(hsv)) # We have set panel value implicitly so we update the hex value ourselves self.monitor.set(hex_string, True) self.monitor.hex_string.set(hex_string) if self._on_change: self._on_change(self.monitor.get())
def update_panel(self, *_): hsl = self.hue.get_hsl()[0], self.sat.get_hsl()[1], self.lum.get_hsl( )[2] hex_string = color.to_hex(color.from_hsl(hsl)) # We have set panel value implicitly so we update the hex value ourselves self.monitor.set(hex_string, True) self.monitor.hex_string.set(hex_string) if self._on_change: self._on_change(self.monitor.get())
def _parse_color(self, value): if value == "" and self.style_def.get("allow_transparent", False): return value try: val = self.winfo_rgb(value) except Exception: return None val = tuple(map(lambda x: round((x / 65535) * 255), val)) return to_hex(val)
def on_hex_string_changed(self): # If the color format is incorrect no need to update the panel # If the string is 8 bit we need to expand it using to_hex if len(self.hex_string.get()) == 4: self.current_model.set(to_hex(self.hex_string.get())) self.callback(self.current_model.get()) # if the color is 8 bit then we can update without errors elif len(self.hex_string.get()) == 7: self.current_model.set(self.hex_string.get()) self.callback(self.current_model.get())
def process(self, event): displace_x = 10 if self.winfo_screenwidth( ) - event.x_root > 60 else -50 displace_y = 0 if self.winfo_screenheight( ) - event.y_root > 50 else -40 self.place(x=event.x_root + displace_x, y=event.y_root + displace_y, width=40, height=40) color = self.pixel_access[event.x_root, event.y_root] self.color = to_hex(color) self.label.config(bg=self.color)
def __init__(self, master=None, color_array=None, **cnf): super().__init__(master, **cnf) self.config(**self.style.dark, **self.style.dark_highlight) self.color_strip = Canvas(self, width=230, height=27, **self.style.dark, highlightthickness=0) self.color_strip.pack(pady=5) self.color_strip.tk.eval(_ColorStrip._RECOLOR_PROC) setattr(self.color_strip, "pos", 5) self.color_strip.bind("<ButtonPress-1>", self._drag_start) self.color_strip.bind("<ButtonRelease>", self._drag_end) self.color_strip.bind("<Motion>", self._adjust) self.drag_state = False x, y = 5, 0 self._width = int(self.color_strip['width']) self.step = step = (self._width - 10) / len(color_array) self.color_array = color_array for c in color_array: fill = color.to_hex(c) self.color_strip.create_rectangle(x, y, x + step, y + 15, fill=fill, width=0) x = x + step self.callback, self.implicit_callback = None, None self.selector = self.color_strip.create_polygon( 5, 15, 0, 25, 10, 25, splinesteps=1, joinstyle="miter", fill="#5a5a5a", outline="#f7f7f7", activeoutline="#3d8aff") # First, I apologise for the crappy syntax # But this was the quickest way to generate an anonymous event object for use with _adjust # Considering we are working inside a lambda function! # This should allow the use of arrow keys to adjust the strip self.bind( "<Left>", lambda e: self._adjust( type("", (), {"x": self.color_strip.pos - 1})(), True)) self.bind( "<Right>", lambda e: self._adjust( type("", (), {"x": self.color_strip.pos + 1})(), True))
def pick_from_clipboard(self, *_): value = self.clipboard_get() try: # convert color from clipboard to general rgb format first # if the color is valid this should not throw any errors # this means if the clipboard contains "red" what is set finally is "#ff0000" r, g, b = self.winfo_rgb(value) # returns 12-bit color # scale down from 12-bit color to 8-bit color that the color-chooser understands color = to_hex( tuple(map(lambda x: round(x * (255 / 65535)), (r, g, b)))) self.set(color) except Exception: # Not a valid color so ignore # TODO Show a message pass
def __init__(self, master, hue, **cnf): super().__init__(master, **cnf) self.hue = hue self._color = None self.color_space = Canvas(self, width=230, height=100, **self.style.surface, highlightthickness=0) self.color_space.pack() self.color_space.bind("<ButtonPress-1>", self._drag_start) self.color_space.bind("<ButtonRelease>", self._drag_end) self.color_space.bind("<Motion>", self._adjust) self.drag_state = False x, y = 0, 0 self.pos = x, y self._width = w = int(self.color_space['width']) self._height = h = int(self.color_space['height']) step_x = round(w / 50) step_y = round(h / 50) for i in range(0, 101, 2): for j in range(100, -1, -2): fill = color.to_hex(color.from_hsv((hue, i, j))) self.color_space.create_rectangle(x, y, x + step_x, y + step_y, fill=fill, width=0) y += step_y y = 0 x += step_x self.callback, self.implicit_callback = None, None self.selector = self.color_space.create_oval(0, 0, 1, 1, outline="#f7f7f7", activeoutline="#3d8aff")
def __init__(self, master=None, color_array=None, **cnf): super().__init__(master, **cnf) self.color_strip = Canvas(self, width=230, height=27, **self.style.surface, highlightthickness=0) self.color_strip.pack(pady=5) self.color_strip.bind("<ButtonPress-1>", self._drag_start) self.color_strip.bind("<ButtonRelease>", self._drag_end) self.color_strip.bind("<Motion>", self._adjust) self.drag_state = False x, y = 5, 0 self.pos = x, y self._width = int(self.color_strip['width']) self.step = step = (self._width - 10) / len(color_array) self.color_array = color_array for c in color_array: fill = color.to_hex(c) self.color_strip.create_rectangle(x, y, x + step, y + 15, fill=fill, width=0) x = x + step self.selector = self.color_strip.create_polygon( 5, 15, 0, 25, 10, 25, splinesteps=1, joinstyle="miter", fill="#5a5a5a", outline="#f7f7f7", activeoutline="#3d8aff")
def get(self) -> str: # return the hex color string hsl = self.h.get(), self.s.get(), self.l.get() if any(i == "" for i in hsl): return self.initial return to_hex(from_hsl(hsl))
def get(self) -> str: # return the hex color string # remember s and v are percentages and underlying hsv converter works with values from 0 to 255 return to_hex(from_hsv((self.h.get(), self.s.get(), self.v.get())))
def get(self) -> str: # return the hex color string return to_hex(from_hsl((self.h.get(), self.s.get(), self.l.get())))
def get(self): return color.to_hex(color.from_hsv(self._color))