Add foreground override contrast threshold

This adds a parameter to text_composition_strategy that specifies
a percentage difference luminance below which the foreground
color will be overridden. The foreground color is set to white if
the background is dark or black if the background is light.

Many programs output colors that look good with the author's
terminal's color scheme but which are completely illegible with
other color schemes. This allows the user ensure that there is
always sufficient contrast to read the text on the screen.

Since we want existing configs to continue working, this also
makes it so that rather than taking exactly two parameters,
text_composition_strategy takes one--three parameters, using the
default values for those not specified.
This commit is contained in:
Luna McNulty
2023-05-18 11:06:00 -04:00
parent 9a0068e318
commit 05d94e8256
5 changed files with 45 additions and 12 deletions

View File

@@ -249,7 +249,7 @@ light text on dark backgrounds thinner. It might also make some text appear like
the strokes are uneven.
You can fine tune the actual contrast curve used for glyph composition by
specifying two space separated numbers for this setting.
specifying up to three space-separated numbers for this setting.
The first number is the gamma adjustment, which controls the thickness of dark
text on light backgrounds. Increasing the value will make text appear thicker.
@@ -263,6 +263,11 @@ The second number is an additional multiplicative contrast. It is percentage
ranging from :code:`0` to :code:`100`. The default value is :code:`0` on Linux
and :code:`30` on macOS.
The third number is an override threshold. It is percentage 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. The default value is :code:`0`.
If you wish to achieve similar looking thickness in light and dark themes, a good way
to experiment is start by setting the value to :code:`1.0 0` and use a dark theme.
Then adjust the second parameter until it looks good. Then switch to a light theme

View File

@@ -183,7 +183,7 @@ static void
text_composition_strategy(PyObject *val, Options *opts) {
if (!PyUnicode_Check(val)) { PyErr_SetString(PyExc_TypeError, "text_rendering_strategy must be a string"); return; }
opts->text_old_gamma = false;
opts->text_gamma_adjustment = 1.0f; opts->text_contrast = 0.f;
opts->text_gamma_adjustment = 1.0f; opts->text_contrast = 0.f; opts->text_fg_override_threshold = 0.f;
if (PyUnicode_CompareWithASCIIString(val, "platform") == 0) {
#ifdef __APPLE__
opts->text_gamma_adjustment = 1.7f; opts->text_contrast = 30.f;
@@ -192,15 +192,29 @@ text_composition_strategy(PyObject *val, Options *opts) {
else if (PyUnicode_CompareWithASCIIString(val, "legacy") == 0) {
opts->text_old_gamma = true;
} else {
DECREF_AFTER_FUNCTION PyObject *parts = PyUnicode_Split(val, NULL, 1);
if (PyList_GET_SIZE(parts) != 2) { PyErr_SetString(PyExc_ValueError, "text_rendering_strategy must be of the form number:number"); return; }
DECREF_AFTER_FUNCTION PyObject *ga = PyFloat_FromString(PyList_GET_ITEM(parts, 0));
if (PyErr_Occurred()) return;
opts->text_gamma_adjustment = MAX(0.01f, PyFloat_AsFloat(ga));
DECREF_AFTER_FUNCTION PyObject *contrast = PyFloat_FromString(PyList_GET_ITEM(parts, 1));
if (PyErr_Occurred()) return;
opts->text_contrast = MAX(0.0f, PyFloat_AsFloat(contrast));
opts->text_contrast = MIN(100.0f, opts->text_contrast);
DECREF_AFTER_FUNCTION PyObject *parts = PyUnicode_Split(val, NULL, 2);
int size = PyList_GET_SIZE(parts);
if (size < 1 || 3 < size) { PyErr_SetString(PyExc_ValueError, "text_rendering_strategy must be of the form number:[number]:[number]"); return; }
if (size > 0) {
DECREF_AFTER_FUNCTION PyObject *ga = PyFloat_FromString(PyList_GET_ITEM(parts, 0));
if (PyErr_Occurred()) return;
opts->text_gamma_adjustment = MAX(0.01f, PyFloat_AsFloat(ga));
}
if (size > 1) {
DECREF_AFTER_FUNCTION PyObject *contrast = PyFloat_FromString(PyList_GET_ITEM(parts, 1));
if (PyErr_Occurred()) return;
opts->text_contrast = MAX(0.0f, PyFloat_AsFloat(contrast));
opts->text_contrast = MIN(100.0f, opts->text_contrast);
}
if (size > 2) {
DECREF_AFTER_FUNCTION PyObject *text_fg_override_threshold = PyFloat_FromString(PyList_GET_ITEM(parts, 2));
if (PyErr_Occurred()) return;
opts->text_fg_override_threshold = MAX(0.f, PyFloat_AsFloat(text_fg_override_threshold));
opts->text_fg_override_threshold = MIN(100.f, PyFloat_AsFloat(text_fg_override_threshold));
}
}
}