import numpy as np
from PIL import Image
import noise
import math
import random
# Constants
WIDTH = HEIGHT = 4096
TOTAL_COLORS = WIDTH * HEIGHT
# Step 1: Generate all RGB colors exactly once
print("Generating RGB color list...")
colors = np.zeros((TOTAL_COLORS, 3), dtype=np.uint8)
index = 0
for r in range(256):
for g in range(256):
for b in range(256):
colors[index] = [r, g, b]
index += 1
assert index == TOTAL_COLORS
# Step 2: Create spatial weights (hallucination-like structure)
print("Generating spatial weights...")
coords = []
scale1 = 6.5
scale2 = 17.0
octaves = 5
for y in range(HEIGHT):
for x in range(WIDTH):
u = (x - WIDTH / 2) / (WIDTH / 2)
v = (y - HEIGHT / 2) / (HEIGHT / 2)
# Multi-scale noise + trigonometric field
n1 = noise.pnoise2(u * scale1, v * scale1, octaves=octaves)
n2 = noise.pnoise2(u * scale2, v * scale2, octaves=octaves)
trig = (
np.sin(8 * (u + v) + n1)
+ np.cos(15 * u * v + n2)
+ np.sin((u**2 - v**2) * 12 + n1)
)
weight = trig + 0.3 * n1 + 0.2 * n2
coords.append((weight, x, y))
coords.sort()
# Step 3: Cluster colors into perceptual groups (brightness + hue)
print("Clustering and reshuffling colors...")
def perceptual_cluster_key(color):
r, g, b = color / 255.0
brightness = 0.3 * r + 0.59 * g + 0.11 * b
hue = math.atan2(math.sqrt(3) * (g - b), 2 * r - g - b) # Improved hue estimation
return (int(hue * 10) % 20, int(brightness * 10)) # Cluster bins
# Sort colors into chunks, then shuffle within and across clusters
clusters = {}
for color in colors:
key = perceptual_cluster_key(color)
clusters.setdefault(key, []).append(color)
# Shuffle colors within each cluster to add internal chaos
for group in clusters.values():
random.shuffle(group)
# Collect and re-shuffle groups to increase global contrast
shuffled_cluster_keys = list(clusters.keys())
random.shuffle(shuffled_cluster_keys)
colors_clustered = []
for key in shuffled_cluster_keys:
colors_clustered.extend(clusters[key])
colors = np.array(colors_clustered, dtype=np.uint8)
assert len(colors) == TOTAL_COLORS, "Color loss or duplication occurred."
# Step 4: Map each color to a spatially-sorted coordinate
print("Mapping hallucinated color field...")
image = Image.new("RGB", (WIDTH, HEIGHT))
pixels = image.load()
for i, (_, x, y) in enumerate(coords):
pixels[x, y] = tuple(colors[i])
# Step 5: Save final image
output_filename = "hallucination_clustered_rgb_exact.png"
print(f"Saving to {output_filename}...")
image.save(output_filename, format="PNG")
print("Done! ✅ Image contains all 16,777,216 RGB colors exactly once, in full chaos.")