Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement color picking #12

Merged
merged 30 commits into from Nov 29, 2023
Merged

Implement color picking #12

merged 30 commits into from Nov 29, 2023

Conversation

ghost
Copy link

@ghost ghost commented Nov 24, 2023

This would fix all the Flatpak color pickers (such as Eyedropper) as well as any Flatpak app that uses a color picker widget for any purpose (such as Gradience). Having these not work for no apparent reason is not great considering that Mint allows the user to install Flatpaks out of the box.

I noticed the lack of progress here, so I thought I'd try to implement this myself.

Fixes #4.

@ghost
Copy link
Author

ghost commented Nov 24, 2023

Looks like this is as far as I can get on my own. Here is a generic color picker for use with this patch:

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <stdio.h>


int main()
{
    Display* display = XOpenDisplay(NULL);
    Window window = DefaultRootWindow(display);
    Cursor cursor = XCreateFontCursor(display, XC_crosshair);

    XGrabPointer(display,
                 window,
                 False,
                 ButtonPressMask,
                 GrabModeAsync,
                 GrabModeAsync,
                 None,
                 cursor,
                 CurrentTime);

    // Wait for click event to take screenshot
    XEvent event;
    XImage* image = NULL;
    while (1) {
        XNextEvent(display, &event);

        if (event.type == ButtonPress) {
            image = XGetImage(display,
                              window,
                              event.xbutton.x,
                              event.xbutton.y,
                              1,
                              1,
                              AllPlanes,
                              ZPixmap);
            break;
        }
    }

    // Get and print pixel color from screenshot
    XColor color;
    color.pixel = XGetPixel(image, 0, 0);
    unsigned char r = (color.pixel & image->red_mask) >> 16;
    unsigned char g = (color.pixel & image->green_mask) >> 8;
    unsigned char b = color.pixel & image->blue_mask;
    printf("[%u,%u,%u]", r, g, b);

    // Clean up
    XDestroyImage(image);
    XUngrabPointer(display, CurrentTime);
    XFreeCursor(display, cursor);
    XCloseDisplay(display);

    return 0;
}

If you install it as mint-color-picker, color picking from Flatpak apps works without errors, except the returned color is always black. This is because I wasn't able to figure out how to get and parse the output from the picker. I don't really know much about C.

@mtwebster @clefebvre Any assistance would be appreciated :)

@ghost
Copy link
Author

ghost commented Nov 25, 2023

Wow, it actually works now.

Screencast.from.2023-11-25.16-30-35.webm

Anyway: this expects a color picker binary called "mint-color-picker" that outputs colors in an [r,g,b] format, where r, g, and b are integers 0-255. Works fine with the example picker I posted earlier.

@ghost ghost marked this pull request as ready for review November 25, 2023 15:34
@mtwebster
Copy link
Member

I appreciate the effort in this (and the actual portal stuff looks fine), but there's no reason to have a separate tool to actually pick the color - we can do this in Cinnamon (using the same mechanism as we use for area screenshots), and avoid relying directly upon xlib.

Have a look at gnome's implementation, ours would be similar in cinnamon:
https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/42.9/src/shell-screenshot.c?ref_type=tags#L955
https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/42.9/js/ui/screenshot.js?ref_type=tags#L2510

Like I said, much of it is already there, as part of the area-screenshot code.

@mtwebster mtwebster added the Blocked Needs rebase, changes, or discussion label Nov 28, 2023
@mtwebster
Copy link
Member

Hi, I just opened a pull request to this pull request:
https://github.com/zehkira/xdg-desktop-portal-xapp/pull/1

Once you merge that I can merge this PR

@mtwebster mtwebster merged commit 2aef41a into linuxmint:master Nov 29, 2023
2 checks passed
@mtwebster
Copy link
Member

Thanks!

Once we figure out whether or not we want to provide xfce/mate support for this or not, we may incorporate your standalone picker as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocked Needs rebase, changes, or discussion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add org.freedesktop.portal.Screenshot implementaion
1 participant