904babadd41551f2b2be6e44159fd53665e3e0c9
jcasper
  Wed Mar 15 13:38:50 2023 -0700
Fixing issues with highlights in image/PDF, particularly with multi-region.  refs #30833

diff --git src/hg/hgTracks/hgTracks.c src/hg/hgTracks/hgTracks.c
index 43b201f..03335ba 100644
--- src/hg/hgTracks/hgTracks.c
+++ src/hg/hgTracks/hgTracks.c
@@ -2251,63 +2251,62 @@
 struct highlightVar *hlList = parseHighlightInfo();
 
 if(hlList && theImgBox == NULL) // Only highlight region when imgBox is not used. (pdf and show-image)
     {
     struct highlightVar *h;
     for (h=hlList; h; h=h->next)
         {
         if (virtualSingleChrom()) // DISGUISE VMODE
             {
             if ((h->db && sameString(h->db, database))
             &&  (h->chrom && sameString(h->chrom,chromName)))
                 {
                 char position[1024];
                 safef(position, sizeof position, "%s:%ld-%ld", h->chrom, h->chromStart, h->chromEnd);
                 char *newPosition = undisguisePosition(position); // UN-DISGUISE VMODE
-                if (startsWith(MULTI_REGION_CHROM, newPosition))
-                   newPosition = replaceChars(position, OLD_MULTI_REGION_CHROM, MULTI_REGION_CHROM);
+                if (startsWith(OLD_MULTI_REGION_CHROM, newPosition))
+                   newPosition = replaceChars(newPosition, OLD_MULTI_REGION_CHROM, MULTI_REGION_CHROM);
                 if (startsWith(MULTI_REGION_CHROM, newPosition))
                     {
                     parseVPosition(newPosition, &h->chrom, &h->chromStart, &h->chromEnd);
                     }
                 }
             }
 
         if ((h->db && sameString(h->db, database))
         &&  (h->chrom && sameString(h->chrom,virtChromName))
         &&  (h->chromEnd != 0)
         &&  (h->chromStart <= virtWinEnd && h->chromEnd >= virtWinStart))
             {
-
             h->chromStart = max(h->chromStart, virtWinStart);
             h->chromEnd = min(h->chromEnd, virtWinEnd);
             double pixelsPerBase = (double)fullInsideWidth/(virtWinEnd - virtWinStart);
             int startPixels = pixelsPerBase * (h->chromStart - virtWinStart); // floor
             if (startPixels < 0)
                 startPixels *= -1;  // reverse complement
             int width = pixelsPerBase * (double)(h->chromEnd - h->chromStart) + 0.5; // round up
             if (width < 2)
                 width = 2;
 
             // Default color to light blue, but if setting has color, use it.
-            unsigned int hexColor = MAKECOLOR_32(170, 255, 255);
+            // 179 for alpha because javascript uses 0.7 opacity, *255 ~= 179
+            unsigned int hexColor = MAKECOLOR_32_A(170, 255, 255,179);
             if (h->hexColor)
                 {
                 long rgb = strtol(h->hexColor,NULL,16); // Big and little Endians
-                hexColor = MAKECOLOR_32( ((rgb>>16)&0xff), ((rgb>>8)&0xff), (rgb&0xff) );
+                hexColor = MAKECOLOR_32_A( ((rgb>>16)&0xff), ((rgb>>8)&0xff), (rgb&0xff), 179 );
                 }
-
             hvGfxBox(hvg, fullInsideX + startPixels, 0, width, imagePixelHeight, hexColor);
             }
         }
     }
 }
 
 struct hash *makeGlobalTrackHash(struct track *trackList)
 /* Create a global track hash and returns a pointer to it. */
 {
 trackHash = newHash(8);
 rAddToTrackHash(trackHash, trackList);
 return trackHash;
 }
 
 //void domAddMenu(char *afterMenuId, char *newMenuId, char *label)
@@ -5160,33 +5159,30 @@
 
         // Also add the side image
         theSideImg = imgBoxImageAdd(theImgBox,pngTnSide.forHtml,NULL,pixWidth, pixHeight,FALSE);
         hvgSide->rc = revCmplDisp;
         initColors(hvgSide);
         }
     }
 maybeNewFonts(hvg);
 maybeNewFonts(hvgSide);
 hvg->rc = revCmplDisp;
 initColors(hvg);
 
 /* Start up client side map. */
 hPrintf("<MAP id='map' Name=%s>\n", mapName);
 
-if (theImgBox == NULL)  // imageV2 highlighting is done by javascript. This does pdf and view-image highlight
-    drawHighlights(cart, hvg, imagePixelHeight);
-
 for (window=windows; window; window=window->next)
     {
     /* Find colors to draw in. */
     findTrackColors(hvg, window->trackList);
     }
 
 
 // Good to go ahead and add all imgTracks regardless of buttons, left label, centerLabel, etc.
 if (theImgBox)
     {
     if (rulerMode != tvHide)
         {
         curImgTrack = imgBoxTrackFindOrAdd(theImgBox,NULL,RULER_TRACK_NAME,rulerMode,FALSE,
                                            IMG_FIXEDPOS); // No tdb, no centerLbl, not reorderable
         }
@@ -5491,30 +5487,32 @@
 	    }
 	else
 	    {
 	    int x;
 	    Color lightBlue = hvGfxFindRgb(bgImg, &guidelineColor);
 	    for (x = fullInsideX+guidelineSpacing-1; x<pixWidth; x += guidelineSpacing)
 		hvGfxBox(bgImg, x, 0, 1, pixHeight, lightBlue);
 	    }
 
         hvGfxUnclip(bgImg);
         if (bgImg != hvg)
             hvGfxClose(&bgImg);
         }
     }
 
+if (theImgBox == NULL)  // imageV2 highlighting is done by javascript. This does pdf and view-image highlight
+    drawHighlights(cart, hvg, imagePixelHeight);
 
 /* Draw ruler */
 
 if (rulerMode != tvHide)
     {
     newWinWidth = calcNewWinWidth(cart,virtWinStart,virtWinEnd,fullInsideWidth);
 
     if (theImgBox)
 	{
 	// data slice for ruler
 	sliceHeight      = basePositionHeight + (rulerCds ? rulerTranslationHeight : 0) + 1;
 	sliceOffsetY     = 0;
 	curImgTrack = imgBoxTrackFind(theImgBox,NULL,RULER_TRACK_NAME);
 	curSlice    = imgTrackSliceUpdateOrAdd(curImgTrack,stData,theOneImg,rulerTtl,
 					       sliceWidth[stData],sliceHeight,