Skip to content

Instantly share code, notes, and snippets.

@iuriguilherme
Last active February 28, 2024 21:24
Show Gist options
  • Save iuriguilherme/6b369c85c6c845a259d0efb06fcd8b2f to your computer and use it in GitHub Desktop.
Save iuriguilherme/6b369c85c6c845a259d0efb06fcd8b2f to your computer and use it in GitHub Desktop.
Tk color, RGB and HEXCODE converter
"""https://stackoverflow.com/questions/57651030/take-hex-code-rgb-and-d\
isplay-the-color-in-tkinter"""
import tkinter as tk
from tkinter import *
color_list = [
('black', (0, 0, 0)),
('blue', (0, 0, 255)),
('red', (255, 0, 0)),
('green', (0, 0, 255)),
('yellow', (255, 255, 0)),
('white', (255, 255, 255)),
]
hex_list = [f"{n:02X}" for n in range(256)]
def name_to_hex_rgb(event):
try:
color = color_list[name_listbox.curselection()[0]]
except IndexError:
color = color_list[0]
color_name = color[0]
color_rgb = color[1]
color_hex = f"#{''.join([f'{rgb:02X}' for rgb in color_rgb])}"
color_inverse = f"""#{''.join([f'{rgb:02X}' for rgb in
[255 - color for color in color_rgb]])}"""
name_label.config(
bg = color_hex,
fg = color_inverse,
text = f"""name: {color_name}\nhex: {color_hex}\nrgb: \
{color_rgb}""",
)
def hex_to_rgb(event):
try:
color_1 = hex_listbox_1.curselection()[0]
except IndexError:
color_1 = 0
try:
color_2 = hex_listbox_2.curselection()[0]
except IndexError:
color_2 = 0
try:
color_3 = hex_listbox_3.curselection()[0]
except IndexError:
color_3 = 0
color_rgb = (
int(hex_list[color_1], 16),
int(hex_list[color_2], 16),
int(hex_list[color_3], 16),
)
color_rgb_inverse = (
255 - int(hex_list[color_1], 16),
255 - int(hex_list[color_2], 16),
255 - int(hex_list[color_3], 16),
)
color_hex = f"""#{hex_list[color_1]}\
{hex_list[color_2]}\
{hex_list[color_3]}"""
color_hex_inverse = f"""#{hex_list[255 - color_1]}\
{hex_list[255 - color_2]}\
{hex_list[255 - color_3]}"""
text = f"rgb: {color_rgb}"
color_name = [color[0] for color in color_list if color[1] == \
color_rgb]
if len(color_name) > 0:
text = f"name: {color_name[0]}\n" + text
hex_label.config(bg = color_hex, fg = color_hex_inverse,
text = text)
def rgb_to_hex(event):
try:
color_1 = rgb_listbox_1.curselection()[0]
except IndexError:
color_1 = 0
try:
color_2 = rgb_listbox_2.curselection()[0]
except IndexError:
color_2 = 0
try:
color_3 = rgb_listbox_3.curselection()[0]
except IndexError:
color_3 = 0
color_rgb = (color_1, color_2, color_3)
color_rgb_inverse = (255 - color_1, 255 - color_2, 255 - color_3)
color_hex = f"#{color_1:02X}{color_2:02X}{color_3:02X}"
color_hex_inverse = f"""#{255 - color_1:02X}{255 - color_2:02X}\
{255 - color_3:02X}"""
text = f"hex: {color_hex}"
color_name = [color[0] for color in color_list if color[1] == \
color_rgb]
if len(color_name) > 0:
text = f"name: {color_name[0]}\n" + text
rgb_label.config(bg = color_hex, fg = color_hex_inverse,
text = text)
root = tk.Tk()
root.title("Color, RGB and Hex Converter")
name_frame = Frame(root)
name_frame.pack(anchor = "nw", fill = BOTH, side = "left")
name_label = Label(name_frame, text = " ")
name_label.pack(anchor = "center")
name_title = Label(name_frame, text = "NAME to HEX & RGB")
name_title.pack(anchor = "center")
name_listbox = Listbox(
name_frame,
listvariable = Variable(value = [color[0] for color in color_list]),
justify = "center",
width = 18,
)
name_listbox.bind("<<ListboxSelect>>", name_to_hex_rgb)
name_listbox.pack()
hex_frame = Frame(root)
hex_frame.pack(anchor = "n", fill = BOTH, side = "left")
hex_label = Label(hex_frame, text = "")
hex_label.pack(anchor = "center")
hex_title = Label(hex_frame, text = "HEX to RGB")
hex_title.pack(anchor = "center")
hex_listbox_1 = Listbox(
hex_frame,
listvariable = Variable(value = hex_list),
exportselection = False,
justify = "center",
width = 6,
)
hex_listbox_1.bind("<<ListboxSelect>>", hex_to_rgb)
hex_listbox_1.pack(anchor = "nw", fill = BOTH, side = "left")
hex_listbox_2 = Listbox(
hex_frame,
listvariable = Variable(value = hex_list),
exportselection = False,
justify = "center",
width = 6,
)
hex_listbox_2.bind("<<ListboxSelect>>", hex_to_rgb)
hex_listbox_2.pack(anchor = "n", fill = BOTH, side = "left")
hex_listbox_3 = Listbox(
hex_frame,
listvariable = Variable(value = hex_list),
exportselection = False,
justify = "center",
width = 6,
)
hex_listbox_3.bind("<<ListboxSelect>>", hex_to_rgb)
hex_listbox_3.pack(anchor = "ne", fill = BOTH, side = "left")
rgb_frame = Frame(root)
rgb_frame.pack(anchor = "ne", fill = BOTH, side = "left")
rgb_label = Label(rgb_frame, text = "")
rgb_label.pack(anchor = "center")
rgb_title = Label(rgb_frame, text = "RGB to HEX")
rgb_title.pack(anchor = "center")
rgb_listbox_1 = Listbox(
rgb_frame,
listvariable = Variable(value = list(range(256))),
exportselection = False,
justify = "center",
width = 6,
)
rgb_listbox_1.bind("<<ListboxSelect>>", rgb_to_hex)
rgb_listbox_1.pack(anchor = "nw", fill = BOTH, side = "left")
rgb_listbox_2 = Listbox(
rgb_frame,
listvariable = Variable(value = list(range(256))),
exportselection = False,
justify = "center",
width = 6,
)
rgb_listbox_2.bind("<<ListboxSelect>>", rgb_to_hex)
rgb_listbox_2.pack(anchor = "n", fill = BOTH, side = "left")
rgb_listbox_3 = Listbox(
rgb_frame,
listvariable = Variable(value = list(range(256))),
exportselection = False,
justify = "center",
width = 6,
)
rgb_listbox_3.bind("<<ListboxSelect>>", rgb_to_hex)
rgb_listbox_3.pack(anchor = "ne", fill = BOTH, side = "left")
name_to_hex_rgb(None)
hex_to_rgb(None)
rgb_to_hex(None)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment