Skip to content

Commit 768f84d

Browse files
committed
Add support for html named colors
Quill deltas often contain named colors from the standard HTML color palette. This is especially true if the delta was created using Quill on web. These named colors could not be handled by flutter_quill, and so an exception was thrown. This change adds dart contants for the 140 named html colors. It modifies `stringToColor` to check if the input is one of the named colors, and returns the value if found. Web colors are case insensitive.
1 parent e14689b commit 768f84d

File tree

4 files changed

+477
-2
lines changed

4 files changed

+477
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
### Fixed
2020

2121
- Fixed `View.of(context)` calls throwing when used with the `screenshot` package [#2662](https://github.com/singerdmx/flutter-quill/pull/2662).
22+
- Fixed support for html named colors [#2675](https://github.com/singerdmx/flutter-quill/pull/2675)
2223

2324
### Added
2425

lib/src/common/utils/color.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'web_colors.dart';
23

34
import '../../editor/widgets/default_styles.dart';
45

@@ -115,7 +116,12 @@ Color stringToColor(String? s,
115116
return Colors.brown;
116117
}
117118

118-
if (s!.startsWith('rgba')) {
119+
// is it an html named color?
120+
if (WebColors.namedColors.containsKey(s!.toLowerCase())) {
121+
return WebColors.namedColors[s.toLowerCase()]!;
122+
}
123+
124+
if (s.startsWith('rgba')) {
119125
s = s.substring(5); // trim left 'rgba('
120126
s = s.substring(0, s.length - 1); // trim right ')'
121127
final arr = s.split(',').map((e) => e.trim()).toList();
@@ -129,7 +135,7 @@ Color stringToColor(String? s,
129135
}
130136

131137
if (!s.startsWith('#')) {
132-
throw UnsupportedError('Color code not supported');
138+
throw UnsupportedError('Color code not supported: $s');
133139
}
134140

135141
var hex = s.replaceFirst('#', '');

0 commit comments

Comments
 (0)