-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpigment.dart
More file actions
70 lines (59 loc) · 1.8 KB
/
Copy pathpigment.dart
File metadata and controls
70 lines (59 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
library pigment;
import 'dart:ui';
part 'named_colors.dart';
class Pigment extends Color {
Pigment(int value) : super(value);
static bool _hasCorrectHexPattern(String string) {
return RegExp(r'^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$')
.hasMatch(string);
}
static Color? _getRGBColorFromString(String string) {
string = string.replaceAll(" ", ""); // pseudo-trimming
if (string.startsWith("rgb(") && string.endsWith(")")) {
string = string.substring(4, string.length - 1);
final rgb = string.split(",");
if (rgb.length == 3) {
final r = int.tryParse(rgb[0]);
final g = int.tryParse(rgb[1]);
final b = int.tryParse(rgb[2]);
if (r != null && g != null && b != null) {
return Color.fromARGB(255, r, g, b);
}
}
}
return null;
}
static Color _getColor(String color) {
color = color.trim();
final rgbColor = _getRGBColorFromString(color);
if (rgbColor != null) {
return rgbColor;
}
if (_hasCorrectHexPattern(color)) {
var hex = color.replaceAll("#", "");
if (hex.length == 3) {
hex = hex.split('').map((c) => c + c).join('');
}
if (hex.length == 6) {
hex = "FF$hex";
}
if (hex.length == 8) {
hex = hex.substring(6, 8) + hex.substring(0, 6);
}
return Color(int.parse("0x$hex"));
}
final namedColor = cssColors[color.toLowerCase()];
if (namedColor != null) {
return _getColor(namedColor);
}
throw 'color pattern [$color] not found! D:';
}
static Color fromString(String color) {
return _getColor(color);
}
static Color fromCSSColor(CSSColor color) {
final colorName =
color.toString().substring(color.toString().indexOf('.') + 1);
return _getColor(colorName);
}
}