Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Fix speaker color inconsistency in identification dialog (#163)

Murtaza Nasir7cc363965972
diff --git a/static/js/app.modular.js b/static/js/app.modular.js
index 1c25b435..67d9a3cd 100644
--- a/static/js/app.modular.js
+++ b/static/js/app.modular.js
@@ -19,6 +19,22 @@ import { useVirtualScroll, getVirtualItemKey } from './modules/composables/virtu
import { showToast } from './modules/utils/toast.js';
import { getContrastTextColor } from './modules/utils/colors.js';
+// Helper to get consistent speaker color based on ID
+const getSpeakerColor = (speakerId) => {
+ // Extract number from SPEAKER_XX format
+ const match = speakerId?.match(/^SPEAKER_(\d+)$/);
+ if (match) {
+ const num = parseInt(match[1], 10);
+ return `speaker-color-${(num % 8) + 1}`;
+ }
+ // Fallback: hash the string for consistent color
+ let hash = 0;
+ for (let i = 0; i < (speakerId || '').length; i++) {
+ hash = speakerId.charCodeAt(i) + ((hash << 5) - hash);
+ }
+ return `speaker-color-${(Math.abs(hash) % 8) + 1}`;
+};
+
// Wait for the DOM to be fully loaded before mounting the Vue app
document.addEventListener('DOMContentLoaded', async () => {
// Initialize i18n before creating Vue app (if not already initialized)
@@ -839,11 +855,11 @@ document.addEventListener('DOMContentLoaded', async () => {
};
}
- // Extract unique speakers
+ // Extract unique speakers and assign ID-based colors
const speakers = [...new Set(transcriptionData.map(segment => segment.speaker).filter(Boolean))];
const speakerColors = {};
- speakers.forEach((speaker, index) => {
- speakerColors[speaker] = `speaker-color-${(index % 8) + 1}`;
+ speakers.forEach((speaker) => {
+ speakerColors[speaker] = getSpeakerColor(speaker);
});
const simpleSegments = transcriptionData.map(segment => ({
diff --git a/static/js/modules/composables/speakers.js b/static/js/modules/composables/speakers.js
index 5c6c0df7..a2878de4 100644
--- a/static/js/modules/composables/speakers.js
+++ b/static/js/modules/composables/speakers.js
@@ -24,6 +24,22 @@ export function useSpeakers(state, utils, processedTranscription) {
// Current speaker highlight state
let currentSpeakerId = null;
+ // Helper to get consistent speaker color based on ID
+ const getSpeakerColor = (speakerId) => {
+ // Extract number from SPEAKER_XX format
+ const match = speakerId?.match(/^SPEAKER_(\d+)$/);
+ if (match) {
+ const num = parseInt(match[1], 10);
+ return `speaker-color-${(num % 8) + 1}`;
+ }
+ // Fallback: hash the string for consistent color
+ let hash = 0;
+ for (let i = 0; i < (speakerId || '').length; i++) {
+ hash = speakerId.charCodeAt(i) + ((hash << 5) - hash);
+ }
+ return `speaker-color-${(Math.abs(hash) % 8) + 1}`;
+ };
+
// Helper to pause outer audio player when opening modals with their own player
const pauseOuterAudioPlayer = () => {
// Find the audio player in the right panel (not in a modal)
@@ -68,12 +84,12 @@ export function useSpeakers(state, utils, processedTranscription) {
// Set modalSpeakers for the template to use
modalSpeakers.value = speakers;
- // Initialize speaker map with the same order and colors as the transcript
- speakerMap.value = speakers.reduce((acc, speaker, index) => {
+ // Initialize speaker map with ID-based colors for consistency
+ speakerMap.value = speakers.reduce((acc, speaker) => {
acc[speaker] = {
name: '',
isMe: false,
- color: `speaker-color-${(index % 8) + 1}` // Same color assignment as processedTranscription
+ color: getSpeakerColor(speaker) // ID-based color assignment
};
// Keep the original speaker ID for display
speakerDisplayMap.value[speaker] = speaker;
@@ -860,12 +876,11 @@ export function useSpeakers(state, utils, processedTranscription) {
// Add to modalSpeakers
modalSpeakers.value.push(newSpeakerId);
- // Add to speakerMap with next available color
- const colorIndex = modalSpeakers.value.length - 1;
+ // Add to speakerMap with ID-based color
speakerMap.value[newSpeakerId] = {
name: name,
isMe: newSpeakerIsMe.value,
- color: `speaker-color-${(colorIndex % 8) + 1}`
+ color: getSpeakerColor(newSpeakerId)
};
// Add to speakerDisplayMap