Split color generation

Signed-off-by: Vasiliy Doylov <nekocwd@mainlining.org>
This commit is contained in:
Vasiliy Doylov 2025-07-10 17:05:46 +03:00
parent 2efd467a2d
commit a00b668f28
2 changed files with 40 additions and 33 deletions

View file

@ -1,4 +1,4 @@
using Gtk 3.0; // This requires blueprint-compiller changes
using Gtk 4.0; // That's fake
template $ExtensionPlayer: Box {
visible: true;
orientation: horizontal;

View file

@ -55,22 +55,11 @@ public class Extension.Player : Gtk.Box {
}
}
private Gdk.RGBA max_color (ref Gee.HashMap<Gdk.RGBA?, int> histogram) {
int max = 0;
Gdk.RGBA bebra = {};
foreach (var kv in histogram) {
if (kv.value > max) {
max = kv.value;
bebra = kv.key;
}
}
return bebra;
}
private async void quanitize (Gdk.Pixbuf pixbuf) {
private async Gee.HashMap<Gdk.RGBA?, int> quanitize_pixbuff (Gdk.Pixbuf pixbuf) {
var histogram = new Gee.HashMap<Gdk.RGBA?, int>
(
(a) => ((uint8) a.red * 255 + ((uint8) a.green * 255 << 8) + ((uint8) a.blue * 255 << 16)),
(a) =>
((uint8) a.red * 255 + ((uint8) a.green * 255 << 8) + ((uint8) a.blue * 255 << 16)),
(a, b) => (a.red == b.red && a.blue == b.blue && a.green == b.green)
);
unowned uint8[] pixels = pixbuf.get_pixels ();
@ -95,25 +84,42 @@ public class Extension.Player : Gtk.Box {
alpha = a / 255.0
};
if (histogram.has_key (rgba)) {
histogram[rgba] += 1;
} else
histogram[rgba] = 1;
}
}
var colors = new Gdk.RGBA[0];
for (var i = 0; i < 2; i++) {
var c = max_color (ref histogram);
colors += c;
histogram[c] = -1;
}
set_colors (colors);
return histogram;
}
private async Icon ? process_pixbuff (Icon? icon) {
private Gdk.RGBA get_most_common_color (ref Gee.HashMap<Gdk.RGBA?, int> histogram) {
int max = 0;
Gdk.RGBA bebra = {};
foreach (var kv in histogram) {
if (kv.value > max) {
max = kv.value;
bebra = kv.key;
}
}
return bebra;
}
private async void generate_style (Gdk.Pixbuf pixbuff) {
var histogram = yield quanitize_pixbuff (pixbuff);
var bg_colors = new Gdk.RGBA[0];
for (var i = 0; i < 2; i++) {
var c = get_most_common_color (ref histogram);
bg_colors += c;
histogram[c] = -1;
}
set_colors (bg_colors);
}
private async Icon ? process_icon (Icon? icon) {
Gdk.Pixbuf pixbuff = null;
if (icon == null)
return null;
Gdk.Pixbuf pixbuff = null;
if (icon is Gdk.Pixbuf)
pixbuff = (Gdk.Pixbuf) icon;
else if (icon is FileIcon) {
@ -127,20 +133,21 @@ public class Extension.Player : Gtk.Box {
} else {
return icon;
}
// Downscale to 110x110
pixbuff = pixbuff.scale_simple (110, 110, Gdk.InterpType.BILINEAR);
quanitize.begin (pixbuff);
var radius = 10;
// Start background generator
generate_style.begin (pixbuff);
var width = pixbuff.width;
var height = pixbuff.height;
message ("%dx%d", width, height);
var surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, width, height);
var cr = new Cairo.Context (surface);
{ // That's a hack for GTK3. TODO: Remove when phosh will be moved to gtk4
var radius = 10;
cr.new_path ();
radius = 10; // Don't cut that edges;
cr.arc (width - radius, radius, radius, deg (-90), deg (0));
cr.arc (width - radius, height - radius, radius, deg (0), deg (90));
// radius = 60;
cr.arc (radius, height - radius, radius, deg (90), deg (180));
cr.arc (radius, radius, radius, deg (180), deg (270));
@ -160,7 +167,7 @@ public class Extension.Player : Gtk.Box {
foreach (var child in box.get_children ()) {
switch (child.get_name ()) {
case "img_art" :
child.notify["gicon"].connect (() => process_pixbuff.begin (((Gtk.Image) child).gicon));
child.notify["gicon"].connect (() => process_icon.begin (((Gtk.Image) child).gicon));
break;
default :
foreach (var child2 in ((Gtk.Box) child).get_children ()) {