diff --git a/vala-quick-setting/color.vala b/vala-quick-setting/color.vala index d874b48..3023835 100644 --- a/vala-quick-setting/color.vala +++ b/vala-quick-setting/color.vala @@ -34,11 +34,17 @@ namespace Color { return histogram; } - public Gdk.RGBA get_most_common_color (ref Gee.HashMap histogram) { + public Gdk.RGBA get_most_common_color (ref Gee.HashMap 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 sort_colors (Gee.HashMap histogram) { - var new_keys = new Gee.ArrayList (); - 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; - } } diff --git a/vala-quick-setting/player.vala b/vala-quick-setting/player.vala index 4da2d33..3ff6958 100644 --- a/vala-quick-setting/player.vala +++ b/vala-quick-setting/player.vala @@ -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 ("Picture", () => generate_style (pb)); + new Thread ("Picture", () => generate_style (pb)); } private async Icon ? process_icon (Icon? icon) {