diff --git a/packages/flutter/lib/src/painting/colors.dart b/packages/flutter/lib/src/painting/colors.dart index 84d59c2ebc1..d8190688fc0 100644 --- a/packages/flutter/lib/src/painting/colors.dart +++ b/packages/flutter/lib/src/painting/colors.dart @@ -2,45 +2,45 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:ui' show Color; +import 'dart:ui' show Color, lerpDouble; class HSVColor { - const HSVColor.fromAHSV(this.a, this.h, this.s, this.v); + const HSVColor.fromAHSV(this.alpha, this.hue, this.saturation, this.value); /// Alpha, from 0.0 to 1.0. - final double a; + final double alpha; /// Hue, from 0.0 to 360.0. - final double h; + final double hue; /// Saturation, from 0.0 to 1.0. - final double s; + final double saturation; /// Value, from 0.0 to 1.0. - final double v; + final double value; - HSVColor withAlpha(double a) { - return new HSVColor.fromAHSV(a, h, s, v); + HSVColor withAlpha(double alpha) { + return new HSVColor.fromAHSV(alpha, hue, saturation, value); } - HSVColor withHue(double h) { - return new HSVColor.fromAHSV(a, h, s, v); + HSVColor withHue(double hue) { + return new HSVColor.fromAHSV(alpha, hue, saturation, value); } - HSVColor withSaturation(double s) { - return new HSVColor.fromAHSV(a, h, s, v); + HSVColor withSaturation(double saturation) { + return new HSVColor.fromAHSV(alpha, hue, saturation, value); } - HSVColor withValue(double v) { - return new HSVColor.fromAHSV(a, h, s, v); + HSVColor withValue(double value) { + return new HSVColor.fromAHSV(alpha, hue, saturation, value); } /// Returns this color in RGB. Color toColor() { - final double h = this.h % 360; - final double c = s * v; + final double h = hue % 360; + final double c = saturation * value; final double x = c * (1 - (((h / 60.0) % 2) - 1).abs()); - final double m = v - c; + final double m = value - c; double r; double g; @@ -71,10 +71,56 @@ class HSVColor { b = x; } return new Color.fromARGB( - ( a * 0xFF).round(), + (alpha * 0xFF).round(), ((r + m) * 0xFF).round(), ((g + m) * 0xFF).round(), ((b + m) * 0xFF).round() ); } + + HSVColor _scaleAlpha(double factor) { + return withAlpha(alpha * factor); + } + + /// Linearly interpolate between two HSVColors. + /// + /// If either color is null, this function linearly interpolates from a + /// transparent instance of the other color. + static HSVColor lerp(HSVColor a, HSVColor b, double t) { + if (a == null && b == null) + return null; + if (a == null) + return b._scaleAlpha(t); + if (b == null) + return a._scaleAlpha(1.0 - t); + return new HSVColor.fromAHSV( + lerpDouble(a.alpha, b.alpha, t), + lerpDouble(a.hue, b.hue, t), + lerpDouble(a.saturation, b.saturation, t), + lerpDouble(a.value, b.value, t) + ); + } + + bool operator ==(dynamic other) { + if (identical(this, other)) + return true; + if (other is! HSVColor) + return false; + final HSVColor typedOther = other; + return typedOther.alpha == alpha + && typedOther.hue == hue + && typedOther.saturation == saturation + && typedOther.value == value; + } + + int get hashCode { + int value = 373; + value = 37 * value + alpha.hashCode; + value = 37 * value + hue.hashCode; + value = 37 * value + saturation.hashCode; + value = 37 * value + value.hashCode; + return value; + } + + String toString() => "HSVColor($alpha, $hue, $saturation, $value)"; } diff --git a/packages/flutter/lib/src/rendering/view.dart b/packages/flutter/lib/src/rendering/view.dart index bf3a9aba548..87d464100e0 100644 --- a/packages/flutter/lib/src/rendering/view.dart +++ b/packages/flutter/lib/src/rendering/view.dart @@ -130,7 +130,7 @@ class RenderView extends RenderObject with RenderObjectWithChildMixin scene.dispose(); assert(() { if (debugEnableRepaintRainbox) - debugCurrentRepaintColor = debugCurrentRepaintColor.withHue(debugCurrentRepaintColor.h + debugRepaintRainboxHueIncrement); + debugCurrentRepaintColor = debugCurrentRepaintColor.withHue(debugCurrentRepaintColor.hue + debugRepaintRainboxHueIncrement); return true; }); } finally {