File size: 3,074 Bytes
5e1ef3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Variation 6 color_format RGB and HSL
import csv
import os

import colorsys

def hex_to_rgb(hex_val):
    """#RRGGBB -> rgb(R,G,B)"""
    hex_val = hex_val.lstrip('#')
    r, g, b = tuple(int(hex_val[i:i+2], 16) for i in (0, 2, 4))
    return f"rgb({r}, {g}, {b})"

def hex_to_hsl(hex_val):
    """#RRGGBB -> hsl(H,S%,L%)"""
    hex_val = hex_val.lstrip('#')
    r, g, b = tuple(int(hex_val[i:i+2], 16) for i in (0, 2, 4))
    h, l, s = colorsys.rgb_to_hls(r/255.0, g/255.0, b/255.0)
    # Convert to common HSL representation: H 0–360, S/L 0–100%
    h = int(round(h*360))
    s = int(round(s*100))
    l = int(round(l*100))
    return f"hsl({h}, {s}%, {l}%)"



def merge_prompt_with_colors(prompt_file, color_file, output_file, variation=1, color_level=1):
    # read prompt
    with open(prompt_file, "r", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        prompts = list(reader)

    # read color 
    with open(color_file, "r", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        colors = list(reader)

    merged_rows = []
    id_counter = 1
    for prompt in prompts:
        prompt_text = prompt["prompt"]
        for color in colors:
            hex_val = color.get("hex") or color.get("color") or ""
            
            merged_rows.append({
                "id": id_counter,
                "prompt": prompt_text,
                "color_1": hex_to_rgb(hex_val),
                "variation": variation,
                "color_level": color_level,
                "split": "test",
                "color_format": "RGB",
            })
            id_counter += 1

    for prompt in prompts:
        prompt_text = prompt["prompt"]
        for color in colors:
            hex_val = color.get("hex") or color.get("color") or ""
            
            merged_rows.append({
                "id": id_counter,
                "prompt": prompt_text,
                "color_1": hex_to_hsl(hex_val),
                "variation": variation,
                "color_level": color_level,
                "split": "test",
                "color_format": "HSL",
            })
            id_counter += 1
    
    output_dir = os.path.dirname(output_file) 
    if output_dir and not os.path.exists(output_dir): 
        os.makedirs(output_dir, exist_ok=True)
    # write CSV
    with open(output_file, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=["id","prompt","color_1","variation","color_level","split","color_format"])
        writer.writeheader()
        writer.writerows(merged_rows)

    print(f"Generation complete: {output_file}")


merge_prompt_with_colors("raw_config/Variation_6.csv", "raw_config/Color_Level_1.csv", "metadata/Variation_6_Color_1.csv", variation=6, color_level=1)
merge_prompt_with_colors("raw_config/Variation_6.csv", "raw_config/Color_Level_2.csv", "metadata/Variation_6_Color_2.csv", variation=6, color_level=2)
merge_prompt_with_colors("raw_config/Variation_6.csv", "raw_config/Color_Level_3.csv", "metadata/Variation_6_Color_3.csv", variation=6, color_level=3)