Set accent color based on primary color (#1124)

This commit is contained in:
Marius Meisenzahl 2021-05-03 19:58:30 +02:00 committed by GitHub
parent a4b4c4b89d
commit 915712865d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 28 deletions

View File

@ -19,7 +19,7 @@
* Authored by: Marius Meisenzahl <mariusmeisenzahl@gmail.com>
*/
public class Gala.AccentColorManager : Object {
public class Gala.AccentColorManager : Object {
private const string INTERFACE_SCHEMA = "org.gnome.desktop.interface";
private const string STYLESHEET_KEY = "gtk-theme";
private const string TAG_ACCENT_COLOR = "Xmp.xmp.io.elementary.AccentColor";
@ -86,23 +86,36 @@
}
}
background_settings.changed["picture-options"].connect (update_accent_color);
background_settings.changed["picture-uri"].connect (update_accent_color);
background_settings.changed["primary-color"].connect (update_accent_color);
update_accent_color ();
}
private void update_accent_color () {
bool set_accent_color_based_on_wallpaper = gala_accounts_service.prefers_accent_color == 0;
bool set_accent_color_auto = gala_accounts_service.prefers_accent_color == 0;
if (set_accent_color_based_on_wallpaper) {
if (!set_accent_color_auto) {
return;
}
bool set_accent_color_based_on_primary_color = background_settings.get_enum ("picture-options") == 0;
var current_stylesheet = interface_settings.get_string (STYLESHEET_KEY);
debug ("Current stylesheet: %s", current_stylesheet);
NamedColor? new_color = null;
if (set_accent_color_based_on_primary_color) {
var primary_color = background_settings.get_string ("primary-color");
debug ("Current primary color: %s", primary_color);
new_color = get_accent_color_based_on_primary_color (primary_color);
} else {
var picture_uri = background_settings.get_string ("picture-uri");
var current_stylesheet = interface_settings.get_string (STYLESHEET_KEY);
debug ("Current wallpaper: %s", picture_uri);
debug ("Current stylesheet: %s", current_stylesheet);
NamedColor? new_color = null;
var accent_color_name = read_accent_color_name_from_exif (picture_uri);
if (accent_color_name != null) {
for (int i = 0; i < theme_colors.length; i++) {
@ -114,15 +127,15 @@
} else {
new_color = get_accent_color_of_picture_simple (picture_uri);
}
}
if (new_color != null && new_color.theme != current_stylesheet) {
debug ("New stylesheet: %s", new_color.theme);
if (new_color != null && new_color.theme != current_stylesheet) {
debug ("New stylesheet: %s", new_color.theme);
interface_settings.set_string (
STYLESHEET_KEY,
new_color.theme
);
}
interface_settings.set_string (
STYLESHEET_KEY,
new_color.theme
);
}
}
@ -141,26 +154,35 @@
return metadata.get_tag_string (TAG_ACCENT_COLOR);
}
public NamedColor? get_accent_color_of_picture_simple (string picture_uri) {
NamedColor new_color = null;
private NamedColor? get_accent_color (ColorExtractor color_extractor) {
var palette = new Gee.ArrayList<Granite.Drawing.Color> ();
for (int i = 0; i < theme_colors.length; i++) {
palette.add (theme_colors[i].color);
}
var index = color_extractor.get_dominant_color_index (palette);
return theme_colors[index];
}
private NamedColor? get_accent_color_of_picture_simple (string picture_uri) {
var file = File.new_for_uri (picture_uri);
try {
var pixbuf = new Gdk.Pixbuf.from_file (file.get_path ());
var color_extractor = new ColorExtractor (pixbuf);
var color_extractor = new ColorExtractor.from_pixbuf (pixbuf);
var palette = new Gee.ArrayList<Granite.Drawing.Color> ();
for (int i = 0; i < theme_colors.length; i++) {
palette.add (theme_colors[i].color);
}
var index = color_extractor.get_dominant_color_index (palette);
new_color = theme_colors[index];
return get_accent_color (color_extractor);
} catch (Error e) {
warning (e.message);
}
return new_color;
return null;
}
private NamedColor? get_accent_color_based_on_primary_color (string primary_color) {
var granite_primary_color = new Granite.Drawing.Color.from_string (primary_color);
var color_extractor = new ColorExtractor.from_primary_color (granite_primary_color);
return get_accent_color (color_extractor);
}
}

View File

@ -22,16 +22,24 @@
public class Gala.ColorExtractor : Object {
private const double PERCENTAGE_SAMPLE_PIXELS = 0.01;
public Gdk.Pixbuf pixbuf { get; construct set; }
public Gdk.Pixbuf? pixbuf { get; construct set; }
public Granite.Drawing.Color? primary_color { get; construct set; }
private Gee.List<Granite.Drawing.Color> pixels;
public ColorExtractor (Gdk.Pixbuf pixbuf) {
public ColorExtractor.from_pixbuf (Gdk.Pixbuf pixbuf) {
Object (pixbuf: pixbuf);
pixels = convert_pixels_to_rgb (pixbuf.get_pixels_with_length (), pixbuf.has_alpha);
}
public ColorExtractor.from_primary_color (Granite.Drawing.Color primary_color) {
Object (primary_color: primary_color);
pixels = new Gee.ArrayList<Granite.Drawing.Color> ();
pixels.add (primary_color);
}
public int get_dominant_color_index (Gee.List<Granite.Drawing.Color> palette) {
int index = 0;
var matches = new double[palette.size];