Unverified Commit efa2a474 authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Adding HSLColor and color 'within' matchers for HSVColor and HSLColor (#18294)

This adds an HSLColor class which uses a perceptual color space based upon human perception of colored light (as opposed to HSV, which is based on pigment colors).

You can see the difference in the color spaces here: https://en.wikipedia.org/wiki/HSL_and_HSV

I also added a "within" matcher for both HSLColor and HSVColor that will check if the (floating point) color components are within a certain error.

And tests.
parent bd4cf628
...@@ -167,7 +167,7 @@ class _ChipDemoState extends State<ChipDemo> { ...@@ -167,7 +167,7 @@ class _ChipDemoState extends State<ChipDemo> {
Color _nameToColor(String name) { Color _nameToColor(String name) {
assert(name.length > 1); assert(name.length > 1);
final int hash = name.hashCode & 0xffff; final int hash = name.hashCode & 0xffff;
final double hue = 360.0 * hash / (1 << 15); final double hue = (360.0 * hash / (1 << 15)) % 360.0;
return new HSVColor.fromAHSV(1.0, hue, 0.4, 0.90).toColor(); return new HSVColor.fromAHSV(1.0, hue, 0.4, 0.90).toColor();
} }
......
...@@ -653,6 +653,8 @@ typedef num AnyDistanceFunction(Null a, Null b); ...@@ -653,6 +653,8 @@ typedef num AnyDistanceFunction(Null a, Null b);
const Map<Type, AnyDistanceFunction> _kStandardDistanceFunctions = const <Type, AnyDistanceFunction>{ const Map<Type, AnyDistanceFunction> _kStandardDistanceFunctions = const <Type, AnyDistanceFunction>{
Color: _maxComponentColorDistance, Color: _maxComponentColorDistance,
HSVColor: _maxComponentHSVColorDistance,
HSLColor: _maxComponentHSLColorDistance,
Offset: _offsetDistance, Offset: _offsetDistance,
int: _intDistance, int: _intDistance,
double: _doubleDistance, double: _doubleDistance,
...@@ -671,6 +673,22 @@ double _maxComponentColorDistance(Color a, Color b) { ...@@ -671,6 +673,22 @@ double _maxComponentColorDistance(Color a, Color b) {
return delta.toDouble(); return delta.toDouble();
} }
// Compares hue by converting it to a 0.0 - 1.0 range, so that the comparison
// can be a similar error percentage per component.
double _maxComponentHSVColorDistance(HSVColor a, HSVColor b) {
double delta = math.max<double>((a.saturation - b.saturation).abs(), (a.value - b.value).abs());
delta = math.max<double>(delta, ((a.hue - b.hue) / 360.0).abs());
return math.max<double>(delta, (a.alpha - b.alpha).abs());
}
// Compares hue by converting it to a 0.0 - 1.0 range, so that the comparison
// can be a similar error percentage per component.
double _maxComponentHSLColorDistance(HSLColor a, HSLColor b) {
double delta = math.max<double>((a.saturation - b.saturation).abs(), (a.lightness - b.lightness).abs());
delta = math.max<double>(delta, ((a.hue - b.hue) / 360.0).abs());
return math.max<double>(delta, (a.alpha - b.alpha).abs());
}
double _rectDistance(Rect a, Rect b) { double _rectDistance(Rect a, Rect b) {
double delta = math.max<double>((a.left - b.left).abs(), (a.top - b.top).abs()); double delta = math.max<double>((a.left - b.left).abs(), (a.top - b.top).abs());
delta = math.max<double>(delta, (a.right - b.right).abs()); delta = math.max<double>(delta, (a.right - b.right).abs());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment