{"thread":{"id":"38913","subject":"[PATCH] gitk: Improve readability of highlighted text","startedAt":"2015-03-26T05:48:45Z","lastAt":"2015-05-17T04:21:55Z","messageCount":2,"participants":["Mark Ventimiglia","Paul Mackerras"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"258535","messageId":"BLU436-SMTP165FCB03C8F3B79226DB3F093080@phx.gbl","threadId":"38913","inReplyTo":null,"subject":"[PATCH] gitk: Improve readability of highlighted text","fromName":"Mark Ventimiglia","fromEmail":"mark.ventimiglia@hotmail.com","sentAt":"2015-03-26T05:48:45Z","receivedAt":"2015-03-26T05:48:45Z","isPatch":true,"sender":{"key":"mark.ventimiglia@hotmail.com","avatar":null},"body":"Selected text is indicated by displaying a rectangle under the selected\ntext. When the default background color of dark blue is used for the\nselection rectangle, it does not have sufficient contrast with the default\ntext color of black, and the highlighted text cannot be read easily.\n\nTo fix this, determine the HSV value of the selection background color.\nThen, set the color of the selected text based on that value -- white if\nthe value is less than 0.6, black otherwise. This ensures that there is\nsufficent contrast to make the text readable over the background color.\nAlso tag all selected text with secseltext, so that it can be reverted to\nthe default color on a change of selection.\n---\n gitk | 44 ++++++++++++++++++++++++++++++++++++++++----\n 1 file changed, 40 insertions(+), 4 deletions(-)\n\ndiff --git a/gitk b/gitk\nindex 9a2daf3..0a5423a 100755\n--- a/gitk\n+++ b/gitk\n@@ -2464,7 +2464,8 @@ proc makewindow {} {\n     pack .bright.sb -side right -fill y\n     pack $cflist -side left -fill both -expand 1\n     $cflist tag configure highlight \\\n-\t-background [$cflist cget -selectbackground]\n+\t-background [$cflist cget -selectbackground] \\\n+\t-foreground [getseltextcolor [$cflist cget -selectbackground]]\n     $cflist tag configure bold -font mainfontbold\n \n     .pwbottom add .bright\n@@ -7188,21 +7189,44 @@ proc dispnexttag {} {\n }\n \n proc make_secsel {id} {\n-    global linehtag linentag linedtag canv canv2 canv3\n+    global linehtag linentag linedtag canv canv2 canv3 fgcolor\n \n     if {![info exists linehtag($id)]} return\n-    $canv delete secsel\n+\n+    allcanvs delete secsel\n+\n+    set textfill [getseltextcolor [$canv cget -selectbackground]]\n+    foreach t [$canv find withtag secseltext] {\n+\t$canv itemconf $t -fill $fgcolor\n+\t$canv dtag $t secseltext\n+    }\n     set t [eval $canv create rect [$canv bbox $linehtag($id)] -outline {{}} \\\n \t       -tags secsel -fill [$canv cget -selectbackground]]\n     $canv lower $t\n+    $canv itemconf $linehtag($id) -fill $textfill\n+    $canv addtag secseltext withtag $linehtag($id)\n+\n+    foreach t [$canv2 find withtag secseltext] {\n+\t$canv2 itemconf $t -fill $fgcolor\n+\t$canv2 dtag $t secseltext\n+    }\n     $canv2 delete secsel\n     set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] -outline {{}} \\\n \t       -tags secsel -fill [$canv2 cget -selectbackground]]\n     $canv2 lower $t\n+    $canv2 itemconf $linentag($id) -fill $textfill\n+    $canv2 addtag secseltext withtag $linentag($id)\n+\n+    foreach t [$canv3 find withtag secseltext] {\n+\t$canv3 itemconf $t -fill $fgcolor\n+\t$canv3 dtag $t secseltext\n+    }\n     $canv3 delete secsel\n     set t [eval $canv3 create rect [$canv3 bbox $linedtag($id)] -outline {{}} \\\n \t       -tags secsel -fill [$canv3 cget -selectbackground]]\n     $canv3 lower $t\n+    $canv3 itemconf $linedtag($id) -fill $textfill\n+    $canv3 addtag secseltext withtag $linedtag($id)\n }\n \n proc make_idmark {id} {\n@@ -11529,14 +11553,26 @@ proc choosecolor {v vi w x cmd} {\n     eval $cmd $c\n }\n \n+proc getseltextcolor {c} {\n+    # Get the largest RGB value -- this is the V in HSV\n+    set value [lindex [lsort -integer [winfo rgb . $c]] end]\n+\n+    # If the normalized value is darker than 0.6 use white text,\n+    # otherwise use black text\n+    return [expr ($value < (65535 * .6))?\"white\":\"black\"]\n+}\n+\n proc setselbg {c} {\n     global bglist cflist\n     foreach w $bglist {\n \t$w configure -selectbackground $c\n     }\n+    set textfill [getseltextcolor $c]\n     $cflist tag configure highlight \\\n-\t-background [$cflist cget -selectbackground]\n+\t-background [$cflist cget -selectbackground] \\\n+\t-foreground $textfill\n     allcanvs itemconf secsel -fill $c\n+    allcanvs itemconf secseltext -fill $textfill\n }\n \n # This sets the background color and the color scheme for the whole UI.\n-- \n1.9.0.msysgit.0\n"},{"id":"261402","messageId":"20150517042155.GD21039@iris.ozlabs.ibm.com","threadId":"38913","inReplyTo":"BLU436-SMTP165FCB03C8F3B79226DB3F093080@phx.gbl","subject":"Re: [PATCH] gitk: Improve readability of highlighted text","fromName":"Paul Mackerras","fromEmail":"paulus@samba.org","sentAt":"2015-05-17T04:21:55Z","receivedAt":"2015-05-17T04:21:55Z","isPatch":true,"sender":{"key":"paulus@samba.org","avatar":"https://avatars.githubusercontent.com/u/1606439?v=4"},"body":"On Thu, Mar 26, 2015 at 12:48:45AM -0500, Mark Ventimiglia wrote:\n> Selected text is indicated by displaying a rectangle under the selected\n> text. When the default background color of dark blue is used for the\n> selection rectangle, it does not have sufficient contrast with the default\n> text color of black, and the highlighted text cannot be read easily.\n> \n> To fix this, determine the HSV value of the selection background color.\n> Then, set the color of the selected text based on that value -- white if\n> the value is less than 0.6, black otherwise. This ensures that there is\n> sufficent contrast to make the text readable over the background color.\n> Also tag all selected text with secseltext, so that it can be reverted to\n> the default color on a change of selection.\n\nFinally got back to looking at this again...\n\n> +    foreach t [$canv find withtag secseltext] {\n> +\t$canv itemconf $t -fill $fgcolor\n> +\t$canv dtag $t secseltext\n> +    }\n\nWhy not just:\n\n\t$canv itemconf secseltext -fill $fgcolor\n\t$canv dtag secseltext\n\n> +proc getseltextcolor {c} {\n> +    # Get the largest RGB value -- this is the V in HSV\n> +    set value [lindex [lsort -integer [winfo rgb . $c]] end]\n> +\n> +    # If the normalized value is darker than 0.6 use white text,\n> +    # otherwise use black text\n> +    return [expr ($value < (65535 * .6))?\"white\":\"black\"]\n\nThe HSV value doesn't correlate very well with perceived lightness,\nand this will use black text when the background is R=0 G=0 B=153,\nwhich is very hard to read.  It would probably be better to use\na weighted sum of R, G and B, maybe something like 0.3R + 0.6G + 0.1B.\n\nPaul.\n"}]}