module patch.invert;

import st;
import x;
import config;
import patches;
import xft_types : XRenderColor, XftColor;
import deimos.X11.Xlib : Display, Visual;
import deimos.X11.X : Colormap;

static if (isPatchEnabled!"INVERT_PATCH") {
    __gshared int invertcolors = 0;

    extern(C) void invert(const(Arg)* dummy) {
        invertcolors = !invertcolors;
        redraw();
    }

    XftColor invertedcolor(XftColor* clr) {
        XRenderColor rc;
        XftColor inverted;
        rc.red = ~clr.color.red;
        rc.green = ~clr.color.green;
        rc.blue = ~clr.color.blue;
        rc.alpha = clr.color.alpha;
        
        // Import external function defined in x.d
        extern(C) int XftColorAllocValue(Display* dpy, Visual* visual, Colormap cmap, 
                                        const(XRenderColor)* color, XftColor* result);
        
        XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &rc, &inverted);
        return inverted;
    }
}