olivercareyncl commited on
Commit
bebf5bf
·
verified ·
1 Parent(s): ec058a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -2
app.py CHANGED
@@ -1,14 +1,40 @@
1
  import gradio as gr
2
  import random
3
  import matplotlib.pyplot as plt
 
4
 
5
  # Generate a random hex color
6
  def random_hex():
7
  return "#" + "".join(f"{random.randint(0, 255):02x}" for _ in range(3))
8
 
9
- # Generate initial 5-color palette
 
 
 
 
 
 
 
 
 
 
10
  def generate_palette(locked_colors):
11
- return [random_hex() if locked is None else locked for locked in locked_colors]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  # Create color swatches
14
  def display_palette(colors):
 
1
  import gradio as gr
2
  import random
3
  import matplotlib.pyplot as plt
4
+ import colorsys
5
 
6
  # Generate a random hex color
7
  def random_hex():
8
  return "#" + "".join(f"{random.randint(0, 255):02x}" for _ in range(3))
9
 
10
+ # Convert hex to HSL
11
+ def hex_to_hsl(hex_color):
12
+ r, g, b = int(hex_color[1:3], 16) / 255.0, int(hex_color[3:5], 16) / 255.0, int(hex_color[5:7], 16) / 255.0
13
+ return colorsys.rgb_to_hls(r, g, b)
14
+
15
+ # Convert HSL to hex
16
+ def hsl_to_hex(h, l, s):
17
+ r, g, b = colorsys.hls_to_rgb(h, l, s)
18
+ return "#" + "".join(f"{int(c * 255):02x}" for c in (r, g, b))
19
+
20
+ # Generate a harmonious palette
21
  def generate_palette(locked_colors):
22
+ locked_hsl = [hex_to_hsl(color) if color else None for color in locked_colors]
23
+ base_hue = next((h for h, _, _ in locked_hsl if h is not None), random.random())
24
+ base_lum = next((l for _, l, _ in locked_hsl if l is not None), 0.5)
25
+ base_sat = next((s for _, _, s in locked_hsl if s is not None), 0.7)
26
+
27
+ new_palette = []
28
+ for i in range(5):
29
+ if locked_colors[i]:
30
+ new_palette.append(locked_colors[i])
31
+ else:
32
+ new_hue = (base_hue + random.uniform(-0.1, 0.1)) % 1.0
33
+ new_lum = min(max(base_lum + random.uniform(-0.1, 0.1), 0.2), 0.8)
34
+ new_sat = min(max(base_sat + random.uniform(-0.1, 0.1), 0.4), 1.0)
35
+ new_palette.append(hsl_to_hex(new_hue, new_lum, new_sat))
36
+
37
+ return new_palette
38
 
39
  # Create color swatches
40
  def display_palette(colors):