LAB.fromHex constructor

LAB.fromHex(
  1. String hex
)

Implementation

LAB.fromHex(String hex) {
  final values = hex.replaceAll('#', '').split('');

  double _r =
      int.parse(values[0].toString() + values[1].toString(), radix: 16) / 255;
  double _g =
      int.parse(values[2].toString() + values[3].toString(), radix: 16) / 255;
  double _b =
      int.parse(values[4].toString() + values[5].toString(), radix: 16) / 255;

  double _x, _y, _z;

  _r = (_r > 0.04045) ? pow((_r + 0.055) / 1.055, 2.4) as double : _r / 12.92;
  _g = (_g > 0.04045) ? pow((_g + 0.055) / 1.055, 2.4) as double : _g / 12.92;
  _b = (_b > 0.04045) ? pow((_b + 0.055) / 1.055, 2.4) as double : _b / 12.92;

  _x = (_r * 0.4124 + _g * 0.3576 + _b * 0.1805) / 0.95047;
  _y = (_r * 0.2126 + _g * 0.7152 + _b * 0.0722) / 1.00000;
  _z = (_r * 0.0193 + _g * 0.1192 + _b * 0.9505) / 1.08883;

  _x = (_x > 0.008856) ? pow(_x, 1 / 3) as double : (7.787 * _x) + 16 / 116;
  _y = (_y > 0.008856) ? pow(_y, 1 / 3) as double : (7.787 * _y) + 16 / 116;
  _z = (_z > 0.008856) ? pow(_z, 1 / 3) as double : (7.787 * _z) + 16 / 116;

  l = ((116 * _y - 16) * 10).roundToDouble() / 10;
  a = (500 * (_x - _y) * 10).roundToDouble() / 10;
  b = (200 * (_y - _z) * 10).roundToDouble() / 10;
}