Spaces:
Sleeping
Sleeping
update color function
Browse files
utils.py
CHANGED
|
@@ -40,34 +40,59 @@ TRANSPARENT_BG = dict(
|
|
| 40 |
# ---------------------------------------------------------------------------
|
| 41 |
|
| 42 |
def colorsCSS(n, startingHue=None, pool=None):
|
| 43 |
-
"""Creates n
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
"""
|
| 53 |
if n == 0:
|
| 54 |
return []
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|