List values and values from dictionary not working together. Simple Python test-project

Viewed 50

I'm trying to create something that would let the user to pick a color and it would return the closest match from the hardcoded list of colors.

I'm completely new to python and I definitely have many more problems with my code but the one thing that I'm not able to work out currently is that my selected_color is probably returning a different type than needed in my distance calculation, so it's always returning the same, wrong value.

from tkinter import *
from tkinter import colorchooser
import math

root = Tk()
root.title("ColorChooser")
root.geometry("400x400")

def get_color():
    c_1 = (254, 252, 245)
    c_2 = (8, 20, 60)
    c_3 = (150, 105, 100)
    c_4 = (146, 19, 13)
    c_5 = (103, 80, 54)
    c_6 = (237, 90, 2)
    c_7 = (220, 136, 9)
    c_8 = (250, 240, 0)

    hardcoded_color = [c_1, c_2, c_3, c_4, c_5, c_6, c_7, c_8]
    color_list = []

    selected_color = colorchooser.askcolor()[0]
    print(selected_color)
    label_color = colorchooser.askcolor()[1]
    my_label = Label(root, text=label_color).pack(pady=10)
    my_label2 = Label(root, text="Your color", font=("Helvetica", 32), bg=label_color).pack()
   

    for i in range (len(hardcoded_color)):
        use_color = hardcoded_color[i] 
        distance = math.sqrt(sum([(a - b) ** 2 for a, b in zip(use_color, selected_color)]))
        color_list.append(distance)

    print("Euclidean distance from x to y: ", color_list)
    closest_color = min(color_list)
    index = color_list.index(closest_color)
    print(hardcoded_color[index])

my_button = Button(root, text="Pick A Color", command=get_color).pack()



root.mainloop()

*bonus question: To display the closest matching color to the user, I'm going to have to somehow hardcode or calculate the hex code, yes?

1 Answers

Use this code:

import tkinter as tk
from tkinter import ttk, Label
from tkinter.colorchooser import askcolor
import math

root = tk.Tk()
root.title('Tkinter Color Chooser')
root.geometry('300x150')


def change_color():
    colors = askcolor(title="Tkinter Color Chooser")
    my_label = Label(root, text=colors[1]).pack(pady=10)
    my_label2 = Label(root, text="Your color", font=("Helvetica", 32), bg=colors[1]).pack()
    root.configure(bg=colors[1])
    
    c_1 = (254, 252, 245)
    c_2 = (8, 20, 60)
    c_3 = (150, 105, 100)
    c_4 = (146, 19, 13)
    c_5 = (103, 80, 54)
    c_6 = (237, 90, 2)
    c_7 = (220, 136, 9)
    c_8 = (250, 240, 0)

    hardcoded_color = [c_1, c_2, c_3, c_4, c_5, c_6, c_7, c_8]
    color_list = []

    selected_color = colors[0]
    print(selected_color)
     
   

    for i in range (len(hardcoded_color)):
        use_color = hardcoded_color[i] 
        distance = math.sqrt(sum([(a - b) ** 2 for a, b in zip(use_color, selected_color)]))
        color_list.append(distance)

    print("Euclidean distance from x to y: ", color_list)
    closest_color = min(color_list)
    index = color_list.index(closest_color)
    print(hardcoded_color[index])


ttk.Button(
    root,
    text='Select a Color',
    command=change_color).pack(expand=True)


root.mainloop()

Output:

enter image description here

Related