Merge lp:~vanvugt/unity/fix-1010348 into lp:unity

Proposed by Daniel van Vugt
Status: Merged
Approved by: Sam Spilsbury
Approved revision: no longer in the source branch.
Merged at revision: 2398
Proposed branch: lp:~vanvugt/unity/fix-1010348
Merge into: lp:unity
Diff against target: 98 lines (+60/-1)
3 files modified
launcher/BamfLauncherIcon.cpp (+33/-1)
launcher/BamfLauncherIcon.h (+2/-0)
tests/test_bamflaunchericon.cpp (+25/-0)
To merge this branch: bzr merge lp:~vanvugt/unity/fix-1010348
Reviewer Review Type Date Requested Status
Thomi Richards (community) Approve
Sam Spilsbury (community) Approve
Review via email: mp+109519@code.launchpad.net

This proposal supersedes a proposal from 2012-06-08.

Commit message

Fix build failure with Nux 2.x. (LP: #1010348)

Don't depend on Nux 3.x API changes until it becomes necessary.

To post a comment you must log in.
Revision history for this message
Andrea Azzarone (azzar1) wrote : Posted in a previous version of this proposal

Can we have unit test for this?

Btw if I remember correctly when you do nux::Color(0xaabbccdd) nux create a nux color with
red = 0xbb
green = 0xcc
blue = 0xdd
alpha = 0xaa

and *not*
red = 0xaa
green = 0xbb
blue = 0xcc
alpha = 0xdd

Revision history for this message
Daniel van Vugt (vanvugt) wrote : Posted in a previous version of this proposal

Fair question. And the answer is: Not today.

Revision history for this message
Sam Spilsbury (smspillaz) :
review: Approve
Revision history for this message
Thomi Richards (thomir-deactivatedaccount) wrote :

Unit tests pass for me.

review: Approve
Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote :

One thing this misses is the support for the 3 digits hex codes... i.e. #abc == #aabbcc...

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'launcher/BamfLauncherIcon.cpp'
2--- launcher/BamfLauncherIcon.cpp 2012-06-01 09:33:15 +0000
3+++ launcher/BamfLauncherIcon.cpp 2012-06-10 09:49:20 +0000
4@@ -782,6 +782,38 @@
5 }
6 }
7
8+//
9+// ColorStrToARGB:
10+// Parses a color string in the form: "#rrggbbaa", where # and aa are optional.
11+// Returns the color in 32-bit ARGB format: 0xaarrggbb.
12+//
13+// In Nux 3.x, this function is superseded by:
14+// nux::color::Color(std::string const& hex)
15+// (even though this function is much smaller and faster)
16+//
17+// I would really like to #if NUX_VERSION <= ... around this code, but
18+// no such integer macro seems to exist in the Nux headers.
19+//
20+unsigned int ColorStrToARGB(const char *str)
21+{
22+ unsigned int ret = 0;
23+ if (str)
24+ {
25+ const char *hex = str[0] == '#' ? str + 1 : str;
26+ int digits = 0, color = 0;
27+ if (sscanf(hex, "%x%n", &color, &digits))
28+ {
29+ if (hex[digits]) // extra characters after the hex
30+ ret = 0;
31+ else if (digits == 6)
32+ ret = (unsigned int)color | 0xff000000;
33+ else if (digits == 8) // Convert RGBA to ARGB:
34+ ret = ((unsigned int)color >> 8) | ((unsigned int)color << 24);
35+ }
36+ }
37+ return ret;
38+}
39+
40 void BamfLauncherIcon::UpdateBackgroundColor()
41 {
42 bool last_use_custom_bg_color = use_custom_bg_color_;
43@@ -792,7 +824,7 @@
44 use_custom_bg_color_ = !color.empty();
45
46 if (use_custom_bg_color_)
47- bg_color_ = nux::Color(color);
48+ bg_color_ = nux::Color(ColorStrToARGB(color.c_str()));
49
50 if (last_use_custom_bg_color != use_custom_bg_color_ ||
51 last_bg_color != bg_color_)
52
53=== modified file 'launcher/BamfLauncherIcon.h'
54--- launcher/BamfLauncherIcon.h 2012-05-28 16:16:06 +0000
55+++ launcher/BamfLauncherIcon.h 2012-06-10 09:49:20 +0000
56@@ -34,6 +34,8 @@
57 namespace launcher
58 {
59
60+extern unsigned int ColorStrToARGB(const char *str);
61+
62 class Launcher;
63
64 class BamfLauncherIcon : public SimpleLauncherIcon
65
66=== modified file 'tests/test_bamflaunchericon.cpp'
67--- tests/test_bamflaunchericon.cpp 2012-06-06 15:42:07 +0000
68+++ tests/test_bamflaunchericon.cpp 2012-06-10 09:49:20 +0000
69@@ -65,4 +65,29 @@
70 EXPECT_EQ(color.alpha, 0xff / 255.0f);
71 }
72
73+TEST_F(TestBamfLauncherIcon, TestColorStringConversion)
74+{
75+ EXPECT_EQ(launcher::ColorStrToARGB("#12345678"), 0x78123456);
76+ EXPECT_EQ(launcher::ColorStrToARGB("12345678"), 0x78123456);
77+ EXPECT_EQ(launcher::ColorStrToARGB("#12345678q"), 0x0);
78+ EXPECT_EQ(launcher::ColorStrToARGB("12345678q"), 0x0);
79+ EXPECT_EQ(launcher::ColorStrToARGB("#1234567890"), 0x0);
80+ EXPECT_EQ(launcher::ColorStrToARGB("1234567890"), 0x0);
81+ EXPECT_EQ(launcher::ColorStrToARGB("#AABBCC"), 0xFFAABBCC);
82+ EXPECT_EQ(launcher::ColorStrToARGB("AABBCC"), 0xFFAABBCC);
83+ EXPECT_EQ(launcher::ColorStrToARGB("#eeddccbb"), 0xBBEEDDCC);
84+ EXPECT_EQ(launcher::ColorStrToARGB("eeddccbb"), 0xBBEEDDCC);
85+ EXPECT_EQ(launcher::ColorStrToARGB("#2040"), 0x0);
86+ EXPECT_EQ(launcher::ColorStrToARGB("2040"), 0x0);
87+ EXPECT_EQ(launcher::ColorStrToARGB("#2040809"), 0x0);
88+ EXPECT_EQ(launcher::ColorStrToARGB("2040809"), 0x0);
89+ EXPECT_EQ(launcher::ColorStrToARGB("#204080"), 0xFF204080);
90+ EXPECT_EQ(launcher::ColorStrToARGB("204080"), 0xFF204080);
91+ EXPECT_EQ(launcher::ColorStrToARGB("#blah"), 0x0);
92+ EXPECT_EQ(launcher::ColorStrToARGB("blah"), 0x0);
93+ EXPECT_EQ(launcher::ColorStrToARGB("#"), 0x0);
94+ EXPECT_EQ(launcher::ColorStrToARGB(""), 0x0);
95+ EXPECT_EQ(launcher::ColorStrToARGB("helloworld helloworld !!!!"), 0x0);
96+}
97+
98 }