1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2025-01-28 01:28:30 +00:00

Premultiply background colors for X11 (#1421)

* initialize XColor/XftColor

this does not change behavior, but avoids passing uninitialized data to X/Xft functions

* gui, x11: move have_argb_visual to x11 files

this variable only has meaning with respect to X11

* x11: clear have_argb_visual if the setting changes to false

otherwise, we may try to use colors with alpha when we no longer requested a 32-bit visual

* x11: premultiply alpha of background colors

* x11: clean up set_foreground_color a bit

---------

Co-authored-by: bi4k8 <bi4k8@github>
This commit is contained in:
bi4k8 2023-02-24 13:13:48 +00:00 committed by GitHub
parent 5e98c49c4c
commit 4a7bec5ba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 26 deletions

View File

@ -61,28 +61,36 @@ public:
static Colour from_argb32(uint32_t argb); static Colour from_argb32(uint32_t argb);
#ifdef BUILD_X11 #ifdef BUILD_X11
unsigned long to_x11_color(Display *display, int screen) { unsigned long to_x11_color(Display *display, int screen,
if(display == nullptr) { bool premultiply = false) {
if (display == nullptr) {
/* cannot work if display is not open */ /* cannot work if display is not open */
return 0; return 0;
} }
XColor xcolor; XColor xcolor{};
xcolor.pixel = 0;
xcolor.red = red * 257; xcolor.red = red * 257;
xcolor.green = green * 257; xcolor.green = green * 257;
xcolor.blue = blue * 257; xcolor.blue = blue * 257;
if (XAllocColor(display, DefaultColormap(display, screen), &xcolor) == 0) { if (XAllocColor(display, DefaultColormap(display, screen), &xcolor) == 0) {
//NORM_ERR("can't allocate X color"); // NORM_ERR("can't allocate X color");
return 0; return 0;
} }
return static_cast<unsigned long>(xcolor.pixel); unsigned long pixel = static_cast<unsigned long>(xcolor.pixel) & 0xffffff;
#ifdef BUILD_ARGB
if (have_argb_visual) {
if (premultiply)
pixel = (red * alpha / 255) << 16 | (green * alpha / 255) << 8 |
(blue * alpha / 255);
pixel |= ((unsigned long)alpha << 24);
}
#endif /* BUILD_ARGB */
return pixel;
} }
#endif /* BUILD_X11 */ #endif /* BUILD_X11 */
}; };
extern Colour error_colour; extern Colour error_colour;
Colour parse_color(const std::string &colour); Colour parse_color(const std::string &colour);

View File

@ -606,14 +606,10 @@ void display_output_x11::cleanup() {
} }
void display_output_x11::set_foreground_color(Colour c) { void display_output_x11::set_foreground_color(Colour c) {
current_color = c;
#ifdef BUILD_ARGB #ifdef BUILD_ARGB
if (have_argb_visual) { if (have_argb_visual) {
current_color = c;
current_color.alpha = own_window_argb_value.get(*state); current_color.alpha = own_window_argb_value.get(*state);
} else {
#endif /* BUILD_ARGB */
current_color = c;
#ifdef BUILD_ARGB
} }
#endif /* BUILD_ARGB */ #endif /* BUILD_ARGB */
XSetForeground(display, window.gc, current_color.to_x11_color(display, screen)); XSetForeground(display, window.gc, current_color.to_x11_color(display, screen));
@ -642,8 +638,8 @@ int display_output_x11::calc_text_width(const char *s) {
void display_output_x11::draw_string_at(int x, int y, const char *s, int w) { void display_output_x11::draw_string_at(int x, int y, const char *s, int w) {
#ifdef BUILD_XFT #ifdef BUILD_XFT
if (use_xft.get(*state)) { if (use_xft.get(*state)) {
XColor c; XColor c{};
XftColor c2; XftColor c2{};
c.pixel = current_color.to_x11_color(display, screen); c.pixel = current_color.to_x11_color(display, screen);
// query color on custom colormap // query color on custom colormap

View File

@ -41,10 +41,6 @@
#include <iostream> #include <iostream>
#endif #endif
#ifdef BUILD_ARGB
bool have_argb_visual;
#endif /* BUILD_ARGB */
/* basic display attributes */ /* basic display attributes */
int display_width; int display_width;
int display_height; int display_height;

View File

@ -29,14 +29,9 @@
#include "x11.h" #include "x11.h"
#endif /* BUILD_X11 */ #endif /* BUILD_X11 */
#include "colour-settings.h"
#include "colours.h" #include "colours.h"
#include "setting.hh" #include "setting.hh"
#include "colour-settings.h"
#if defined(BUILD_ARGB) && defined(OWN_WINDOW)
/* true if use_argb_visual=true and argb visual was found*/
extern bool have_argb_visual;
#endif
#ifdef BUILD_X11 #ifdef BUILD_X11
extern Display *display; extern Display *display;

View File

@ -68,6 +68,10 @@ Display *display = nullptr;
/* Window stuff */ /* Window stuff */
struct conky_x11_window window; struct conky_x11_window window;
#ifdef BUILD_ARGB
bool have_argb_visual;
#endif /* BUILD_ARGB */
conky::simple_config_setting<std::string> display_name("display", std::string(), conky::simple_config_setting<std::string> display_name("display", std::string(),
false); false);
@ -405,7 +409,7 @@ namespace {
void do_set_background(Window win, uint8_t alpha) { void do_set_background(Window win, uint8_t alpha) {
Colour colour = background_colour.get(*state); Colour colour = background_colour.get(*state);
colour.alpha = alpha; colour.alpha = alpha;
unsigned long xcolor = colour.to_x11_color(display, screen); unsigned long xcolor = colour.to_x11_color(display, screen, true);
XSetWindowBackground(display, win, xcolor); XSetWindowBackground(display, win, xcolor);
} }
} // namespace } // namespace
@ -511,6 +515,7 @@ void x11_init_window(lua::state &l __attribute__((unused)), bool own) {
depth = CopyFromParent; depth = CopyFromParent;
visual = CopyFromParent; visual = CopyFromParent;
#ifdef BUILD_ARGB #ifdef BUILD_ARGB
have_argb_visual = false;
} }
#endif /* BUILD_ARGB */ #endif /* BUILD_ARGB */

View File

@ -37,9 +37,13 @@
#include <X11/extensions/Xdbe.h> #include <X11/extensions/Xdbe.h>
#endif #endif
#include "colours.h"
#include "setting.hh" #include "setting.hh"
#ifdef BUILD_ARGB
/* true if use_argb_visual=true and argb visual was found*/
extern bool have_argb_visual;
#endif /* BUILD_ARGB */
#define ATOM(a) XInternAtom(display, #a, False) #define ATOM(a) XInternAtom(display, #a, False)
#ifdef OWN_WINDOW #ifdef OWN_WINDOW