import numpy as np
import matplotlib.pyplot as plt
import colorsys

def palette_rg(r_steps=256, g_steps=256, b_fixed=128):
    """Fixe B et fait varier R (x) et G (y)."""
    R, G = np.meshgrid(np.linspace(0,1,r_steps), np.linspace(0,1,g_steps))
    B = np.full_like(R, b_fixed/255)
    img = np.stack([R, G, B], axis=2)
    return img

def palette_proj(r_steps=256, g_steps=256):
    """Projection : R en x, G en y, B = (R+G)/2."""
    R, G = np.meshgrid(np.linspace(0,1,r_steps), np.linspace(0,1,g_steps))
    B = (R + G)/2
    img = np.stack([R, G, B], axis=2)
    return img

def palette_hsv(size=256, v_fixed=1.0):
    """Roue chromatique : Hue (angle), Saturation (rayon), V fixé."""
    x = np.linspace(-1,1,size)
    y = np.linspace(-1,1,size)
    X, Y = np.meshgrid(x,y)
    H = (np.arctan2(Y,X)/(2*np.pi)) % 1.0
    S = np.clip(np.sqrt(X**2 + Y**2), 0, 1)
    V = np.full_like(H, v_fixed)
    img = np.zeros((size,size,3))
    for i in range(size):
        for j in range(size):
            img[i,j] = colorsys.hsv_to_rgb(H[i,j], S[i,j], V[i,j])
    return img

def palette_cube(r_steps=64, g_steps=64, b_levels=4):
    """Cube RGB déplié : sous-grilles pour chaque niveau de B."""
    block_h = g_steps
    block_w = r_steps
    img = np.zeros((block_h*b_levels, block_w, 3))
    B_values = np.linspace(0,1,b_levels)
    for k, b in enumerate(B_values):
        R, G = np.meshgrid(np.linspace(0,1,r_steps), np.linspace(0,1,g_steps))
        B = np.full_like(R, b)
        sub = np.stack([R, G, B], axis=2)
        img[k*block_h:(k+1)*block_h,:,:] = sub
    return img

# --- Menu ---
print("Choisis une méthode :")
print("1 - Plan RG avec B fixé")
print("2 - Projection (R,G) avec B = (R+G)/2")
print("3 - Roue HSV (teinte/saturation)")
print("4 - Cube RGB déplié (plusieurs B)")

choix = input("Ton choix (1-4) : ")

if choix == "1":
    img = palette_rg(b_fixed=128)
elif choix == "2":
    img = palette_proj()
elif choix == "3":
    img = palette_hsv()
elif choix == "4":
    img = palette_cube()
else:
    print("Choix invalide, défaut = 1")
    img = palette_rg()

plt.imshow(img)
plt.axis("off")
plt.show()
