threads / patch / 38913

patchgitk: Improve readability of highlighted text

Subject: [PATCH] gitk: Improve readability of highlighted text

## tl;dr

2 messages between Mar 26, 2015 and May 17, 2015. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Mark Ventimiglia· Mar 26, 2015, 05:48 UTC · lore

Selected text is indicated by displaying a rectangle under the selected text. When the default background color of dark blue is used for the selection rectangle, it does not have sufficient contrast with the default text color of black, and the highlighted text cannot be read easily.

To fix this, determine the HSV value of the selection background color.
Then, set the color of the selected text based on that value -- white if
the value is less than 0.6, black otherwise. This ensures that there is
sufficent contrast to make the text readable over the background color.
Also tag all selected text with secseltext, so that it can be reverted to
the default color on a change of selection.
---
 gitk | 44 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 4 deletions(-)
Show changes to gitk +40 −4
diff --git a/gitk b/gitk
index 9a2daf3..0a5423a 100755
--- a/gitk
+++ b/gitk
@@ -2464,7 +2464,8 @@ proc makewindow {} {
     pack .bright.sb -side right -fill y
     pack $cflist -side left -fill both -expand 1
     $cflist tag configure highlight \
-	-background [$cflist cget -selectbackground]
+	-background [$cflist cget -selectbackground] \
+	-foreground [getseltextcolor [$cflist cget -selectbackground]]
     $cflist tag configure bold -font mainfontbold
 
     .pwbottom add .bright
@@ -7188,21 +7189,44 @@ proc dispnexttag {} {
 }
 
 proc make_secsel {id} {
-    global linehtag linentag linedtag canv canv2 canv3
+    global linehtag linentag linedtag canv canv2 canv3 fgcolor
 
     if {![info exists linehtag($id)]} return
-    $canv delete secsel
+
+    allcanvs delete secsel
+
+    set textfill [getseltextcolor [$canv cget -selectbackground]]
+    foreach t [$canv find withtag secseltext] {
+	$canv itemconf $t -fill $fgcolor
+	$canv dtag $t secseltext
+    }
     set t [eval $canv create rect [$canv bbox $linehtag($id)] -outline {{}} \
 	       -tags secsel -fill [$canv cget -selectbackground]]
     $canv lower $t
+    $canv itemconf $linehtag($id) -fill $textfill
+    $canv addtag secseltext withtag $linehtag($id)
+
+    foreach t [$canv2 find withtag secseltext] {
+	$canv2 itemconf $t -fill $fgcolor
+	$canv2 dtag $t secseltext
+    }
     $canv2 delete secsel
     set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] -outline {{}} \
 	       -tags secsel -fill [$canv2 cget -selectbackground]]
     $canv2 lower $t
+    $canv2 itemconf $linentag($id) -fill $textfill
+    $canv2 addtag secseltext withtag $linentag($id)
+
+    foreach t [$canv3 find withtag secseltext] {
+	$canv3 itemconf $t -fill $fgcolor
+	$canv3 dtag $t secseltext
+    }
     $canv3 delete secsel
     set t [eval $canv3 create rect [$canv3 bbox $linedtag($id)] -outline {{}} \
 	       -tags secsel -fill [$canv3 cget -selectbackground]]
     $canv3 lower $t
+    $canv3 itemconf $linedtag($id) -fill $textfill
+    $canv3 addtag secseltext withtag $linedtag($id)
 }
 
 proc make_idmark {id} {
@@ -11529,14 +11553,26 @@ proc choosecolor {v vi w x cmd} {
     eval $cmd $c
 }
 
+proc getseltextcolor {c} {
+    # Get the largest RGB value -- this is the V in HSV
+    set value [lindex [lsort -integer [winfo rgb . $c]] end]
+
+    # If the normalized value is darker than 0.6 use white text,
+    # otherwise use black text
+    return [expr ($value < (65535 * .6))?"white":"black"]
+}
+
 proc setselbg {c} {
     global bglist cflist
     foreach w $bglist {
 	$w configure -selectbackground $c
     }
+    set textfill [getseltextcolor $c]
     $cflist tag configure highlight \
-	-background [$cflist cget -selectbackground]
+	-background [$cflist cget -selectbackground] \
+	-foreground $textfill
     allcanvs itemconf secsel -fill $c
+    allcanvs itemconf secseltext -fill $textfill
 }
 
 # This sets the background color and the color scheme for the whole UI.
-- 
1.9.0.msysgit.0
Paul Mackerras· May 17, 2015, 04:21 UTC · re: Mark Ventimiglia · lore

Re: [PATCH] gitk: Improve readability of highlighted text

On Thu, Mar 26, 2015 at 12:48:45AM -0500, Mark Ventimiglia wrote:
Show 11 quoted lines
> Selected text is indicated by displaying a rectangle under the selected
> text. When the default background color of dark blue is used for the
> selection rectangle, it does not have sufficient contrast with the default
> text color of black, and the highlighted text cannot be read easily.
> 
> To fix this, determine the HSV value of the selection background color.
> Then, set the color of the selected text based on that value -- white if
> the value is less than 0.6, black otherwise. This ensures that there is
> sufficent contrast to make the text readable over the background color.
> Also tag all selected text with secseltext, so that it can be reverted to
> the default color on a change of selection.
Finally got back to looking at this again...
> +    foreach t [$canv find withtag secseltext] {
> +	$canv itemconf $t -fill $fgcolor
> +	$canv dtag $t secseltext
> +    }
Why not just:
	$canv itemconf secseltext -fill $fgcolor
	$canv dtag secseltext
Show 7 quoted lines
> +proc getseltextcolor {c} {
> +    # Get the largest RGB value -- this is the V in HSV
> +    set value [lindex [lsort -integer [winfo rgb . $c]] end]
> +
> +    # If the normalized value is darker than 0.6 use white text,
> +    # otherwise use black text
> +    return [expr ($value < (65535 * .6))?"white":"black"]

The HSV value doesn't correlate very well with perceived lightness, and this will use black text when the background is R=0 G=0 B=153, which is very hard to read. It would probably be better to use a weighted sum of R, G and B, maybe something like 0.3R + 0.6G + 0.1B.

Paul.

← back to recent threads