Dart DocumentationbotHslColor

HslColor class

class HslColor implements Hashable {
  final num h, s, l;

  const HslColor._internal(this.h, this.s, this.l);

  factory HslColor(num h, num s, num l) {
    requireArgument(isValidNumber(h), 'h');
    h = (h % 360);

    requireArgument(isValidNumber(s), 's', 'must be a valid number');
    requireArgument(s >= 0 && s <= 1, 's', 'must be >= 0 && <= 1 but was $s');
    requireArgument(isValidNumber(l), 'l', 'must be a valid number');
    requireArgument(l >= 0 && l <= 1, 'l', 'must be >= 0 && <=1 but was $l');

    return new HslColor._internal(h, s, l);
  }

  RgbColor toRgb() {
    final normH = h / 360; // normalize h to fall in [0, 1]

    num r, g, b;

    if (s == 0) {
      r = g = b = l * 255;
    } else {
      var temp1 = 0;
      var temp2 = 0;
      if (l < 0.5) {
        temp2 = l * (1 + s);
      } else {
        temp2 = l + s - (s * l);
      }
      temp1 = 2 * l - temp2;
      r = 255 * _hueToRgb(temp1, temp2, normH + (1 / 3));
      g = 255 * _hueToRgb(temp1, temp2, normH);
      b = 255 * _hueToRgb(temp1, temp2, normH - (1 / 3));
    }

    return new RgbColor(r.round().toInt(), g.round().toInt(), b.round().toInt());
  }

  int hashCode() => Util.getHashCode([h,s,l]);

  bool operator ==(HslColor other) {
    return other != null && other.h == h && other.s == s && other.l == l;
  }

  String toString() => '{HslColor: $h, $s, $l}';

  static num _hueToRgb(num v1, num v2, num vH) {
    vH %= 1.0;

    if ((6 * vH) < 1) {
      return (v1 + (v2 - v1) * 6 * vH);
    } else if (2 * vH < 1) {
      return v2;
    } else if (3 * vH < 2) {
      return (v1 + (v2 - v1) * ((2 / 3) - vH) * 6);
    }
    return v1;
  }

}

Implements

Hashable

Constructors

factory HslColor(num h, num s, num l) #

factory HslColor(num h, num s, num l) {
  requireArgument(isValidNumber(h), 'h');
  h = (h % 360);

  requireArgument(isValidNumber(s), 's', 'must be a valid number');
  requireArgument(s >= 0 && s <= 1, 's', 'must be >= 0 && <= 1 but was $s');
  requireArgument(isValidNumber(l), 'l', 'must be a valid number');
  requireArgument(l >= 0 && l <= 1, 'l', 'must be >= 0 && <=1 but was $l');

  return new HslColor._internal(h, s, l);
}

const HslColor._internal(num h, num s, num l) #

const HslColor._internal(this.h, this.s, this.l);

Properties

final num h #

final num h, s, l;

final num l #

final num h, s, l;

final Type runtimeType #

inherited from Object

A representation of the runtime type of the object.

external Type get runtimeType;

final num s #

final num h, s, l;

Operators

bool operator ==(HslColor other) #

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

docs inherited from Object
bool operator ==(HslColor other) {
  return other != null && other.h == h && other.s == s && other.l == l;
}

Methods

int hashCode() #

Get a hash code for this object.

All objects have hash codes. Hash codes are guaranteed to be the same for objects that are equal when compared using the equality operator ==. Other than that there are no guarantees about the hash codes. They will not be consistent between runs and there are no distribution guarantees.

If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.

docs inherited from Object
int hashCode() => Util.getHashCode([h,s,l]);

factory HslColor(num h, num s, num l) #

factory HslColor(num h, num s, num l) {
  requireArgument(isValidNumber(h), 'h');
  h = (h % 360);

  requireArgument(isValidNumber(s), 's', 'must be a valid number');
  requireArgument(s >= 0 && s <= 1, 's', 'must be >= 0 && <= 1 but was $s');
  requireArgument(isValidNumber(l), 'l', 'must be a valid number');
  requireArgument(l >= 0 && l <= 1, 'l', 'must be >= 0 && <=1 but was $l');

  return new HslColor._internal(h, s, l);
}

const HslColor._internal(num h, num s, num l) #

const HslColor._internal(this.h, this.s, this.l);

noSuchMethod(String name, List args) #

inherited from Object

noSuchMethod is invoked when users invoke a non-existant method on an object. The name of the method and the arguments of the invocation are passed to noSuchMethod. If noSuchMethod returns a value, that value becomes the result of the original invocation.

The default behavior of noSuchMethod is to throw a noSuchMethodError.

external Dynamic noSuchMethod(String name, List args);

const Object() #

inherited from Object

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

const Object();

RgbColor toRgb() #

RgbColor toRgb() {
  final normH = h / 360; // normalize h to fall in [0, 1]

  num r, g, b;

  if (s == 0) {
    r = g = b = l * 255;
  } else {
    var temp1 = 0;
    var temp2 = 0;
    if (l < 0.5) {
      temp2 = l * (1 + s);
    } else {
      temp2 = l + s - (s * l);
    }
    temp1 = 2 * l - temp2;
    r = 255 * _hueToRgb(temp1, temp2, normH + (1 / 3));
    g = 255 * _hueToRgb(temp1, temp2, normH);
    b = 255 * _hueToRgb(temp1, temp2, normH - (1 / 3));
  }

  return new RgbColor(r.round().toInt(), g.round().toInt(), b.round().toInt());
}

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() => '{HslColor: $h, $s, $l}';