import numpy as np
from PIL import Image
import colorsys
import itertools
# --- CONFIG ---
IMAGE_SIZE = 4096
TOTAL_COLORS = IMAGE_SIZE * IMAGE_SIZE
# --- STEP 1: Generate All RGB Colors and Sort by HSB ---
print("Generating and sorting colors...")
def rgb_to_hsb(rgb):
r, g, b = [x / 255.0 for x in rgb]
h, s, v = colorsys.rgb_to_hsv(r, g, b)
return (h, s, v)
# Generate all RGB colors
all_colors = np.array(list(itertools.product(range(256), repeat=3)), dtype=np.uint8)
sorted_colors = sorted(all_colors, key=rgb_to_hsb)
# --- STEP 2: Generate Spiral Coordinates (Corrected Version) ---
print("Generating correct spiral coordinates...")
def generate_spiral_coords(size):
coords = np.zeros((size * size, 2), dtype=np.int32)
x, y = size // 2, size // 2
dx, dy = 1, 0 # Start by moving right
steps = 1 # Number of steps in current leg
idx = 0
while idx < size * size:
for _ in range(2): # Each step count is repeated twice (x and y direction)
for _ in range(steps):
if 0 <= x < size and 0 <= y < size:
coords[idx] = (x, y)
idx += 1
if idx >= size * size:
return coords
x += dx
y += dy
# Rotate direction: right → down → left → up
dx, dy = -dy, dx
steps += 1
return coords
spiral_coords = generate_spiral_coords(IMAGE_SIZE)
# --- STEP 3: Paint the Image ---
print("Painting the image...")
image_array = np.zeros((IMAGE_SIZE, IMAGE_SIZE, 3), dtype=np.uint8)
for idx, (x, y) in enumerate(spiral_coords):
image_array[y, x] = sorted_colors[idx]
# --- STEP 4: Save the Image ---
print("Saving image...")
image = Image.fromarray(image_array, 'RGB')
image.save("spiral_hsb_rgb_universe_fixed.png", "PNG")
print("Done! Image saved as 'spiral_hsb_rgb_universe_fixed.png'")