All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] gitk: Improve readability of highlighted text
@ 2015-03-26  5:48 Mark Ventimiglia
  2015-05-17  4:21 ` Paul Mackerras
  0 siblings, 1 reply; 2+ messages in thread
From: Mark Ventimiglia @ 2015-03-26  5:48 UTC (permalink / raw)
  To: git; +Cc: paulus, Mark Ventimiglia

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(-)

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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] gitk: Improve readability of highlighted text
  2015-03-26  5:48 [PATCH] gitk: Improve readability of highlighted text Mark Ventimiglia
@ 2015-05-17  4:21 ` Paul Mackerras
  0 siblings, 0 replies; 2+ messages in thread
From: Paul Mackerras @ 2015-05-17  4:21 UTC (permalink / raw)
  To: Mark Ventimiglia; +Cc: git

On Thu, Mar 26, 2015 at 12:48:45AM -0500, Mark Ventimiglia wrote:
> 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

> +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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-05-17  4:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-26  5:48 [PATCH] gitk: Improve readability of highlighted text Mark Ventimiglia
2015-05-17  4:21 ` Paul Mackerras

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.