import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

def pascal_triangle(n):
    """Generate the first n rows of Pascal's triangle."""
    triangle = np.zeros((n, n), dtype=int)
    for i in range(n):
        triangle[i, 0] = 1
        for j in range(1, i + 1):
            triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j]
    return triangle

def plot_pascal_triangle(n):
    """Plot the first n rows of Pascal's triangle with color."""
    triangle = pascal_triangle(n)
    
    fig, ax = plt.subplots(figsize=(10, 10))
    ax.axis('off')

    # Define a color map
    colors = plt.cm.viridis(np.linspace(0, 1, n))
    
    for i in range(n):
        for j in range(i + 1):
            x = i
            y = -i / 2 + j
            color = colors[i]
            intensity = 1 - abs(j - i / 2) / (i / 2 + 1)
            ax.text(y, -x, triangle[i, j], ha='center', va='center',
                    bbox=dict(facecolor=color * intensity, edgecolor='none', boxstyle='round,pad=0.3'))
    
    plt.xlim(-n/2, n/2)
    plt.ylim(-n, 1)
    plt.gca().set_aspect('equal', adjustable='box')
    plt.savefig('triangle_de_pascal.png', dpi=300, bbox_inches='tight')
    plt.show()

plot_pascal_triangle(10)
