fromJson static method

PixelChannel fromJson(
  1. Map<String, Object?> json
)

The written form back into a PixelDiff a stage can draw.

The exact counts are gone — toJson keeps a rounded fraction — so they are reconstructed from it, which is faithful to the same five decimal places every reader of the file sees.

Implementation

static PixelChannel fromJson(Map<String, Object?> json) {
  var width = json['width'] as int? ?? 0;
  var height = json['height'] as int? ?? 0;
  var fraction = (json['changed'] as num?)?.toDouble() ?? 0;
  return PixelChannel(
    PixelDiff(
      width: width,
      height: height,
      comparedPixels: width * height,
      changedPixels: (fraction * width * height).round(),
      sizeChanged: json['sizeChanged'] as bool? ?? false,
      clusters: [
        for (var rect in json['clusters'] as List? ?? const [])
          DiffRect.fromJson((rect as Map).cast<String, Object?>()),
      ],
    ),
  );
}