From d3fc8a2897b897d3d1f57bf8078b7d3c63fd54f6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 24 May 2023 21:23:39 +0530 Subject: [PATCH] Remove a bunch of option processing code and directly define the threshold in the shader --- kitty/cell_fragment.glsl | 2 +- kitty/options/definition.py | 2 +- kitty/options/to-c-generated.h | 15 --------------- kitty/options/to-c.h | 7 ------- kitty/shaders.c | 2 -- kitty/state.h | 1 - kitty/window.py | 4 ++-- 7 files changed, 4 insertions(+), 29 deletions(-) diff --git a/kitty/cell_fragment.glsl b/kitty/cell_fragment.glsl index 51205ee12..8cdc388cf 100644 --- a/kitty/cell_fragment.glsl +++ b/kitty/cell_fragment.glsl @@ -134,7 +134,7 @@ vec3 fg_override(float under_luminance, float over_lumininace, vec3 over) { // If the difference in luminance is too small, // force the foreground color to be black or white. float diff_luminance = abs(under_luminance - over_lumininace); - float override_level = (1.f - colored_sprite) * step(diff_luminance, text_fg_override_threshold); + float override_level = (1.f - colored_sprite) * step(diff_luminance, FG_OVERRIDE); float original_level = 1.f - override_level; return original_level * over + override_level * vec3(step(under_luminance, 0.5f)); } diff --git a/kitty/options/definition.py b/kitty/options/definition.py index ea4f8e007..e700e2695 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -269,7 +269,7 @@ 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', ctype='percent', long_text=''' +opt('text_fg_override_threshold', 0, option_type='float', long_text=''' 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 diff --git a/kitty/options/to-c-generated.h b/kitty/options/to-c-generated.h index a4f57e5c7..c1ea350a2 100644 --- a/kitty/options/to-c-generated.h +++ b/kitty/options/to-c-generated.h @@ -70,19 +70,6 @@ convert_from_opts_text_composition_strategy(PyObject *py_opts, Options *opts) { Py_DECREF(ret); } -static void -convert_from_python_text_fg_override_threshold(PyObject *val, Options *opts) { - opts->text_fg_override_threshold = percent(val); -} - -static void -convert_from_opts_text_fg_override_threshold(PyObject *py_opts, Options *opts) { - PyObject *ret = PyObject_GetAttrString(py_opts, "text_fg_override_threshold"); - if (ret == NULL) return; - convert_from_python_text_fg_override_threshold(ret, opts); - Py_DECREF(ret); -} - static void convert_from_python_cursor_shape(PyObject *val, Options *opts) { opts->cursor_shape = PyLong_AsLong(val); @@ -1083,8 +1070,6 @@ convert_opts_from_python_opts(PyObject *py_opts, Options *opts) { if (PyErr_Occurred()) return false; convert_from_opts_text_composition_strategy(py_opts, opts); if (PyErr_Occurred()) return false; - convert_from_opts_text_fg_override_threshold(py_opts, opts); - if (PyErr_Occurred()) return false; convert_from_opts_cursor_shape(py_opts, opts); if (PyErr_Occurred()) return false; convert_from_opts_cursor_beam_thickness(py_opts, opts); diff --git a/kitty/options/to-c.h b/kitty/options/to-c.h index 1f2c9f3f6..935d0e175 100644 --- a/kitty/options/to-c.h +++ b/kitty/options/to-c.h @@ -14,13 +14,6 @@ PyFloat_AsFloat(PyObject *o) { return (float)PyFloat_AsDouble(o); } -static inline float -percent(PyObject *o) { - float ans = PyFloat_AsFloat(o); - return MAX(0.f, MIN(ans, 100.f)) * 0.01f; -} - - static inline color_type color_as_int(PyObject *color) { if (!PyObject_TypeCheck(color, &Color_Type)) { PyErr_SetString(PyExc_TypeError, "Not a Color object"); return 0; } diff --git a/kitty/shaders.c b/kitty/shaders.c index c0a8772aa..1edb689c9 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -574,8 +574,6 @@ set_cell_uniforms(float current_inactive_text_alpha, bool force) { S(CELL_PROGRAM, text_contrast, text_contrast, 1f); S(CELL_FG_PROGRAM, text_contrast, text_contrast, 1f); float text_gamma_adjustment = OPT(text_gamma_adjustment) < 0.01f ? 1.0f : 1.0f / OPT(text_gamma_adjustment); S(CELL_PROGRAM, text_gamma_adjustment, text_gamma_adjustment, 1f); S(CELL_FG_PROGRAM, text_gamma_adjustment, text_gamma_adjustment, 1f); - S(CELL_PROGRAM, text_fg_override_threshold, OPT(text_fg_override_threshold), 1f); - S(CELL_FG_PROGRAM, text_fg_override_threshold, OPT(text_fg_override_threshold), 1f); #undef S #define SV(prog, name, num, val, type) { bind_program(prog); glUniform##type(glGetUniformLocation(program_id(prog), #name), num, val); } SV(CELL_PROGRAM, gamma_lut, 256, srgb_lut, 1fv); SV(CELL_FG_PROGRAM, gamma_lut, 256, srgb_lut, 1fv); diff --git a/kitty/state.h b/kitty/state.h index 7fd748d63..454aa90d0 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -49,7 +49,6 @@ typedef struct { char *bell_path, *bell_theme; float background_opacity, dim_opacity; float text_contrast, text_gamma_adjustment; - float text_fg_override_threshold; bool text_old_gamma; char *background_image, *default_window_logo; diff --git a/kitty/window.py b/kitty/window.py index 543d7004f..9de56ed4e 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -391,7 +391,7 @@ class LoadShaderPrograms: self.semi_transparent = semi_transparent opts = get_options() self.text_old_gamma = opts.text_composition_strategy == 'legacy' - self.text_fg_override_threshold = opts.text_fg_override_threshold + self.text_fg_override_threshold = max(0, min(opts.text_fg_override_threshold, 100)) * 0.01 compile_program(BLIT_PROGRAM, *load_shaders('blit'), allow_recompile) v, f = load_shaders('cell') @@ -415,7 +415,7 @@ class LoadShaderPrograms: STRIKE_SPRITE_INDEX=NUM_UNDERLINE_STYLES + 1, ) if self.text_fg_override_threshold != 0.: - ff = ff.replace('#define NO_FG_OVERRIDE', '#define FG_OVERRIDE') + ff = ff.replace('#define NO_FG_OVERRIDE', f'#define FG_OVERRIDE {self.text_fg_override_threshold}') if self.text_old_gamma: ff = ff.replace('#define TEXT_NEW_GAMMA', '#define TEXT_OLD_GAMMA') if semi_transparent: