Skip to content

Commit 09ac8ea

Browse files
committed
Add experimental transparency background for subpixel rendering
1 parent 8d14447 commit 09ac8ea

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

kitty/cell_fragment.glsl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,37 @@ vec4 alpha_blend_premul(vec3 over, float over_alpha, vec3 under, float under_alp
5353
float alpha = mix(under_alpha, 1.0f, over_alpha);
5454
return vec4(premul_blend(over, over_alpha, under), alpha);
5555
}
56+
57+
float max3(vec3 v) {
58+
return max(max(v.r, v.g), v.b);
59+
}
60+
61+
float rgb_to_grayscale(vec3 color) {
62+
return dot(vec3(0.3, 0.59, 0.11), color);
63+
}
5664
// }}}
5765

5866
#ifdef NEEDS_FOREGROUND
5967
vec4 calculate_foreground() {
6068
vec4 text_fg = texture(sprites, sprite_pos);
6169
#ifdef SUBPIXEL
6270
vec3 unblended_fg = mix(foreground, text_fg.rgb, colored_sprite);
71+
#ifdef TRANSPARENT
72+
// According to https://stackoverflow.com/questions/33507617/blending-text-rendered-by-freetype-in-color-and-alpha
73+
// it's impossible to precisely blend it if we use RGBA. Hence, the following hack is used.
74+
float alpha = rgb_to_grayscale(text_fg.rgb); // Grayscale looks much nicer than max3
75+
vec3 scaled_mask = mix(vec3(1.0), text_fg.rgb / alpha, bvec3(alpha > 0)); // TODO: May get not normalized values?
76+
vec3 blended_fg = mix(background * bg_alpha * bg_alpha, foreground, scaled_mask); // TODO: Check whether we should multiply by bg_alpha
77+
float text_alpha = mix(text_fg.a, alpha, subpixel);
78+
#else
6379
vec3 blended_fg = mix(background, foreground, text_fg.rgb);
80+
float text_alpha = text_fg.a;
81+
#endif
6482
vec3 fg = mix(unblended_fg, blended_fg, subpixel);
6583
#else
6684
vec3 fg = mix(foreground, text_fg.rgb, colored_sprite);
67-
#endif
6885
float text_alpha = text_fg.a;
86+
#endif
6987
float underline_alpha = texture(sprites, underline_pos).a;
7088
float strike_alpha = texture(sprites, strike_pos).a;
7189
float cursor_alpha = texture(sprites, cursor_pos).a;

0 commit comments

Comments
 (0)