Dart DocumentationbotRgbColor

RgbColor class

class RgbColor implements Hashable {
  final int r, g, b;

  const RgbColor._internal(this.r, this.g, this.b);

  factory RgbColor(int r, int g, int b) {
    _validateComponent(r, 'r');
    _validateComponent(g, 'g');
    _validateComponent(b, 'b');

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

  factory RgbColor.fromHex(String hexColor) {
    requireArgumentNotNull(hexColor, 'hexColor');
    hexColor = _normalizeHex(hexColor);
    var r = math.parseInt('0x'.concat(hexColor.substring(1, 3)));
    var g = math.parseInt('0x'.concat(hexColor.substring(3, 5)));
    var b = math.parseInt('0x'.concat(hexColor.substring(5, 7)));

    return new RgbColor(r,g,b);
  }

  String toHex() {
    final buffer = new StringBuffer('#');
    [r,g,b].forEach((c) {
      buffer.add(_prependZeroIfNecessaryHelper(c.toRadixString(16)));
    });
    return buffer.toString();
  }

  HslColor toHsl() {
    // First must normalize r, g, b to be between 0 and 1.
    final normR = r / 255;
    final normG = g / 255;
    final normB = b / 255;
    final max = math.max(normR, math.max(normG, normB));
    final min = math.min(normR, math.min(normG, normB));
    // Luminosity is the average of the max and min rgb color intensities.
    final l = 0.5 * (max + min);

    double h = 0.0;
    double s = 0.0;

    // The hue and saturation are dependent on which color intensity is the max.
    // If max and min are equal, the color is gray and h and s should be 0.
    if (max != min) {
      if (max == normR) {
        h = 60 * (normG - normB) / (max - min);
      } else if (max == normG) {
        h = 60 * (normB - normR) / (max - min) + 120;
      } else if (max == normB) {
        h = 60 * (normR - normG) / (max - min) + 240;
      }

      if (0 < l && l <= 0.5) {
        s = (max - min) / (2 * l);
      } else {
        s = (max - min) / (2 - 2 * l);
        // handle a nice case where s can be just over 1
        s = math.min(1.0, s);
      }
    }

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

  int hashCode() => Util.getHashCode([r,g,b]);

  bool operator ==(RgbColor other) {
    return other != null && other.r == r && other.g == g && other.b == b;
  }

  String toString() => '{RgbColor: $r, $g, $b}';

  static void _validateComponent(int c, String name) {
    requireArgument(isValidNumber(c), name);
    requireArgument(c >= 0 && c <= 255, name);
  }

  static String _prependZeroIfNecessaryHelper(String hex) {
    return hex.length == 1 ? '0'.concat(hex) : hex;
  }

  // TODO: support colors in the format #rgb
  //       right now we only support #rrggbb
  static String _normalizeHex(String hexColor) {
    if (!_isValidHexColor(hexColor)) {
      throw new IllegalArgumentException("'$hexColor' is not a valid hex color");
    }
    return hexColor.toLowerCase();
  }

  static const RegExp _validHexColorRe = const RegExp('^#(?:[0-9a-f]{6})\$',
      multiLine: false, ignoreCase: true);

  static bool _isValidHexColor(String str) {
    return _validHexColorRe.hasMatch(str);
  }
}

Implements

Hashable

Constructors

factory RgbColor(int r, int g, int b) #

factory RgbColor(int r, int g, int b) {
  _validateComponent(r, 'r');
  _validateComponent(g, 'g');
  _validateComponent(b, 'b');

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

factory RgbColor.fromHex(String hexColor) #

factory RgbColor.fromHex(String hexColor) {
  requireArgumentNotNull(hexColor, 'hexColor');
  hexColor = _normalizeHex(hexColor);
  var r = math.parseInt('0x'.concat(hexColor.substring(1, 3)));
  var g = math.parseInt('0x'.concat(hexColor.substring(3, 5)));
  var b = math.parseInt('0x'.concat(hexColor.substring(5, 7)));

  return new RgbColor(r,g,b);
}

const RgbColor._internal(int r, int g, int b) #

const RgbColor._internal(this.r, this.g, this.b);

Properties

final int b #

final int r, g, b;

final int g #

final int r, g, b;

final int r #

final int r, g, b;

final Type runtimeType #

inherited from Object

A representation of the runtime type of the object.

external Type get runtimeType;

Operators

bool operator ==(RgbColor 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 ==(RgbColor other) {
  return other != null && other.r == r && other.g == g && other.b == b;
}

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([r,g,b]);

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();

factory RgbColor(int r, int g, int b) #

factory RgbColor(int r, int g, int b) {
  _validateComponent(r, 'r');
  _validateComponent(g, 'g');
  _validateComponent(b, 'b');

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

const RgbColor._internal(int r, int g, int b) #

const RgbColor._internal(this.r, this.g, this.b);

factory RgbColor.fromHex(String hexColor) #

factory RgbColor.fromHex(String hexColor) {
  requireArgumentNotNull(hexColor, 'hexColor');
  hexColor = _normalizeHex(hexColor);
  var r = math.parseInt('0x'.concat(hexColor.substring(1, 3)));
  var g = math.parseInt('0x'.concat(hexColor.substring(3, 5)));
  var b = math.parseInt('0x'.concat(hexColor.substring(5, 7)));

  return new RgbColor(r,g,b);
}

String toHex() #

String toHex() {
  final buffer = new StringBuffer('#');
  [r,g,b].forEach((c) {
    buffer.add(_prependZeroIfNecessaryHelper(c.toRadixString(16)));
  });
  return buffer.toString();
}

HslColor toHsl() #

HslColor toHsl() {
  // First must normalize r, g, b to be between 0 and 1.
  final normR = r / 255;
  final normG = g / 255;
  final normB = b / 255;
  final max = math.max(normR, math.max(normG, normB));
  final min = math.min(normR, math.min(normG, normB));
  // Luminosity is the average of the max and min rgb color intensities.
  final l = 0.5 * (max + min);

  double h = 0.0;
  double s = 0.0;

  // The hue and saturation are dependent on which color intensity is the max.
  // If max and min are equal, the color is gray and h and s should be 0.
  if (max != min) {
    if (max == normR) {
      h = 60 * (normG - normB) / (max - min);
    } else if (max == normG) {
      h = 60 * (normB - normR) / (max - min) + 120;
    } else if (max == normB) {
      h = 60 * (normR - normG) / (max - min) + 240;
    }

    if (0 < l && l <= 0.5) {
      s = (max - min) / (2 * l);
    } else {
      s = (max - min) / (2 - 2 * l);
      // handle a nice case where s can be just over 1
      s = math.min(1.0, s);
    }
  }

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

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() => '{RgbColor: $r, $g, $b}';