-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.py
More file actions
253 lines (202 loc) · 9.66 KB
/
Copy pathcolor.py
File metadata and controls
253 lines (202 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""
Color generation module using HSV color space.
Generates n unique colors with equidistant hues, fixed saturation (100%), and fixed value (100%).
"""
import colorsys
import tkinter as tk
from tkinter import ttk, messagebox
import math
def generate_colors(n):
"""
Generate n unique RGB colors using equidistant hues in HSV color space.
Args:
n (int): Number of colors to generate
Returns:
list: List of RGB tuples, each tuple contains (R, G, B) values in range 0-255
"""
if n <= 0:
return []
if n == 1:
# Single color: use hue 0 (red)
hsv = (0.0, 1.0, 1.0) # Hue=0, Saturation=100%, Value=100%
rgb = colorsys.hsv_to_rgb(*hsv)
return [(int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255))]
# Calculate equidistant hue values
# Step size: 360 / n to maximize difference between colors
hue_step = 360.0 / n
hues = [i * hue_step for i in range(n)]
# Convert to 0-1 range for colorsys (HSV uses 0-1 for all components)
# Saturation and Value are fixed at 100% (1.0)
saturation = 1.0 # 100%
value = 1.0 # 100%
rgb_colors = []
for hue in hues:
# Convert hue from 0-360 to 0-1 range
h = hue / 360.0
hsv = (h, saturation, value)
# Convert HSV to RGB (colorsys returns values in 0-1 range)
rgb_normalized = colorsys.hsv_to_rgb(*hsv)
# Convert to 0-255 range
rgb = (
int(round(rgb_normalized[0] * 255)),
int(round(rgb_normalized[1] * 255)),
int(round(rgb_normalized[2] * 255))
)
rgb_colors.append(rgb)
return rgb_colors
def rgb_to_hex(rgb):
"""Convert RGB tuple to hex color string."""
return f"#{rgb[0]:02X}{rgb[1]:02X}{rgb[2]:02X}"
def create_color_gui():
"""
Create a GUI application to visualize the color generation process.
"""
root = tk.Tk()
root.title("Color Generator - HSV to RGB")
root.geometry("800x700")
root.resizable(True, True)
# Main frame
main_frame = ttk.Frame(root, padding="10")
main_frame.pack(fill=tk.BOTH, expand=True)
# Input section
input_frame = ttk.LabelFrame(main_frame, text="Input", padding="10")
input_frame.pack(fill=tk.X, pady=(0, 10))
ttk.Label(input_frame, text="Number of colors (n):").pack(side=tk.LEFT, padx=5)
n_var = tk.StringVar(value="8")
n_entry = ttk.Entry(input_frame, textvariable=n_var, width=10)
n_entry.pack(side=tk.LEFT, padx=5)
def generate_and_display():
try:
n = int(n_var.get())
if n < 1:
messagebox.showerror("Error", "Number of colors must be at least 1")
return
if n > 100:
messagebox.showerror("Error", "Number of colors cannot exceed 100")
return
# Generate colors
colors = generate_colors(n)
# Clear previous results
for widget in process_frame.winfo_children():
widget.destroy()
for widget in colors_frame.winfo_children():
widget.destroy()
# Display process information
process_text = tk.Text(process_frame, height=8, wrap=tk.WORD, font=("Courier", 9))
process_text.pack(fill=tk.BOTH, expand=True)
process_scroll = ttk.Scrollbar(process_frame, orient=tk.VERTICAL, command=process_text.yview)
process_scroll.pack(side=tk.RIGHT, fill=tk.Y)
process_text.config(yscrollcommand=process_scroll.set)
# Calculate and display process
if n == 1:
hue_step = 0
process_text.insert(tk.END, f"Input: n = {n}\n")
process_text.insert(tk.END, f"\nHSV Parameters:\n")
process_text.insert(tk.END, f" - Hue: 0° (single color)\n")
process_text.insert(tk.END, f" - Saturation: 100% (fixed)\n")
process_text.insert(tk.END, f" - Value: 100% (fixed)\n")
else:
hue_step = 360.0 / n
process_text.insert(tk.END, f"Input: n = {n}\n")
process_text.insert(tk.END, f"\nHSV Parameters:\n")
process_text.insert(tk.END, f" - Hue range: 0° to 360°\n")
process_text.insert(tk.END, f" - Saturation: 100% (fixed)\n")
process_text.insert(tk.END, f" - Value: 100% (fixed)\n")
process_text.insert(tk.END, f" - Hue step: 360° / {n} = {hue_step:.2f}°\n")
process_text.insert(tk.END, f"\nEquidistant Hue Values:\n")
for i, color in enumerate(colors):
hue = i * hue_step
process_text.insert(tk.END, f" Color {i+1}: Hue = {hue:.2f}°\n")
process_text.insert(tk.END, f"\nRGB Conversion:\n")
for i, color in enumerate(colors):
hex_color = rgb_to_hex(color)
process_text.insert(tk.END, f" Color {i+1}: RGB({color[0]}, {color[1]}, {color[2]}) = {hex_color}\n")
process_text.config(state=tk.DISABLED)
# Display colors
colors_label = ttk.Label(colors_frame, text=f"Generated {n} Colors:", font=("Arial", 12, "bold"))
colors_label.pack(pady=(0, 10))
# Create a canvas for color swatches
canvas_frame = ttk.Frame(colors_frame)
canvas_frame.pack(fill=tk.BOTH, expand=True)
# Calculate grid layout
cols = min(8, n) # Maximum 8 columns
rows = math.ceil(n / cols)
swatch_size = 80
spacing = 10
canvas_width = cols * (swatch_size + spacing) + spacing
canvas_height = rows * (swatch_size + spacing + 30) + spacing # Extra space for labels
color_canvas = tk.Canvas(canvas_frame, width=canvas_width, height=canvas_height, bg="white")
color_canvas.pack()
# Draw color swatches
for i, color in enumerate(colors):
row = i // cols
col = i % cols
x1 = spacing + col * (swatch_size + spacing)
y1 = spacing + row * (swatch_size + spacing + 30)
x2 = x1 + swatch_size
y2 = y1 + swatch_size
# Draw color rectangle
hex_color = rgb_to_hex(color)
color_canvas.create_rectangle(x1, y1, x2, y2, fill=hex_color, outline="black", width=1)
# Draw label below
label_y = y2 + 5
color_canvas.create_text(
x1 + swatch_size // 2, label_y,
text=f"#{i+1}",
font=("Arial", 8)
)
color_canvas.create_text(
x1 + swatch_size // 2, label_y + 12,
text=hex_color,
font=("Courier", 7)
)
# Create a scrollable frame for the color list
list_frame = ttk.LabelFrame(colors_frame, text="Color Details", padding="10")
list_frame.pack(fill=tk.BOTH, expand=True, pady=(10, 0))
# Create treeview for color details
tree = ttk.Treeview(list_frame, columns=("Index", "RGB", "Hex", "Hue"), show="headings", height=min(10, n))
tree.heading("Index", text="#")
tree.heading("RGB", text="RGB")
tree.heading("Hex", text="Hex")
tree.heading("Hue", text="Hue (°)")
tree.column("Index", width=50, anchor=tk.CENTER)
tree.column("RGB", width=150, anchor=tk.CENTER)
tree.column("Hex", width=100, anchor=tk.CENTER)
tree.column("Hue", width=100, anchor=tk.CENTER)
for i, color in enumerate(colors):
hue = i * hue_step if n > 1 else 0
tree.insert("", tk.END, values=(
i + 1,
f"({color[0]}, {color[1]}, {color[2]})",
rgb_to_hex(color),
f"{hue:.2f}"
))
tree.pack(fill=tk.BOTH, expand=True)
# Scrollbar for treeview
tree_scroll = ttk.Scrollbar(list_frame, orient=tk.VERTICAL, command=tree.yview)
tree_scroll.pack(side=tk.RIGHT, fill=tk.Y)
tree.config(yscrollcommand=tree_scroll.set)
except ValueError:
messagebox.showerror("Error", "Please enter a valid integer")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
generate_button = ttk.Button(input_frame, text="Generate Colors", command=generate_and_display)
generate_button.pack(side=tk.LEFT, padx=10)
# Process display section
process_frame = ttk.LabelFrame(main_frame, text="Process", padding="10")
process_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 10))
# Colors display section
colors_frame = ttk.LabelFrame(main_frame, text="Generated Colors", padding="10")
colors_frame.pack(fill=tk.BOTH, expand=True)
# Generate initial colors
generate_and_display()
# Bind Enter key to generate
n_entry.bind('<Return>', lambda e: generate_and_display())
# Center window
root.update_idletasks()
x = (root.winfo_screenwidth() // 2) - (root.winfo_width() // 2)
y = (root.winfo_screenheight() // 2) - (root.winfo_height() // 2)
root.geometry(f"+{x}+{y}")
root.mainloop()
if __name__ == "__main__":
create_color_gui()