duongthienz commited on
Commit
163101d
·
verified ·
1 Parent(s): 84ecf63

update color function

Browse files
Files changed (1) hide show
  1. utils.py +44 -19
utils.py CHANGED
@@ -40,34 +40,59 @@ TRANSPARENT_BG = dict(
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]]])
 
 
 
 
 
 
63
  bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
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):
 
40
  # ---------------------------------------------------------------------------
41
 
42
  def colorsCSS(n, startingHue=None, pool=None):
43
+ """Creates n visually distinct CSS hex colors for up to ~30 speakers.
44
 
45
+ Strategy: generate a large pre-built palette of 30 colors using the full
46
+ 360-degree hue wheel in two interleaved saturation/brightness tiers.
47
+ Tier 1 (even indices): high saturation, medium-high brightness vivid colors.
48
+ Tier 2 (odd indices): medium saturation, high brightness — lighter pastels.
49
+ Interleaving means adjacent speakers in the list always come from different
50
+ tiers, maximizing perceptual distance even with many speakers.
51
 
52
+ The palette is built once at a fixed seed for stability, then a random
53
+ rotation is applied via startingHue so different sessions get variety
54
+ while within a session all color pools stay coordinated.
55
+
56
+ pool parameter is accepted but ignored — kept for backwards compatibility.
57
  """
58
  if n == 0:
59
  return []
60
+
61
+ PALETTE_SIZE = 30
62
+ # Build the full palette at a neutral starting point
63
+ palette = []
64
+ tiers = [
65
+ (220, 200), # tier 1: vivid — HSV saturation=220, value=200
66
+ (140, 230), # tier 2: pastel — HSV saturation=140, value=230
67
+ ]
68
+ for i in range(PALETTE_SIZE):
69
+ hue = int(i * 360 / PALETTE_SIZE) % 360
70
+ # OpenCV HSV: hue is 0-179 (half degrees), so divide by 2
71
+ h_cv = hue // 2
72
+ sat, val = tiers[i % 2]
73
+ hsv = np.uint8([[[h_cv, sat, val]]])
74
  bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
75
  b = f'{bgr[0][0][0].item():02x}'
76
  g = f'{bgr[0][0][1].item():02x}'
77
  r = f'{bgr[0][0][2].item():02x}'
78
+ palette.append('#' + b + g + r)
79
+
80
+ # Apply rotation from startingHue so sessions get color variety
81
+ offset = 0
82
+ if startingHue is not None:
83
+ offset = int(startingHue / 180 * PALETTE_SIZE) % PALETTE_SIZE
84
+
85
+ # Pick n colors evenly spaced from the rotated palette
86
+ result = []
87
+ for i in range(min(n, PALETTE_SIZE)):
88
+ idx = (offset + int(i * PALETTE_SIZE / min(n, PALETTE_SIZE))) % PALETTE_SIZE
89
+ result.append(palette[idx])
90
+
91
+ # If n > PALETTE_SIZE (very unlikely), cycle
92
+ while len(result) < n:
93
+ result.extend(palette[:n - len(result)])
94
+
95
+ return result
96
 
97
 
98
  def extract_clip_bytes(waveform, sample_rate, seg_start, seg_end):