ibibrahim Xenova HF Staff commited on
Commit
9fe079e
·
verified ·
1 Parent(s): 27f9f7d

feat: different (random) colors per label group (#3)

Browse files

- feat: different (random) colors per label group (23a7073e74dd24194470c41520494f9d92fca6db)


Co-authored-by: Joshua <[email protected]>

Files changed (1) hide show
  1. index.html +17 -13
index.html CHANGED
@@ -41,12 +41,11 @@
41
  background-color: #4f46e5;
42
  }
43
  .overlay {
44
- background-color: rgba(255, 255, 0, 0.3);
45
- border: 2px solid #fbbf24;
46
  transition: background-color 0.2s;
47
  }
48
  .overlay:hover {
49
- background-color: rgba(255, 255, 0, 0.7);
50
  }
51
  </style>
52
  </head>
@@ -336,17 +335,17 @@
336
  doclingOutput.textContent = fullText;
337
 
338
  // Parse loc tags and create overlays
339
- const locRegex = /<loc_(\d+)>/g;
340
- const locMatches = [];
341
  let match;
342
- while ((match = locRegex.exec(fullText)) !== null) {
343
- locMatches.push(parseInt(match[1]));
 
 
344
  }
345
- const overlays = [];
346
- for (let i = 0; i < locMatches.length; i += 4) {
347
- if (i + 3 < locMatches.length) {
348
- overlays.push(locMatches.slice(i, i + 4));
349
- }
350
  }
351
  const imgRect = imagePreview.getBoundingClientRect();
352
  const containerRect = imagePreviewContainer.getBoundingClientRect();
@@ -354,13 +353,18 @@
354
  const imageOffsetTop = imgRect.top - containerRect.top;
355
  const scaleX = imgRect.width / currentImageWidth;
356
  const scaleY = imgRect.height / currentImageHeight;
357
- overlays.forEach(([leftLoc, topLoc, rightLoc, bottomLoc]) => {
 
 
358
  const left = imageOffsetLeft + (leftLoc / 500) * currentImageWidth * scaleX;
359
  const top = imageOffsetTop + (topLoc / 500) * currentImageHeight * scaleY;
360
  const width = ((rightLoc - leftLoc) / 500) * currentImageWidth * scaleX;
361
  const height = ((bottomLoc - topLoc) / 500) * currentImageHeight * scaleY;
362
  const overlay = document.createElement("div");
363
  overlay.className = "overlay";
 
 
 
364
  overlay.style.position = "absolute";
365
  overlay.style.left = left + "px";
366
  overlay.style.top = top + "px";
 
41
  background-color: #4f46e5;
42
  }
43
  .overlay {
44
+ border: 2px solid var(--overlay-color);
 
45
  transition: background-color 0.2s;
46
  }
47
  .overlay:hover {
48
+ background-color: rgba(var(--overlay-color-rgb), 0.7);
49
  }
50
  </style>
51
  </head>
 
335
  doclingOutput.textContent = fullText;
336
 
337
  // Parse loc tags and create overlays
338
+ const tagRegex = /<(\w+)><loc_(\d+)><loc_(\d+)><loc_(\d+)><loc_(\d+)>/g;
339
+ const overlays = [];
340
  let match;
341
+ while ((match = tagRegex.exec(fullText)) !== null) {
342
+ const tagType = match[1];
343
+ const locs = [parseInt(match[2]), parseInt(match[3]), parseInt(match[4]), parseInt(match[5])];
344
+ overlays.push({ tagType, locs });
345
  }
346
+ const colorMap = {};
347
+ function getRandomColor() {
348
+ return `rgb(${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)})`;
 
 
349
  }
350
  const imgRect = imagePreview.getBoundingClientRect();
351
  const containerRect = imagePreviewContainer.getBoundingClientRect();
 
353
  const imageOffsetTop = imgRect.top - containerRect.top;
354
  const scaleX = imgRect.width / currentImageWidth;
355
  const scaleY = imgRect.height / currentImageHeight;
356
+ overlays.forEach(({ tagType, locs }) => {
357
+ const color = colorMap[tagType] || (colorMap[tagType] = getRandomColor());
358
+ const [leftLoc, topLoc, rightLoc, bottomLoc] = locs;
359
  const left = imageOffsetLeft + (leftLoc / 500) * currentImageWidth * scaleX;
360
  const top = imageOffsetTop + (topLoc / 500) * currentImageHeight * scaleY;
361
  const width = ((rightLoc - leftLoc) / 500) * currentImageWidth * scaleX;
362
  const height = ((bottomLoc - topLoc) / 500) * currentImageHeight * scaleY;
363
  const overlay = document.createElement("div");
364
  overlay.className = "overlay";
365
+ overlay.style.setProperty('--overlay-color', color);
366
+ const rgbMatch = color.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/);
367
+ overlay.style.setProperty('--overlay-color-rgb', `${rgbMatch[1]},${rgbMatch[2]},${rgbMatch[3]}`);
368
  overlay.style.position = "absolute";
369
  overlay.style.left = left + "px";
370
  overlay.style.top = top + "px";