Make it faster

Signed-off-by: Vasiliy Doylov <nekocwd@mainlining.org>
This commit is contained in:
Vasiliy Doylov 2025-07-10 19:04:01 +03:00
parent b0d9a44f36
commit cb75e22abe
2 changed files with 17 additions and 41 deletions

View file

@ -34,11 +34,17 @@ namespace Color {
return histogram;
}
public Gdk.RGBA get_most_common_color (ref Gee.HashMap<Gdk.RGBA?, int> histogram) {
public Gdk.RGBA get_most_common_color (ref Gee.HashMap<Gdk.RGBA?, int> histogram, double? luma = null, double luma_delta = 0.4, bool inv = false) {
int max = 0;
Gdk.RGBA color = {};
foreach (var kv in histogram) {
if (kv.value > max) {
if (luma != null) {
var _luma = get_pixel_luminance (kv.key);
var state = Math.fabs (_luma - luma) > luma_delta;
if (inv ? !state : state)
continue;
}
max = kv.value;
color = kv.key;
}
@ -49,22 +55,4 @@ namespace Color {
public double get_pixel_luminance (Gdk.RGBA pix) {
return pix.red * 0.299 + pix.green * 0.587 + pix.blue * 0.114;
}
public Gee.ArrayList<Gdk.RGBA?> sort_colors (Gee.HashMap<Gdk.RGBA?, int> histogram) {
var new_keys = new Gee.ArrayList<Gdk.RGBA?> ();
var tr = 2;
foreach (var key in histogram.keys) {
if (key == null)
continue;
if (histogram[key] < tr)
continue;
new_keys.add (key);
}
new_keys.sort ((a, b) => {
var a_ = histogram[a];
var b_ = histogram[b];
return a_ > b_ ? -1 : a_ == b_ ? 0 : 1;
});
return new_keys;
}
}

View file

@ -57,29 +57,17 @@ public class Extension.Player : Gtk.Box {
message ("Start");
var histogram = Color.quanitize_pixbuff (pixbuff);
message ("Histogram OK");
var colors = Color.sort_colors (histogram);
message ("Sort OK");
Gdk.RGBA? fg_color = null, bg_start_color = null, bg_end_color = null;
double fg_lum = 0, bg_start_lum = 0, bg_end_lum = 0;
foreach (var color in colors) {
if (fg_color != null && bg_start_color != null && bg_end_color != null)
break;
var lum = Color.get_pixel_luminance (color);
if (bg_start_color == null) {
bg_start_color = color;
bg_start_lum = lum;
continue;
}
if (Math.fabs (bg_start_lum - lum) > 0.4) {
if (fg_color != null)
continue;
fg_color = color;
continue;
} else if (bg_end_color == null) {
bg_end_color = color;
continue;
}
}
double bg_start_lum = 0;
bg_start_color = Color.get_most_common_color (ref histogram);
bg_start_lum = Color.get_pixel_luminance (bg_start_color);
histogram.unset (bg_start_color);
bg_end_color = Color.get_most_common_color (ref histogram, bg_start_lum, 0.3);
histogram.unset (bg_end_color);
fg_color = Color.get_most_common_color (ref histogram, bg_start_lum, 0.5, true);
if (bg_end_color == null) {
message ("Failed to chose end background color color. Fallbacking to start color");
bg_end_color = bg_start_color;
@ -109,7 +97,7 @@ public class Extension.Player : Gtk.Box {
}
private void run_color_thred (Gdk.Pixbuf pb) {
var thread = new Thread<void> ("Picture", () => generate_style (pb));
new Thread<void> ("Picture", () => generate_style (pb));
}
private async Icon ? process_icon (Icon? icon) {