8000 Change colour implementation · rk0cc/github_colour_flutter@325d33c · GitHub
[go: up one dir, main page]

Skip to content

Commit 325d33c

Browse files
committed
Change colour implementation
1 parent b4eff34 commit 325d33c

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.2.4
2+
3+
* Change `GitHubColour`'s default colour from `#8f8f8f` to `#f0f0f0`.
4+
15
## 1.2.3
26

37
* Add generic throwable type `GitHubColourThrowable`

lib/github_colour.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ class UndefinedLanguageColourError extends ArgumentError
5858
"UndefinedLanguageColourError");
5959
}
6060

61-
Map<String, Color> _colourReader(String json) =>
62-
(jsonDecode(json) as Map<String, dynamic>)
63-
.map<String, Color>((language, node) {
64-
String hex = node["color"] ?? GitHubColour.defaultColourHex;
65-
hex = "FF${hex.substring(1).toUpperCase()}";
61+
Color _hex2C(String hex, [String alphaHex = "ff"]) {
62+
assert(RegExp(r"^#[0-9a-f]{6}$", caseSensitive: false).hasMatch(hex));
63+
assert(RegExp(r"^[0-9a-f]{2}$", caseSensitive: false).hasMatch(alphaHex));
64+
65+
return Color(
66+
int.parse("$alphaHex${hex.substring(1)}".toUpperCase(), radix: 16));
67+
}
6668

67-
return MapEntry(language, Color(int.parse(hex, radix: 16)));
68-
});
69+
Map<String, Color> _colourReader(String json) =>
70+
(jsonDecode(json) as Map<String, dynamic>).map<String, Color>(
71+
(language, node) => MapEntry(
72+
language, _hex2C(node["color"] ?? GitHubColour.defaultColourHex)));
6973

7074
/// A class for getting GitHub language colour.
7175
@sealed
@@ -74,10 +78,10 @@ class GitHubColour {
7478
final Map<String, Color> _githubLangColour;
7579

7680
/// A [String] of hex value when the language is undefined or null.
77-
static const String defaultColourHex = "#8f8f8f";
81+
static const String defaultColourHex = "#f0f0f0";
7882

7983
/// [Color] object of [defaultColourHex].
80-
static const Color defaultColour = Color(0xff8f8f8f);
84+
static Color get defaultColour => _hex2C(defaultColourHex);
8185

8286
GitHubColour._(Map<String, Color> githubLangColour)
8387
: this._githubLangColour = Map.unmodifiable(githubLangColour);
@@ -122,6 +126,7 @@ class GitHubColour {
122126
ghjson = _colourReader(resp.body);
123127
} catch (neterr) {
124128
try {
129+
// Second source.
125130
ghjson = await getCache();
126131
cacheSource = true;
127132
} catch (cerr) {
@@ -142,6 +147,7 @@ class GitHubColour {
142147
}
143148

144149
if (!cacheSource) {
150+
// Do nothing if received data is exact same with source.
145151
await saveCache(ghjson);
146152
}
147153

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: github_colour
22
description: Receive ozh's GitHub colour to Flutter's colour implementation with caching and fallback.
3-
version: 1.2.3
3+
version: 1.2.4
44
homepage: https://www.rk0cc.xyz
55
repository: https://github.com/rk0cc/github_colour_flutter
66
environment:

0 commit comments

Comments
 (0)
0