Skip to content

Instantly share code, notes, and snippets.

@davidhicks980
Last active January 5, 2023 07:32
Show Gist options
  • Save davidhicks980/c7cc25601805474246768a681fc0203c to your computer and use it in GitHub Desktop.
Save davidhicks980/c7cc25601805474246768a681fc0203c to your computer and use it in GitHub Desktop.
Color extension: hue, saturation, lightness
import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
extension ColorWithHSL on Color {
HSLColor get hsl => HSLColor.fromColor(this);
/// Returns a copy of this color with the [hue] parameter replaced with
/// the given value.
///
/// [saturation] should be between `0.0` and `1.0`.
///
/// Saturation describes how colorful the color is.
///
/// * 0.0 implies a shade of grey (i.e. no pigment)
/// * 1.0 implies a pure pigment (e.g. red)
Color withSaturation(double saturation) {
return hsl.withSaturation(clampDouble(saturation, 0.0, 1.0)).toColor();
}
/// Returns a copy of this color with the [lightness] parameter replaced with
/// the given value.
///
/// [lightness] should be between `0.0` and `1.0`.
///
/// The lightness of a color describes how much white or black is mixed into
/// a color
///
/// * 0.0 is 100% black
/// * 0.5 is pure hue (pure red, for example)
/// * 1.0 is 100% white
Color withLightness(double lightness) {
return hsl.withLightness(clampDouble(lightness, 0.0, 1.0)).toColor();
}
/// Returns a copy of this color with the [hue] parameter replaced with
/// the given value.
///
/// [hue] should be `0.0` to `360.0`.
///
/// * A value of `0.0` represents red, as does `360.0`.
/// * Values in between go through all the hues representable in RGB.
Color withHue(double hue) {
return hsl.withHue(clampDouble(hue, 0.0, 360.0)).toColor();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment