OLD | NEW |
1 // | 1 // |
2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
3 // | 3 // |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file or at | 5 // license that can be found in the LICENSE file or at |
6 // https://developers.google.com/open-source/licenses/bsd | 6 // https://developers.google.com/open-source/licenses/bsd |
7 // | 7 // |
8 | 8 |
9 part of charted.core.utils; | 9 part of charted.core.utils; |
10 | 10 |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 /// Create an instance using the passed HEX string. | 269 /// Create an instance using the passed HEX string. |
270 /// Assumes that the string starts with a '#' before HEX chars. | 270 /// Assumes that the string starts with a '#' before HEX chars. |
271 static Color _fromHexString(String hex) { | 271 static Color _fromHexString(String hex) { |
272 if (isNullOrEmpty(hex) || (hex.length != 4 && hex.length != 7)) { | 272 if (isNullOrEmpty(hex) || (hex.length != 4 && hex.length != 7)) { |
273 return new Color.fromRgba(0, 0, 0, 0.0); | 273 return new Color.fromRgba(0, 0, 0, 0.0); |
274 } | 274 } |
275 int rgb = 0; | 275 int rgb = 0; |
276 | 276 |
277 hex = hex.substring(1); | 277 hex = hex.substring(1); |
278 if (hex.length == 3) { | 278 if (hex.length == 3) { |
279 for (final char in hex) { | 279 for (int i = 0; i < hex.length; i++) { |
280 final val = int.parse(char, radix: 16); | 280 final val = int.parse(hex[i], radix: 16); |
281 rgb = (rgb * 16 + val) * 16 + val; | 281 rgb = (rgb * 16 + val) * 16 + val; |
282 } | 282 } |
283 } else if (hex.length == 6) { | 283 } else if (hex.length == 6) { |
284 rgb = int.parse(hex, radix: 16); | 284 rgb = int.parse(hex, radix: 16); |
285 } | 285 } |
286 | 286 |
287 return new Color.fromRgba( | 287 return new Color.fromRgba( |
288 (rgb & 0xff0000) >> 0x10, (rgb & 0xff00) >> 8, (rgb & 0xff), 1.0); | 288 (rgb & 0xff0000) >> 0x10, (rgb & 0xff00) >> 8, (rgb & 0xff), 1.0); |
289 } | 289 } |
290 | 290 |
291 /// Create an instance using the passed HSL color string. | 291 /// Create an instance using the passed HSL color string. |
292 static Color _fromHslString(String value) { | 292 static Color _fromHslString(String value) { |
293 int pos = (value.startsWith('hsl(') || value.startsWith('HSL(')) | 293 int pos = (value.startsWith('hsl(') || value.startsWith('HSL(')) |
294 ? 4 | 294 ? 4 |
295 : (value.startsWith('hsla(') || value.startsWith('HSLA(')) ? 5 : 0; | 295 : (value.startsWith('hsla(') || value.startsWith('HSLA(')) ? 5 : 0; |
296 if (pos != 0) { | 296 if (pos != 0) { |
297 final params = value.substring(pos, value.length - 1).split(','); | 297 final params = value.substring(pos, value.length - 1).split(','); |
298 int h = int.parse(params[0]), | 298 int h = int.parse(params[0]), |
299 s = int.parse(params[1]), | 299 s = int.parse(params[1]), |
300 l = int.parse(params[2]); | 300 l = int.parse(params[2]); |
301 double a = params.length == 3 ? 1.0 : double.parse(params[3]); | 301 double a = params.length == 3 ? 1.0 : double.parse(params[3]); |
302 return new Color.fromHsla(h, s, l, a); | 302 return new Color.fromHsla(h, s, l, a); |
303 } | 303 } |
304 return new Color.fromHsla(0, 0, 0, 0.0); | 304 return new Color.fromHsla(0, 0, 0, 0.0); |
305 } | 305 } |
306 } | 306 } |
OLD | NEW |