import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
root.title("高级配色练习")
root.geometry("650x520")
root.resizable(False, False)

base_colors = [
    "#FF0000","#FF7700","#FFFF00","#00FF00","#0088FF","#0000FF",
    "#9900FF","#FF00AA","#000000","#555555","#AAAAAA","#FFFFFF",
    "#8B4513","#2F4F4F","#800080","#B22222","#32CD32","#1E90FF"
]
history_colors = []
now_color = "#808080"

# 预览区域
preview_frame = tk.Frame(root, bd=3, relief=tk.GROOVE)
preview_frame.pack(pady=10, fill="x", padx=20)

show_canvas = tk.Canvas(preview_frame, width=220, height=140, bg="#808080")
show_canvas.grid(row=0, column=0, padx=10)
contrast_canvas = tk.Canvas(preview_frame, width=220, height=140, bg="#ffffff")
contrast_canvas.grid(row=0, column=1, padx=10)

info_label = tk.Label(root, text="RGB:128,128,128 | HEX:#808080", font=("微软雅黑",11))
info_label.pack()

def set_color(color):
    global now_color
    now_color = color
    show_canvas.config(bg=color)
    r = int(color[1:3],16)
    g = int(color[3:5],16)
    b = int(color[5:7],16)
    info_label.config(text=f"RGB:{r},{g},{b} | HEX:{color}")
    add_history(color)

def add_history(c):
    if c not in history_colors:
        history_colors.append(c)
        if len(history_colors) > 12:
            history_colors.pop(0)
        refresh_history()

def refresh_history():
    for widget in history_frame.winfo_children():
        widget.destroy()
    idx = 0
    for c in history_colors:
        btn = tk.Button(history_frame, bg=c, width=3, height=2, command=lambda x=c:set_color(x))
        btn.grid(row=idx//6, column=idx%6, padx=2, pady=2)
        idx += 1

def copy_hex():
    root.clipboard_clear()
    root.clipboard_append(now_color)
    messagebox.showinfo("提示","色值复制成功")

def light_adjust(val):
    scale = int(val) / 100
    r = int(now_color[1:3],16)
    g = int(now_color[3:5],16)
    b = int(now_color[5:7],16)
    nr = min(255, int(r * scale))
    ng = min(255, int(g * scale))
    nb = min(255, int(b * scale))
    new_c = f"#{nr:02x}{ng:02x}{nb:02x}"
    show_canvas.config(bg=new_c)

# RGB滑块
rgb_frame = tk.Frame(root)
rgb_frame.pack(pady=8)
r_var = tk.IntVar(value=128)
g_var = tk.IntVar(value=128)
b_var = tk.IntVar(value=128)

def slide_change(event=None):
    r = r_var.get()
    g = g_var.get()
    b = b_var.get()
    hex_c = f"#{r:02x}{g:02x}{b:02x}"
    set_color(hex_c)

tk.Scale(rgb_frame,from_=0,to=255,variable=r_var,orient=tk.HORIZONTAL,
         command=slide_change,label="红",length=110).grid(row=0,column=0,padx=3)
tk.Scale(rgb_frame,from_=0,to=255,variable=g_var,orient=tk.HORIZONTAL,
         command=slide_change,label="绿",length=110).grid(row=0,column=1,padx=3)
tk.Scale(rgb_frame,from_=0,to=255,variable=b_var,orient=tk.HORIZONTAL,
         command=slide_change,label="蓝",length=110).grid(row=0,column=2,padx=3)

# 亮度调节
tk.Label(root,text="亮度调节").pack()
tk.Scale(root,from_=30,to=180,orient=tk.HORIZONTAL,command=light_adjust,length=300).pack()

# 基础色块
tk.Label(root,text="基础颜色库",font=("微软雅黑",10)).pack(pady=3)
base_frame = tk.Frame(root)
base_frame.pack()
i = 0
for col in base_colors:
    btn = tk.Button(base_frame,bg=col,width=3,height=2,command=lambda x=col:set_color(x))
    btn.grid(row=i//9,column=i%9,padx=2,pady=2)
    i += 1

# 历史记录
tk.Label(root,text="历史颜色",font=("微软雅黑",10)).pack(pady=3)
history_frame = tk.Frame(root)
history_frame.pack()

# 功能按钮
btn_frame = tk.Frame(root)
btn_frame.pack(pady=10)
tk.Button(btn_frame,text="复制色值",command=copy_hex,bg="#4285F4",fg="white").grid(row=0,column=0,padx=10)
tk.Button(btn_frame,text="清空历史",command=lambda:(history_colors.clear(),refresh_history()),bg="#EA4335",fg="white").grid(row=0,column=1,padx=10)

root.mainloop()