File size: 2,143 Bytes
6ef117e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# utils/excluded_colors.py
import gradio as gr

from utils.color_utils import (
    hex_to_rgb,
)
from utils.image_utils import (
    convert_str_to_int_or_zero,
)

excluded_color_list = gr.State([(0,0,0,0),(255,255,255,0)])

def add_color(color, excluded_colors_var):
    excluded_colors = excluded_colors_var.value
    # Convert the color from hex to RGBA
    color = hex_to_rgb(color) + (255,)
    if color not in [tuple(lst) for lst in excluded_colors]:
        excluded_colors.append(color)
        excluded_color_lst = [tuple(lst) for lst in excluded_colors]
    else:
        excluded_color_lst = [tuple(lst) for lst in excluded_colors]
    return excluded_color_lst, excluded_color_lst

def delete_color(row, excluded_colors_var):
    global excluded_color_list
    excluded_colors = list(excluded_colors_var)
    row_index = convert_str_to_int_or_zero(row)
    print(f"Delete Excluded Color {row_index} of {len(excluded_colors) - 1}")
    if row_index <= len(excluded_colors) - 1:        
        del excluded_colors[row_index]
        excluded_color_lst = [tuple(lst) for lst in excluded_colors]
        excluded_color_list = excluded_color_lst
        return excluded_color_lst
    else:
        excluded_color_lst = [tuple(lst) for lst in excluded_color_list]
        print(f"Row index {row_index} not found in the list:{excluded_color_lst}")
        excluded_color_list = excluded_color_lst
        return excluded_color_lst

def build_dataframe(excluded_colors_var):
    excluded_colors = [tuple(lst) for lst in excluded_colors_var.value]
    #print(f"input: {excluded_colors}")
    return excluded_colors

def on_input(excluded_colors):
    print(f"input: {excluded_colors}")
    excluded_color_lst = [tuple(lst) for lst in excluded_colors]
    print(f"output: {excluded_color_lst}")
    return excluded_color_lst, excluded_color_lst

# Event listener for when the user selects a row
def on_color_display_select(selected_rows, event: gr.SelectData):
    # Get the selected row
    selected_index = event.index[0]
    print(f"Selected row index:{selected_rows[selected_index]}, index: {selected_index}")
    return selected_index