config: number with unit for min contrast ratio

This commit is contained in:
arne314
2025-03-14 14:05:34 +01:00
parent a6fcdf1964
commit 40ef6b4f37
5 changed files with 52 additions and 20 deletions

View File

@@ -265,21 +265,21 @@ Then adjust the second parameter until it looks good. Then switch to a light the
and adjust the first parameter until the perceived thickness matches the dark theme.
''')
opt('text_fg_override_threshold', 0, option_type='float', long_text='''
A setting to prevent low contrast scenarios, configurable in two different modes (positive and negative value).
opt('text_fg_override_threshold', '0 %', option_type='number_with_unit', long_text='''
A setting to prevent low contrast scenarios, configurable in two different modes (suffix :code:` %` and suffix :code:` ratio`).
The default value is :code:`0`, which means no overriding is performed. Useful when working with applications
that use colors that do not contrast well with your preferred color scheme.
A positive value represents the minimum accepted difference in luminance between the foreground and background
A value with the suffix :code:` %` represents the minimum accepted difference in luminance between the foreground and background
color, below which kitty will override the foreground color. It is percentage
ranging from :code:`0` to :code:`100`. If the difference in luminance of the
ranging from :code:`0 %` to :code:`100 %`. If the difference in luminance of the
foreground and background is below this threshold, the foreground color will be set
to white if the background is dark or black if the background is light.
A negative value represents the minimum accepted contrast ratio between the foreground and background color.
Possible values range from :code:`-0.0` to :code:`-21.0`.
A value with the suffix :code:` ratio` represents the minimum accepted contrast ratio between the foreground and background color.
Possible values range from :code:`0.0 ratio` to :code:`21.0 ratio`.
To for example meet :link:`WCAG level AA <https://en.wikipedia.org/wiki/Web_Content_Accessibility_Guidelines>`
a value of :code:`-4.5` can be provided.
a value of :code:`4.5 ratio` can be provided.
The algorithm is implemented using :link:`HSLuv <https://www.hsluv.org/>` which enables it to change
the perceived lightness of a color just as much as needed without really changing its hue and saturation.

17
kitty/options/parse.py generated
View File

@@ -4,8 +4,16 @@
import typing
import collections.abc # noqa: F401, RUF100
from kitty.conf.utils import (
merge_dicts, positive_float, positive_int, python_string, to_bool, to_cmdline, to_color,
to_color_or_none, unit_float
merge_dicts,
positive_float,
positive_int,
python_string,
number_with_unit,
to_bool,
to_cmdline,
to_color,
to_color_or_none,
unit_float,
)
from kitty.options.utils import (
action_alias, active_tab_title_template, allow_hyperlinks, bell_on_tab, box_drawing_scale,
@@ -1324,7 +1332,10 @@ class Parser:
ans['text_composition_strategy'] = str(val)
def text_fg_override_threshold(self, val: str, ans: dict[str, typing.Any]) -> None:
ans['text_fg_override_threshold'] = float(val)
value, unit = number_with_unit(val, default_unit='%')
if unit not in ['%', 'ratio']:
raise ValueError(f'The unit {unit} is not a valid choice for text_fg_override_threshold')
ans['text_fg_override_threshold'] = value, unit
def touch_scroll_multiplier(self, val: str, ans: dict[str, typing.Any]) -> None:
ans['touch_scroll_multiplier'] = float(val)

View File

@@ -612,7 +612,7 @@ class Options:
term: str = 'xterm-kitty'
terminfo_type: choices_for_terminfo_type = 'path'
text_composition_strategy: str = 'platform'
text_fg_override_threshold: float = 0.0
text_fg_override_threshold: tuple[float, str] = 0.0, '%'
touch_scroll_multiplier: float = 1.0
transparent_background_colors: tuple[tuple[kitty.fast_data_types.Color, float], ...] = ()
undercurl_style: choices_for_undercurl_style = 'thin-sparse'