File size: 5,071 Bytes
bb897c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405fea2
bb897c4
 
 
 
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
#wallpaper

import gradio as gr
import math
import random

def generate_svg(width, height, num_shapes, symmetry_group, shape_complexity, color_scheme):
    # Calculate the size of a single tile
    tile_width = width // 3
    tile_height = height // 3

    svg = f'<svg width="{width}" height="{height}" xmlns="http://www.w3.org/2000/svg">'
    
    # Create a group for the tile
    svg += f'<g id="tile">'

    def create_shape(x, y):
        shape_type = random.choice(['circle', 'rect', 'polygon', 'star', 'flower'])
        colors = get_color_scheme(color_scheme)
        color = random.choice(colors)
        
        if shape_type == 'circle':
            r = random.randint(5, 20)
            return f'<circle cx="{x}" cy="{y}" r="{r}" fill="{color}" />'
        elif shape_type == 'rect':
            w, h = random.randint(10, 40), random.randint(10, 40)
            return f'<rect x="{x-w/2}" y="{y-h/2}" width="{w}" height="{h}" fill="{color}" />'
        elif shape_type == 'polygon':
            points = " ".join([f"{x+random.randint(-20,20)},{y+random.randint(-20,20)}" for _ in range(shape_complexity)])
            return f'<polygon points="{points}" fill="{color}" />'
        elif shape_type == 'star':
            points = star_points(x, y, 5, 10, 20)
            return f'<polygon points="{points}" fill="{color}" />'
        else:  # flower
            petals = flower_petals(x, y, shape_complexity, 20)
            return f'<path d="{petals}" fill="{color}" />'

    def apply_symmetry(shape, group):
        shapes = [shape]
        if 'p1' in group:  # Translation
            shapes.append(shape.replace(f'x="{x}"', f'x="{x+50}"').replace(f'y="{y}"', f'y="{y+50}"'))
        if 'pm' in group:  # Reflection
            shapes.append(shape.replace(f'x="{x}"', f'x="{tile_width-x}"'))
        if 'pg' in group:  # Glide reflection
            shapes.append(shape.replace(f'x="{x}"', f'x="{tile_width-x}"').replace(f'y="{y}"', f'y="{y+50}"'))
        if 'cm' in group:  # Reflection + Glide reflection
            shapes.append(shape.replace(f'x="{x}"', f'x="{tile_width-x}"'))
            shapes.append(shape.replace(f'x="{x}"', f'x="{tile_width-x}"').replace(f'y="{y}"', f'y="{y+50}"'))
        if 'p2' in group:  # 180° rotation
            shapes.append(shape.replace(f'x="{x}"', f'x="{tile_width-x}"').replace(f'y="{y}"', f'y="{tile_height-y}"'))
        if 'p4' in group:  # 90° rotation
            for i in range(1, 4):
                angle = i * 90
                shapes.append(f'<g transform="rotate({angle}, {tile_width/2}, {tile_height/2})">{shape}</g>')
        return "".join(shapes)

    for _ in range(num_shapes):
        x, y = random.randint(0, tile_width), random.randint(0, tile_height)
        shape = create_shape(x, y)
        svg += apply_symmetry(shape, symmetry_group)

    svg += '</g>'

    # Use the tile to create a 3x3 grid
    for i in range(3):
        for j in range(3):
            svg += f'<use href="#tile" x="{i*tile_width}" y="{j*tile_height}" />'

    svg += '</svg>'
    return svg

def star_points(cx, cy, inner_radius, outer_radius, num_points):
    points = []
    for i in range(num_points * 2):
        angle = math.pi * i / num_points
        radius = inner_radius if i % 2 == 0 else outer_radius
        x = cx + math.cos(angle) * radius
        y = cy + math.sin(angle) * radius
        points.append(f"{x},{y}")
    return " ".join(points)

def flower_petals(cx, cy, num_petals, size):
    path = f"M {cx},{cy}"
    for i in range(num_petals):
        angle = 2 * math.pi * i / num_petals
        x1 = cx + math.cos(angle) * size * 0.5
        y1 = cy + math.sin(angle) * size * 0.5
        x2 = cx + math.cos(angle) * size
        y2 = cy + math.sin(angle) * size
        path += f" Q {x1},{y1} {x2},{y2} Q {x1},{y1} {cx},{cy}"
    return path

def get_color_scheme(scheme):
    schemes = {
        'Pastel': ['#FFB3BA', '#BAFFC9', '#BAE1FF', '#FFFFBA'],
        'Vibrant': ['#FF1493', '#00FF00', '#1E90FF', '#FFD700'],
        'Earthy': ['#8B4513', '#556B2F', '#8B8B83', '#D2691E'],
        'Monochrome': ['#000000', '#333333', '#666666', '#999999']
    }
    return schemes.get(scheme, schemes['Pastel'])

def generate_wallpaper(width, height, num_shapes, symmetry_group, shape_complexity, color_scheme):
    svg = generate_svg(width, height, num_shapes, symmetry_group, shape_complexity, color_scheme)
    return svg

iface = gr.Interface(
    fn=generate_wallpaper,
    inputs=[
        gr.Slider(300, 900, 600, label="Width"),
        gr.Slider(300, 900, 600, label="Height"),
        gr.Slider(1, 50, 10, step=1, label="Number of Shapes"),
        gr.Dropdown(["p1", "pm", "pg", "cm", "p2", "p4"], label="Symmetry Group"),
        gr.Slider(3, 10, 5, step=1, label="Shape Complexity"),
        gr.Dropdown(["Pastel", "Vibrant", "Earthy", "Monochrome"], label="Color Scheme")
    ],
    outputs=gr.HTML(),
    title="An SVG Wallpaper Pattern Generator v2",
    description="Generate custom SVG wallpaper patterns with various symmetry groups and shape types."
)

iface.launch(share=True)