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

From: Paul Mackerras <[email-masked]>
Date: Sun, 17 May 2015 14:21:55 +1000

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.