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

Wave goodbye to material-color-utilities-python #361

Merged
merged 3 commits into from
Mar 28, 2024

Conversation

midn8hustlr
Copy link
Contributor

Removing dependency material-color-utilities-python in favor of materialyoucolor-python for picking colors from image, as this issue is fixed, and the algorithm is faster too.

@end-4
Copy link
Owner

end-4 commented Mar 26, 2024

The color generation is faster, but not the color picking?

This PR makes the process for large images much slower. Try setting this very large AI-generated and -upscaled image as wallpaper and you'll see... For me it takes only 1s currently but with this PR it takes 20s

@midn8hustlr
Copy link
Contributor Author

I haven't tested that for large 4K images.
Added image resizing for very large images in c62d771.
Please check the latest commit.

@T-Dynamos
Copy link

T-Dynamos commented Mar 26, 2024

I recommend to have a look at android's compression algorithm:

    // Maximum size that a bitmap can have to keep our calculations valid
    private static final int MAX_BITMAP_SIZE = 112;

    // Even though we have a maximum size, we'll mainly match bitmap sizes
    // using the area instead. This way our comparisons are aspect ratio independent.
    private static final int MAX_WALLPAPER_EXTRACTION_AREA = MAX_BITMAP_SIZE * MAX_BITMAP_SIZE;
        if (bitmapArea > MAX_WALLPAPER_EXTRACTION_AREA) {
            shouldRecycle = true;
            Size optimalSize = calculateOptimalSize(bitmap.getWidth(), bitmap.getHeight());
            bitmap = Bitmap.createScaledBitmap(bitmap, optimalSize.getWidth(),
                    optimalSize.getHeight(), false /* filter */);
        }
    private static Size calculateOptimalSize(int width, int height) {
        // Calculate how big the bitmap needs to be.
        // This avoids unnecessary processing and allocation inside Palette.
        final int requestedArea = width * height;
        double scale = 1;
        if (requestedArea > MAX_WALLPAPER_EXTRACTION_AREA) {
            scale = Math.sqrt(MAX_WALLPAPER_EXTRACTION_AREA / (double) requestedArea);
        }
        int newWidth = (int) (width * scale);
        int newHeight = (int) (height * scale);
        // Dealing with edge cases of the drawable being too wide or too tall.
        // Width or height would end up being 0, in this case we'll set it to 1.
        if (newWidth == 0) {
            newWidth = 1;
        }
        if (newHeight == 0) {
            newHeight = 1;
        }

        return new Size(newWidth, newHeight);
    }

(That bitmap is used for generating colors)

@midn8hustlr midn8hustlr reopened this Mar 26, 2024
@midn8hustlr
Copy link
Contributor Author

Hmm, This algorithm is much better, as it will handle any arbitrary image aspect ratio and still ensure that the resized area is always the same.
So the compution time is going to be same for any image resolution with any kind of aspect ratio.

The previous algorithm will yield a high bitmap for arbitrary non-standard image aspect ratios (like panaromas).

Perhaps we should always crop the image to the respective screen resolution and then generate colors.

I will consider this algorithm and push a new commit. end-4 dont merge this right now.

Thanks a lot T-Dynamos for the recommendation 💙

@midn8hustlr
Copy link
Contributor Author

Added new algo in 7d50e43
You can merge it now

@end-4
Copy link
Owner

end-4 commented Mar 28, 2024

Performance is good now
Thanks for your work!

@end-4 end-4 merged commit 6af1066 into end-4:main Mar 28, 2024
end-4 added a commit to Soliprem/dots-hyprland that referenced this pull request May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants