@@ -58,14 +58,18 @@ class UndefinedLanguageColourError extends ArgumentError
58
58
"UndefinedLanguageColourError" );
59
59
}
60
60
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
+ }
66
68
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)));
69
73
70
74
/// A class for getting GitHub language colour.
71
75
@sealed
@@ -74,10 +78,10 @@ class GitHubColour {
74
78
final Map <String , Color > _githubLangColour;
75
79
76
80
/// A [String] of hex value when the language is undefined or null.
77
- static const String defaultColourHex = "#8f8f8f " ;
81
+ static const String defaultColourHex = "#f0f0f0 " ;
78
82
79
83
/// [Color] object of [defaultColourHex] .
80
- static const Color defaultColour = Color ( 0xff8f8f8f );
84
+ static Color get defaultColour => _hex2C (defaultColourHex );
81
85
82
86
GitHubColour ._(Map <String , Color > githubLangColour)
83
87
: this ._githubLangColour = Map .unmodifiable (githubLangColour);
@@ -122,6 +126,7 @@ class GitHubColour {
122
126
ghjson = _colourReader (resp.body);
123
127
} catch (neterr) {
124
128
try {
129
+ // Second source.
125
130
ghjson = await getCache ();
126
131
cacheSource = true ;
127
132
} catch (cerr) {
@@ -142,6 +147,7 @@ class GitHubColour {
142
147
}
143
148
144
149
if (! cacheSource) {
150
+ // Do nothing if received data is exact same with source.
145
151
await saveCache (ghjson);
146
152
}
147
153
0 commit comments