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; 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; int max = 0;
Gdk.RGBA color = {}; Gdk.RGBA color = {};
foreach (var kv in histogram) { foreach (var kv in histogram) {
if (kv.value > max) { 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; max = kv.value;
color = kv.key; color = kv.key;
} }
@ -49,22 +55,4 @@ namespace Color {
public double get_pixel_luminance (Gdk.RGBA pix) { public double get_pixel_luminance (Gdk.RGBA pix) {
return pix.red * 0.299 + pix.green * 0.587 + pix.blue * 0.114; 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"); message ("Start");
var histogram = Color.quanitize_pixbuff (pixbuff); var histogram = Color.quanitize_pixbuff (pixbuff);
message ("Histogram OK"); message ("Histogram OK");
var colors = Color.sort_colors (histogram);
message ("Sort OK"); message ("Sort OK");
Gdk.RGBA? fg_color = null, bg_start_color = null, bg_end_color = null; 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; double bg_start_lum = 0;
foreach (var color in colors) {
if (fg_color != null && bg_start_color != null && bg_end_color != null) bg_start_color = Color.get_most_common_color (ref histogram);
break; bg_start_lum = Color.get_pixel_luminance (bg_start_color);
var lum = Color.get_pixel_luminance (color); histogram.unset (bg_start_color);
if (bg_start_color == null) { bg_end_color = Color.get_most_common_color (ref histogram, bg_start_lum, 0.3);
bg_start_color = color; histogram.unset (bg_end_color);
bg_start_lum = lum; fg_color = Color.get_most_common_color (ref histogram, bg_start_lum, 0.5, true);
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;
}
}
if (bg_end_color == null) { if (bg_end_color == null) {
message ("Failed to chose end background color color. Fallbacking to start color"); message ("Failed to chose end background color color. Fallbacking to start color");
bg_end_color = bg_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) { 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) { private async Icon ? process_icon (Icon? icon) {