-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set accent color based on primary color (#1124)
- Loading branch information
1 parent
a4b4c4b
commit 9157128
Showing
2 changed files
with
57 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -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++) { | ||
|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters