src/hg/hgTracks/simpleTracks.c 1.129

1.129 2010/03/25 17:11:09 kent
Adding utility functions to get darker colors. Using one of these to darken text when primary color is too light.
Index: src/hg/hgTracks/simpleTracks.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/hgTracks/simpleTracks.c,v
retrieving revision 1.128
retrieving revision 1.129
diff -b -B -U 4 -r1.128 -r1.129
--- src/hg/hgTracks/simpleTracks.c	9 Mar 2010 00:18:37 -0000	1.128
+++ src/hg/hgTracks/simpleTracks.c	25 Mar 2010 17:11:09 -0000	1.129
@@ -238,8 +238,36 @@
     tl.leftLabelWidth = leftLabelWidthChars*tl.nWidth + trackTabWidth;
     }
 }
 
+static boolean isTooLightForTextOnWhite(struct hvGfx *hvg, Color color)
+/* Return TRUE if text in this color would probably be invisible on a white background. */
+{
+struct rgbColor rgbColor =  hvGfxColorIxToRgb(hvg, color);
+int colorTotal = rgbColor.r + 2*rgbColor.g + rgbColor.b;
+return colorTotal >= 512;
+}
+
+Color darkerColor(struct hvGfx *hvg, Color color)
+/* Get darker shade of a color - half way between this color and black */
+{
+struct rgbColor rgbColor =  hvGfxColorIxToRgb(hvg, color);
+rgbColor.r = ((int)rgbColor.r)/2;
+rgbColor.g = ((int)rgbColor.g)/2;
+rgbColor.b = ((int)rgbColor.b)/2;
+return hvGfxFindColorIx(hvg, rgbColor.r, rgbColor.g, rgbColor.b);
+}
+
+Color somewhatDarkerColor(struct hvGfx *hvg, Color color)
+/* Get a somewhat lighter shade of a color - 1/3 of the way towards black. */
+{
+struct rgbColor rgbColor =  hvGfxColorIxToRgb(hvg, color);
+rgbColor.r = (2*(int)rgbColor.r)/3;
+rgbColor.g = (2*(int)rgbColor.g)/3;
+rgbColor.b = (2*(int)rgbColor.b)/3;
+return hvGfxFindColorIx(hvg, rgbColor.r, rgbColor.g, rgbColor.b);
+}
+
 Color lighterColor(struct hvGfx *hvg, Color color)
 /* Get lighter shade of a color - half way between this color and white */
 {
 struct rgbColor rgbColor =  hvGfxColorIxToRgb(hvg, color);
@@ -2926,9 +2954,9 @@
 }
 
 static void genericDrawItem(struct track *tg, struct spaceNode *sn,
                             struct hvGfx *hvg, int xOff, int yOff, int width,
-                            MgFont *font, Color color, enum trackVisibility vis,
+                            MgFont *font, Color color, Color labelColor, enum trackVisibility vis,
                             double scale, boolean withLeftLabels)
 /* draw one non-overflow item */
 {
 struct slList *item = sn->val;
@@ -2942,9 +2970,14 @@
 int textX = x1;
 char *name = tg->itemName(tg, item);
 
 if(tg->itemNameColor != NULL)
+    {
     color = tg->itemNameColor(tg, item, hvg);
+    labelColor = color;
+    if (withLeftLabels && isTooLightForTextOnWhite(hvg, color))
+	labelColor = somewhatDarkerColor(hvg, color);
+    }
 int y = yOff + tg->lineHeight * sn->row;
 tg->drawItemAt(tg, item, hvg, xOff, y, scale, font, color, vis);
 /* pgSnp tracks may change flags between items */
 withLabels = (withLeftLabels && withIndividualLabels && (vis == tvPack) && !tg->drawName);
@@ -2972,9 +3005,9 @@
                         MG_WHITE, font, name);
             }
         else
             hvGfxTextRight(hvg, leftLabelX, y, leftLabelWidth-1, tg->heightPer,
-                        color, font, name);
+                        labelColor, font, name);
         hvGfxUnclip(hvg);
         hvGfxSetClip(hvg, insideX, yOff, insideWidth, tg->height);
         }
     else
@@ -2984,9 +3017,9 @@
             hvGfxBox(hvg, textX - 1, y, nameWidth+1, tg->heightPer-1, color);
             hvGfxTextRight(hvg, textX, y, nameWidth, tg->heightPer, MG_WHITE, font, name);
             }
         else
-            hvGfxTextRight(hvg, textX, y, nameWidth, tg->heightPer, color, font, name);
+            hvGfxTextRight(hvg, textX, y, nameWidth, tg->heightPer, labelColor, font, name);
         }
     }
 if (!tg->mapsSelf)
     {
@@ -3031,9 +3064,9 @@
                                 scale, overflowRow, firstOverflow);
         firstOverflow = FALSE;
         }
     else
-        genericDrawItem(tg, sn, hvg, xOff, yOff, width, font, color, vis,
+        genericDrawItem(tg, sn, hvg, xOff, yOff, width, font, color, color, vis,
                         scale, withLeftLabels);
     }
 
 hvGfxUnclip(hvg);