Skip to content

Commit

Permalink
Set accent color based on primary color (#1124)
Browse files Browse the repository at this point in the history
  • Loading branch information
meisenzahl authored May 3, 2021
1 parent a4b4c4b commit 9157128
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
72 changes: 47 additions & 25 deletions src/AccentColor/AccentColorManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Authored by: Marius Meisenzahl <[email protected]>
*/

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";
Expand Down Expand Up @@ -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) {
var picture_uri = background_settings.get_string ("picture-uri");
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);

var current_stylesheet = interface_settings.get_string (STYLESHEET_KEY);
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");
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++) {
Expand All @@ -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
);
}
}

Expand All @@ -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);
}
}
12 changes: 10 additions & 2 deletions src/AccentColor/ColorExtractor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 9157128

Please sign in to comment.