toHex method

  1. @override
String toHex()

Implementation

@override
String toHex() {
  double _x = x / 100;
  double _y = y / 100;
  double _z = z / 100;

  double _r = _x * 3.2406 + _y * -1.5372 + _z * -0.4986;
  double _g = _x * -0.9689 + _y * 1.8758 + _z * 0.0415;
  double _b = _x * 0.0557 + _y * -0.2040 + _z * 1.0570;

  _r = _r > 0.0031308 ? 1.055 * pow(_r, 1 / 2.4) - 0.055 : _r * 12.92;
  _g = _g > 0.0031308 ? 1.055 * pow(_g, 1 / 2.4) - 0.055 : _g * 12.92;
  _b = _b > 0.0031308 ? 1.055 * pow(_b, 1 / 2.4) - 0.055 : _b * 12.92;

  Function toHex = (double x) {
    var hex = (x * 255).round().toRadixString(16);
    return hex.length == 1 ? '0' + hex : hex;
  };
  return "#" +
      toHex(max(0, min(1, _r)).toDouble()) +
      toHex(max(0, min(1, _g)).toDouble()) +
      toHex(max(0, min(1, _b)).toDouble());
}