Merge lp:~tuxator/midori/colors into lp:midori

Proposed by Paweł Forysiuk
Status: Merged
Approved by: Cris Dywan
Approved revision: 6174
Merged at revision: 6177
Proposed branch: lp:~tuxator/midori/colors
Merge into: lp:midori
Diff against target: 241 lines (+149/-66)
1 file modified
extensions/colorful-tabs.c (+149/-66)
To merge this branch: bzr merge lp:~tuxator/midori/colors
Reviewer Review Type Date Requested Status
Cris Dywan Approve
Review via email: mp+165714@code.launchpad.net

Commit message

Split colorful tabs code into helper functions and add unit tests

To post a comment you must log in.
Revision history for this message
Cris Dywan (kalikiana) wrote :

I love the refactoring.

As a follow-up thought, it might be nice to improve the big if () before the color function calls, it duplicates part of what the functions do and probably can be improved.

review: Approve
Revision history for this message
Cris Dywan (kalikiana) wrote :

If I'm nitpicking I'd use a prefix for the function names in case you see them in a backtrace but that can also be fixed another time.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'extensions/colorful-tabs.c'
--- extensions/colorful-tabs.c 2013-04-26 18:54:16 +0000
+++ extensions/colorful-tabs.c 2013-05-27 10:54:30 +0000
@@ -13,55 +13,90 @@
13#include <midori/midori.h>13#include <midori/midori.h>
1414
15static GdkColor15static GdkColor
16view_get_bgcolor_for_hostname (MidoriView* view, gchar* hostname)16get_foreground_color_for_GdkColor (GdkColor color)
17{17{
18 GdkColor color;18 /* rgb (160, 160, 160) is gray */
19 GdkPixbuf* icon = midori_view_get_icon (view);19 guint gray = 160 * 255;
20 if (icon != NULL)20 GdkColor fgcolor;
21 {21
22 GdkPixbuf* newpix;22 /* Ensure high contrast by enforcing black/ white text colour. */
23 guchar* pixels;23 if ((color.red < gray)
2424 && (color.green < gray)
25 newpix = gdk_pixbuf_scale_simple (icon, 1, 1, GDK_INTERP_BILINEAR);25 && (color.blue < gray))
26 pixels = gdk_pixbuf_get_pixels (newpix);26 gdk_color_parse ("white", &fgcolor);
27 color.red = pixels[0] * 255;27 else
28 color.green = pixels[1] * 255;28 gdk_color_parse ("black", &fgcolor);
29 color.blue = pixels[2] * 255;29
30 }30 return fgcolor;
31 else31}
32 {32
33 gchar* hash, *colorstr;33static GdkColor adjust_brightness (GdkColor color)
3434{
35 hash = g_compute_checksum_for_string (G_CHECKSUM_MD5, hostname, 1);35 guint dark_grey = 137 * 255;
36 colorstr = g_strndup (hash, 6 + 1);36 guint adjustment = 78 * 255;
37 colorstr[0] = '#';37 guint blue = 39 * 255;
38 gdk_color_parse (colorstr, &color);38 guint readjust = 19 * 255;
3939
40 g_free (hash);40 if ((color.red < dark_grey)
41 g_free (colorstr);41 && (color.green < dark_grey)
42 }42 && (color.blue < dark_grey))
4343 {
44 if ((color.red < 35000)44 color.red += adjustment;
45 && (color.green < 35000)45 color.green += adjustment;
46 && (color.blue < 35000))46 color.blue += adjustment;
47 {47 }
48 color.red += 20000;48
49 color.green += 20000;49 if (color.red < blue)
50 color.blue += 20000;50 color.red = readjust;
51 }51 else
5252 color.red -= readjust;
53 if (color.red < 10000)53
54 color.red = 5000;54 if (color.blue < blue)
55 else55 color.blue = readjust;
56 color.red -= 5000;56 else
57 if (color.blue < 10000)57 color.blue -= readjust;
58 color.blue = 5000;58
59 else59 if (color.green < blue)
60 color.blue -= 5000;60 color.green = readjust;
61 if (color.green < 10000)61 else
62 color.green = 5000;62 color.green -= readjust;
63 else63
64 color.green -= 5000;64 return color;
65}
66
67static GdkColor
68view_get_bgcolor_for_favicon (GdkPixbuf* icon)
69{
70 GdkColor color;
71 GdkPixbuf* newpix;
72 guchar* pixels;
73
74 newpix = gdk_pixbuf_scale_simple (icon, 1, 1, GDK_INTERP_BILINEAR);
75 pixels = gdk_pixbuf_get_pixels (newpix);
76 color.red = pixels[0] * 255;
77 color.green = pixels[1] * 255;
78 color.blue = pixels[2] * 255;
79
80 color = adjust_brightness (color);
81
82 return color;
83}
84
85static GdkColor
86view_get_bgcolor_for_hostname (gchar* hostname)
87{
88 gchar* hash, *colorstr;
89 GdkColor color;
90
91 hash = g_compute_checksum_for_string (G_CHECKSUM_MD5, hostname, 1);
92 colorstr = g_strndup (hash, 6 + 1);
93 colorstr[0] = '#';
94 gdk_color_parse (colorstr, &color);
95
96 g_free (hash);
97 g_free (colorstr);
98
99 color = adjust_brightness (color);
65100
66 return color;101 return color;
67}102}
@@ -71,25 +106,28 @@
71 GParamSpec* pspec,106 GParamSpec* pspec,
72 MidoriExtension* extension)107 MidoriExtension* extension)
73{108{
74 gchar* hostname;109 const gchar* uri = midori_view_get_display_uri (view);
75 GdkColor color;110 if (!*uri)
76 GdkColor fgcolor;111 return;
77112
78 if (!midori_uri_is_blank (midori_view_get_display_uri (view))113 if (!midori_uri_is_blank (uri))
79 && (hostname = midori_uri_parse_hostname (midori_view_get_display_uri (view), NULL))
80 && midori_view_get_icon_uri (view) != NULL)
81 {114 {
82 color = view_get_bgcolor_for_hostname (view, hostname);115 gchar* hostname = midori_uri_parse_hostname (uri, NULL);
83 g_free (hostname);116 if (hostname)
84 /* Ensure high contrast by enforcing black/ white text colour. */117 {
85 if ((color.red < 41000)118 GdkColor fgcolor, color;
86 && (color.green < 41000)119 GdkPixbuf* icon = midori_view_get_icon (view);
87 && (color.blue < 41000))120
88 gdk_color_parse ("#fff", &fgcolor);121 if (icon)
89 else122 color = view_get_bgcolor_for_favicon (icon);
90 gdk_color_parse ("#000", &fgcolor);123 else
91124 color = view_get_bgcolor_for_hostname (hostname);
92 midori_view_set_colors (view, &fgcolor, &color);125
126 fgcolor = get_foreground_color_for_GdkColor (color);
127 midori_view_set_colors (view, &fgcolor, &color);
128
129 g_free (hostname);
130 }
93 }131 }
94 else132 else
95 midori_view_set_colors (view, NULL, NULL);133 midori_view_set_colors (view, NULL, NULL);
@@ -170,6 +208,51 @@
170 g_object_unref (browsers);208 g_object_unref (browsers);
171}209}
172210
211void test_colour_for_hostname (void)
212{
213 GdkColor color;
214 GdkColor fgcolor;
215
216 typedef struct
217 {
218 const gchar* host;
219 const gchar* fgcolor;
220 const gchar* color;
221 } ColorItem;
222
223 static const ColorItem items[] = {
224 { "www.last.fm", "#ffffffffffff", "#12ed7da312ed" },
225 { "git.xfce.org", "#000000000000", "#1c424c72e207" },
226 { "elementaryos.org", "#000000000000", "#50dbac36b43e" },
227 { "news.ycombinator.com", "#000000000000", "#a5cba6cc5278" },
228 { "cgit.freedesktop.org", "#ffffffffffff", "#95bb8db37ca2" },
229 { "get.cm", "#000000000000", "#1c424c72e207" },
230 };
231
232 guint i;
233 for (i = 0; i < G_N_ELEMENTS (items); i++)
234 {
235 color = view_get_bgcolor_for_hostname ((gchar*)items[i].host);
236 fgcolor = get_foreground_color_for_GdkColor (color);
237
238 g_assert_cmpstr (items[i].color, ==, gdk_color_to_string (&color));
239 g_assert_cmpstr (items[i].fgcolor, ==, gdk_color_to_string (&fgcolor));
240 }
241}
242
243void
244extension_test (void)
245{
246 #ifndef HAVE_WEBKIT2
247 g_object_set_data (G_OBJECT (webkit_get_default_session ()),
248 "midori-session-initialized", (void*)1);
249 #endif
250
251 /* TODO: Add test which uses favicon codepath */
252
253 g_test_add_func ("/extensions/colorful_tabs/hostname_colour", test_colour_for_hostname);
254}
255
173MidoriExtension*256MidoriExtension*
174extension_init (void)257extension_init (void)
175{258{

Subscribers

People subscribed via source and target branches

to all changes: