Commit 5722c96b authored by Adam Barth's avatar Adam Barth

Merge pull request #518 from abarth/hsv_color

Elaborate HSVColor interface
parents ffb95f05 c1b07630
...@@ -2,45 +2,45 @@ ...@@ -2,45 +2,45 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:ui' show Color; import 'dart:ui' show Color, lerpDouble;
class HSVColor { 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. /// Alpha, from 0.0 to 1.0.
final double a; final double alpha;
/// Hue, from 0.0 to 360.0. /// Hue, from 0.0 to 360.0.
final double h; final double hue;
/// Saturation, from 0.0 to 1.0. /// Saturation, from 0.0 to 1.0.
final double s; final double saturation;
/// Value, from 0.0 to 1.0. /// Value, from 0.0 to 1.0.
final double v; final double value;
HSVColor withAlpha(double a) { HSVColor withAlpha(double alpha) {
return new HSVColor.fromAHSV(a, h, s, v); return new HSVColor.fromAHSV(alpha, hue, saturation, value);
} }
HSVColor withHue(double h) { HSVColor withHue(double hue) {
return new HSVColor.fromAHSV(a, h, s, v); return new HSVColor.fromAHSV(alpha, hue, saturation, value);
} }
HSVColor withSaturation(double s) { HSVColor withSaturation(double saturation) {
return new HSVColor.fromAHSV(a, h, s, v); return new HSVColor.fromAHSV(alpha, hue, saturation, value);
} }
HSVColor withValue(double v) { HSVColor withValue(double value) {
return new HSVColor.fromAHSV(a, h, s, v); return new HSVColor.fromAHSV(alpha, hue, saturation, value);
} }
/// Returns this color in RGB. /// Returns this color in RGB.
Color toColor() { Color toColor() {
final double h = this.h % 360; final double h = hue % 360;
final double c = s * v; final double c = saturation * value;
final double x = c * (1 - (((h / 60.0) % 2) - 1).abs()); final double x = c * (1 - (((h / 60.0) % 2) - 1).abs());
final double m = v - c; final double m = value - c;
double r; double r;
double g; double g;
...@@ -71,10 +71,56 @@ class HSVColor { ...@@ -71,10 +71,56 @@ class HSVColor {
b = x; b = x;
} }
return new Color.fromARGB( return new Color.fromARGB(
( a * 0xFF).round(), (alpha * 0xFF).round(),
((r + m) * 0xFF).round(), ((r + m) * 0xFF).round(),
((g + m) * 0xFF).round(), ((g + m) * 0xFF).round(),
((b + 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)";
} }
...@@ -130,7 +130,7 @@ class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> ...@@ -130,7 +130,7 @@ class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox>
scene.dispose(); scene.dispose();
assert(() { assert(() {
if (debugEnableRepaintRainbox) if (debugEnableRepaintRainbox)
debugCurrentRepaintColor = debugCurrentRepaintColor.withHue(debugCurrentRepaintColor.h + debugRepaintRainboxHueIncrement); debugCurrentRepaintColor = debugCurrentRepaintColor.withHue(debugCurrentRepaintColor.hue + debugRepaintRainboxHueIncrement);
return true; return true;
}); });
} finally { } finally {
......
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