File size: 6,379 Bytes
aaac77b
dd58977
aaac77b
7cb998c
 
aaac77b
 
6b9866e
aaac77b
 
 
 
6b9866e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaac77b
 
 
 
 
 
6b9866e
aaac77b
 
 
 
 
 
 
6b9866e
aaac77b
 
 
 
6b9866e
aaac77b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b9866e
aaac77b
 
6b9866e
 
aaac77b
6b9866e
 
 
aaac77b
6b9866e
aaac77b
6b9866e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaac77b
 
 
 
6b9866e
 
 
 
 
 
 
aaac77b
 
6b9866e
 
 
 
 
 
 
 
 
 
 
 
 
aaac77b
 
6b9866e
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import numpy as np
import gradio as gr
import time


def get_random_color(rng):
    for i in range(1):
        # Only want one solid color
        color1 = rng.integers(low=0, high=255, size=(1, 1, 3))
        color2 = rng.integers(low=0, high=255, size=(1, 1, 3))
        color3 = rng.integers(low=0, high=255, size=(1, 1, 3))
        color4 = rng.integers(low=0, high=255, size=(1, 1, 3))
        color5 = rng.integers(low=0, high=255, size=(1, 1, 3))
        color6 = rng.integers(low=0, high=255, size=(1, 1, 3))
        color7 = rng.integers(low=0, high=255, size=(1, 1, 3))
        color8 = rng.integers(low=0, high=255, size=(1, 1, 3))
        color9 = rng.integers(low=0, high=255, size=(1, 1, 3))
        # Make the image resolution higher
        color1 = np.resize(color1, new_shape=(256, 256, 3))
        color2 = np.resize(color2, new_shape=(256, 256, 3))
        color3 = np.resize(color3, new_shape=(256, 256, 3))
        color4 = np.resize(color4, new_shape=(256, 256, 3))
        color5 = np.resize(color5, new_shape=(256, 256, 3))
        color6 = np.resize(color6, new_shape=(256, 256, 3))
        color7 = np.resize(color7, new_shape=(256, 256, 3))
        color8 = np.resize(color8, new_shape=(256, 256, 3))
        color9 = np.resize(color9, new_shape=(256, 256, 3))
        # Mix the color into blocks
        block1 = np.concatenate((color1, color2, color3), axis=0)
        # print(f"Shape of block: {block1.shape}")
        block2 = np.concatenate((color4, color5, color6), axis=0)
        block3 = np.concatenate((color7, color8, color9), axis=0)
        # print(f"Shape of block: {block2.shape}")
        block4 = np.concatenate((block1, block2, block3), axis=1)
        # print(f"Shape of block: {block3.shape}")
    return color1, color2, color3, \
           color4, color5, color6, \
           color7, color8, color9, \
           block4


def color_me(num1):
    # Make some random color
    rng1 = np.random.default_rng(seed=num1)
    return get_random_color(rng1)


def auto_color_me():
    # Make some random color
    # Get time to be the seed
    seed = int(time.time())
    rng1 = np.random.default_rng(seed=seed)
    return get_random_color(rng1)


# Title of the blocks
with gr.Blocks() as demo1:
    image_width = 64
    image_height = 64

    with gr.Row():
        gr.Markdown(
            """
            # Random color mix and match
            
            The program displays four random colors and combined them together.
            
            The color can be changed from the slider or text box. The same number \
            will always give the same color.
            
            The color can also be set to change automatically by the \
            auto button. To stop the program, please close the browser tab.
            
            The color can be downloaded from the download button at the top right \
            corner to the image.
            """
        )

    with gr.Row():
        num1 = gr.Slider(value=10, minimum=0, maximum=1000, step=1,
                         label="Value", info="Slide the bar or enter a value to change the color.")

    with gr.Row():
        #btn_auto = gr.Button(value="Auto change color")
        #btn_stop = gr.Button(value="Stop")
        examples = gr.Examples(examples=[[82], [333], [590]], inputs=[num1])

    with gr.Row():
        with gr.Column():
            image1 = gr.Image(height=image_height*4,
                              width=image_width*4, show_label=False)
            # image2 = gr.Image(height=image_height, width=image_width, show_label=False)
            # image3 = gr.Image(height=image_height, width=image_width, show_label=False)
    
        with gr.Row():
            color1 = gr.Image(height=image_height,
                                  width=image_width, 
                                  show_label=False,
                                  interactive=False)
            color2 = gr.Image(height=image_height,
                                  width=image_width, 
                                  show_label=False,
                                  interactive=False)
            color3 = gr.Image(height=image_height,
                                  width=image_width, 
                                  show_label=False,
                                  interactive=False)
        with gr.Row():
            color4 = gr.Image(height=image_height,
                                  width=image_width, 
                                  show_label=False,
                                  interactive=False)
            color5 = gr.Image(height=image_height,
                                  width=image_width, 
                                  show_label=False,
                                  interactive=False)
            color6 = gr.Image(height=image_height,
                                  width=image_width, 
                                  show_label=False,
                                  interactive=False)
        with gr.Row():    
            color7 = gr.Image(height=image_height,
                                  width=image_width, 
                                  show_label=False,
                                  interactive=False)
            color8 = gr.Image(height=image_height,
                                  width=image_width, 
                                  show_label=False,
                                  interactive=False)
            color9 = gr.Image(height=image_height,
                                  width=image_width, 
                                  show_label=False,
                                  interactive=False)

    # Events
    # Release of slider
    num1.change(
        fn=color_me,
        inputs=[num1],
        outputs=[color1, color2, color3, 
                 color4, color5, color6, 
                 color7, color8, color9,
                 image1]
    )
    # Clicking on auto change color
    # Input = -1 indicate autochanging of color
    # Need to figure out how to stop the event.
    # Will disable the auto change of color for now.
    """
    btn_auto.click(
        fn=auto_color_me,
        inputs=[],
        outputs=[color1, color2, color3, 
                 color4, color5, color6,
                 color7, color8, color9,
                 image1],
        every=1)
    """
    
# Main
if __name__ == "__main__":
    demo1.launch()