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
1=== modified file 'extensions/colorful-tabs.c'
2--- extensions/colorful-tabs.c 2013-04-26 18:54:16 +0000
3+++ extensions/colorful-tabs.c 2013-05-27 10:54:30 +0000
4@@ -13,55 +13,90 @@
5 #include <midori/midori.h>
6
7 static GdkColor
8-view_get_bgcolor_for_hostname (MidoriView* view, gchar* hostname)
9-{
10- GdkColor color;
11- GdkPixbuf* icon = midori_view_get_icon (view);
12- if (icon != NULL)
13- {
14- GdkPixbuf* newpix;
15- guchar* pixels;
16-
17- newpix = gdk_pixbuf_scale_simple (icon, 1, 1, GDK_INTERP_BILINEAR);
18- pixels = gdk_pixbuf_get_pixels (newpix);
19- color.red = pixels[0] * 255;
20- color.green = pixels[1] * 255;
21- color.blue = pixels[2] * 255;
22- }
23- else
24- {
25- gchar* hash, *colorstr;
26-
27- hash = g_compute_checksum_for_string (G_CHECKSUM_MD5, hostname, 1);
28- colorstr = g_strndup (hash, 6 + 1);
29- colorstr[0] = '#';
30- gdk_color_parse (colorstr, &color);
31-
32- g_free (hash);
33- g_free (colorstr);
34- }
35-
36- if ((color.red < 35000)
37- && (color.green < 35000)
38- && (color.blue < 35000))
39- {
40- color.red += 20000;
41- color.green += 20000;
42- color.blue += 20000;
43- }
44-
45- if (color.red < 10000)
46- color.red = 5000;
47- else
48- color.red -= 5000;
49- if (color.blue < 10000)
50- color.blue = 5000;
51- else
52- color.blue -= 5000;
53- if (color.green < 10000)
54- color.green = 5000;
55- else
56- color.green -= 5000;
57+get_foreground_color_for_GdkColor (GdkColor color)
58+{
59+ /* rgb (160, 160, 160) is gray */
60+ guint gray = 160 * 255;
61+ GdkColor fgcolor;
62+
63+ /* Ensure high contrast by enforcing black/ white text colour. */
64+ if ((color.red < gray)
65+ && (color.green < gray)
66+ && (color.blue < gray))
67+ gdk_color_parse ("white", &fgcolor);
68+ else
69+ gdk_color_parse ("black", &fgcolor);
70+
71+ return fgcolor;
72+}
73+
74+static GdkColor adjust_brightness (GdkColor color)
75+{
76+ guint dark_grey = 137 * 255;
77+ guint adjustment = 78 * 255;
78+ guint blue = 39 * 255;
79+ guint readjust = 19 * 255;
80+
81+ if ((color.red < dark_grey)
82+ && (color.green < dark_grey)
83+ && (color.blue < dark_grey))
84+ {
85+ color.red += adjustment;
86+ color.green += adjustment;
87+ color.blue += adjustment;
88+ }
89+
90+ if (color.red < blue)
91+ color.red = readjust;
92+ else
93+ color.red -= readjust;
94+
95+ if (color.blue < blue)
96+ color.blue = readjust;
97+ else
98+ color.blue -= readjust;
99+
100+ if (color.green < blue)
101+ color.green = readjust;
102+ else
103+ color.green -= readjust;
104+
105+ return color;
106+}
107+
108+static GdkColor
109+view_get_bgcolor_for_favicon (GdkPixbuf* icon)
110+{
111+ GdkColor color;
112+ GdkPixbuf* newpix;
113+ guchar* pixels;
114+
115+ newpix = gdk_pixbuf_scale_simple (icon, 1, 1, GDK_INTERP_BILINEAR);
116+ pixels = gdk_pixbuf_get_pixels (newpix);
117+ color.red = pixels[0] * 255;
118+ color.green = pixels[1] * 255;
119+ color.blue = pixels[2] * 255;
120+
121+ color = adjust_brightness (color);
122+
123+ return color;
124+}
125+
126+static GdkColor
127+view_get_bgcolor_for_hostname (gchar* hostname)
128+{
129+ gchar* hash, *colorstr;
130+ GdkColor color;
131+
132+ hash = g_compute_checksum_for_string (G_CHECKSUM_MD5, hostname, 1);
133+ colorstr = g_strndup (hash, 6 + 1);
134+ colorstr[0] = '#';
135+ gdk_color_parse (colorstr, &color);
136+
137+ g_free (hash);
138+ g_free (colorstr);
139+
140+ color = adjust_brightness (color);
141
142 return color;
143 }
144@@ -71,25 +106,28 @@
145 GParamSpec* pspec,
146 MidoriExtension* extension)
147 {
148- gchar* hostname;
149- GdkColor color;
150- GdkColor fgcolor;
151+ const gchar* uri = midori_view_get_display_uri (view);
152+ if (!*uri)
153+ return;
154
155- if (!midori_uri_is_blank (midori_view_get_display_uri (view))
156- && (hostname = midori_uri_parse_hostname (midori_view_get_display_uri (view), NULL))
157- && midori_view_get_icon_uri (view) != NULL)
158+ if (!midori_uri_is_blank (uri))
159 {
160- color = view_get_bgcolor_for_hostname (view, hostname);
161- g_free (hostname);
162- /* Ensure high contrast by enforcing black/ white text colour. */
163- if ((color.red < 41000)
164- && (color.green < 41000)
165- && (color.blue < 41000))
166- gdk_color_parse ("#fff", &fgcolor);
167- else
168- gdk_color_parse ("#000", &fgcolor);
169-
170- midori_view_set_colors (view, &fgcolor, &color);
171+ gchar* hostname = midori_uri_parse_hostname (uri, NULL);
172+ if (hostname)
173+ {
174+ GdkColor fgcolor, color;
175+ GdkPixbuf* icon = midori_view_get_icon (view);
176+
177+ if (icon)
178+ color = view_get_bgcolor_for_favicon (icon);
179+ else
180+ color = view_get_bgcolor_for_hostname (hostname);
181+
182+ fgcolor = get_foreground_color_for_GdkColor (color);
183+ midori_view_set_colors (view, &fgcolor, &color);
184+
185+ g_free (hostname);
186+ }
187 }
188 else
189 midori_view_set_colors (view, NULL, NULL);
190@@ -170,6 +208,51 @@
191 g_object_unref (browsers);
192 }
193
194+void test_colour_for_hostname (void)
195+{
196+ GdkColor color;
197+ GdkColor fgcolor;
198+
199+ typedef struct
200+ {
201+ const gchar* host;
202+ const gchar* fgcolor;
203+ const gchar* color;
204+ } ColorItem;
205+
206+ static const ColorItem items[] = {
207+ { "www.last.fm", "#ffffffffffff", "#12ed7da312ed" },
208+ { "git.xfce.org", "#000000000000", "#1c424c72e207" },
209+ { "elementaryos.org", "#000000000000", "#50dbac36b43e" },
210+ { "news.ycombinator.com", "#000000000000", "#a5cba6cc5278" },
211+ { "cgit.freedesktop.org", "#ffffffffffff", "#95bb8db37ca2" },
212+ { "get.cm", "#000000000000", "#1c424c72e207" },
213+ };
214+
215+ guint i;
216+ for (i = 0; i < G_N_ELEMENTS (items); i++)
217+ {
218+ color = view_get_bgcolor_for_hostname ((gchar*)items[i].host);
219+ fgcolor = get_foreground_color_for_GdkColor (color);
220+
221+ g_assert_cmpstr (items[i].color, ==, gdk_color_to_string (&color));
222+ g_assert_cmpstr (items[i].fgcolor, ==, gdk_color_to_string (&fgcolor));
223+ }
224+}
225+
226+void
227+extension_test (void)
228+{
229+ #ifndef HAVE_WEBKIT2
230+ g_object_set_data (G_OBJECT (webkit_get_default_session ()),
231+ "midori-session-initialized", (void*)1);
232+ #endif
233+
234+ /* TODO: Add test which uses favicon codepath */
235+
236+ g_test_add_func ("/extensions/colorful_tabs/hostname_colour", test_colour_for_hostname);
237+}
238+
239 MidoriExtension*
240 extension_init (void)
241 {

Subscribers

People subscribed via source and target branches

to all changes: