Skip to content

Commit 23dc3eb

Browse files
committed
fix: fix comparing named colors
Consider the alpha channel value.
1 parent acb7df5 commit 23dc3eb

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

lib/src/utils.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,27 @@ class ColorUtils {
2626
/// Convert into bbcode recognized color.
2727
static String toBBCodeColor(String color, {bool useHex = true}) {
2828
// Try use named color first.
29-
final colorValue = int.tryParse(color.substring(1), radix: 16);
29+
int? colorValue;
30+
31+
if (color.startsWith('#')) {
32+
colorValue = int.tryParse(color.substring(1), radix: 16);
33+
} else {
34+
colorValue = int.tryParse(color, radix: 16);
35+
}
36+
3037
final WebColors webColor;
3138
if (colorValue != null) {
39+
// WebColors all have alpha value, append the same value if not have it.
40+
if (colorValue < 0xFF000000) {
41+
colorValue += 0xFF000000;
42+
}
3243
webColor = WebColors.values.firstWhereOrNull((e) => e.colorValue == colorValue) ?? WebColors.fromString(color);
3344
} else {
3445
webColor = WebColors.fromString(color);
3546
}
3647

48+
print('>>> WEB COLOR: $color => $webColor');
49+
3750
if (webColor.isValid) {
3851
return '${webColor.name[0].toUpperCase()}${webColor.name.substring(1)}';
3952
}

0 commit comments

Comments
 (0)