duongthienz commited on
Commit
efa4117
·
verified ·
1 Parent(s): 0080630

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +13 -6
utils.py CHANGED
@@ -39,19 +39,24 @@ TRANSPARENT_BG = dict(
39
  # Speaker sample helpers
40
  # ---------------------------------------------------------------------------
41
 
42
- def colorsCSS(n, startingHue=None):
43
  """Creates n distinctive CSS hex colors evenly spaced across the hue wheel.
44
 
45
  Drop-in replacement for su.colorsCSS that accepts an optional startingHue
46
  so related color pools (catTypeColors, speakerColors, catColors) can share
47
  the same starting point and stay visually consistent across rerenders.
 
 
 
 
48
  """
49
  if n == 0:
50
  return []
51
- ret = []
52
  h = startingHue if startingHue is not None else int(random.random() * 180)
53
- step = 180 / n
54
- for _ in range(n):
 
 
55
  h += step
56
  h = int(h) % 180
57
  hsv = np.uint8([[[h, 200, 200]]])
@@ -59,8 +64,10 @@ def colorsCSS(n, startingHue=None):
59
  b = f'{bgr[0][0][0].item():02x}'
60
  g = f'{bgr[0][0][1].item():02x}'
61
  r = f'{bgr[0][0][2].item():02x}'
62
- ret.append('#' + b + g + r)
63
- return ret
 
 
64
 
65
 
66
  def extract_clip_bytes(waveform, sample_rate, seg_start, seg_end):
 
39
  # Speaker sample helpers
40
  # ---------------------------------------------------------------------------
41
 
42
+ def colorsCSS(n, startingHue=None, pool=None):
43
  """Creates n distinctive CSS hex colors evenly spaced across the hue wheel.
44
 
45
  Drop-in replacement for su.colorsCSS that accepts an optional startingHue
46
  so related color pools (catTypeColors, speakerColors, catColors) can share
47
  the same starting point and stay visually consistent across rerenders.
48
+
49
+ pool: if set, generate this many evenly-spaced colors first, then pick n
50
+ from them. This constrains the color space so colors are never too similar
51
+ even when n is small. Recommended value: 24.
52
  """
53
  if n == 0:
54
  return []
 
55
  h = startingHue if startingHue is not None else int(random.random() * 180)
56
+ num = pool if (pool and pool >= n) else n
57
+ step = 180 / num
58
+ all_colors = []
59
+ for _ in range(num):
60
  h += step
61
  h = int(h) % 180
62
  hsv = np.uint8([[[h, 200, 200]]])
 
64
  b = f'{bgr[0][0][0].item():02x}'
65
  g = f'{bgr[0][0][1].item():02x}'
66
  r = f'{bgr[0][0][2].item():02x}'
67
+ all_colors.append('#' + b + g + r)
68
+ # Pick n evenly-spaced colors from the pool
69
+ indices = [int(i * num / n) for i in range(n)]
70
+ return [all_colors[i] for i in indices]
71
 
72
 
73
  def extract_clip_bytes(waveform, sample_rate, seg_start, seg_end):