Files
korken_mosaic/lib/project_codec.dart

96 lines
3.2 KiB
Dart

import 'dart:convert';
import 'dart:typed_data';
class MosaicPaletteSnapshotEntry {
final String name;
final int colorValue;
const MosaicPaletteSnapshotEntry({
required this.name,
required this.colorValue,
});
Map<String, dynamic> toJson() => {
'name': name,
'colorValue': colorValue,
};
factory MosaicPaletteSnapshotEntry.fromJson(Map<String, dynamic> json) {
return MosaicPaletteSnapshotEntry(
name: json['name'] as String? ?? 'Unbenannt',
colorValue: (json['colorValue'] as num?)?.toInt() ?? 0xFF000000,
);
}
}
class MosaicProjectData {
final bool useCapSize;
final String gridWidth;
final String gridHeight;
final String capSize;
final double fidelityStructure;
final double ditheringStrength;
final double edgeEmphasis;
final double colorVariation;
final String selectedPreset;
final Uint8List? sourceImageBytes;
final List<MosaicPaletteSnapshotEntry> catalogSnapshot;
final DateTime savedAt;
const MosaicProjectData({
required this.useCapSize,
required this.gridWidth,
required this.gridHeight,
required this.capSize,
required this.fidelityStructure,
required this.ditheringStrength,
required this.edgeEmphasis,
required this.colorVariation,
required this.selectedPreset,
required this.sourceImageBytes,
required this.catalogSnapshot,
required this.savedAt,
});
Map<String, dynamic> toJson() => {
'useCapSize': useCapSize,
'gridWidth': gridWidth,
'gridHeight': gridHeight,
'capSize': capSize,
'fidelityStructure': fidelityStructure,
'ditheringStrength': ditheringStrength,
'edgeEmphasis': edgeEmphasis,
'colorVariation': colorVariation,
'selectedPreset': selectedPreset,
'sourceImageBase64':
sourceImageBytes == null ? null : base64Encode(sourceImageBytes!),
'catalogSnapshot': catalogSnapshot.map((entry) => entry.toJson()).toList(),
'savedAt': savedAt.toIso8601String(),
};
factory MosaicProjectData.fromJson(Map<String, dynamic> json) {
final sourceB64 = json['sourceImageBase64'] as String?;
final snapshotRaw = (json['catalogSnapshot'] as List?) ?? const [];
return MosaicProjectData(
useCapSize: json['useCapSize'] as bool? ?? false,
gridWidth: json['gridWidth'] as String? ?? '40',
gridHeight: json['gridHeight'] as String? ?? '30',
capSize: json['capSize'] as String? ?? '12',
fidelityStructure: (json['fidelityStructure'] as num?)?.toDouble() ?? 0.5,
ditheringStrength:
(json['ditheringStrength'] as num?)?.toDouble() ?? 0.35,
edgeEmphasis: (json['edgeEmphasis'] as num?)?.toDouble() ?? 0.4,
colorVariation: (json['colorVariation'] as num?)?.toDouble() ?? 0.3,
selectedPreset: json['selectedPreset'] as String? ?? 'ausgewogen',
sourceImageBytes: sourceB64 == null ? null : base64Decode(sourceB64),
catalogSnapshot: snapshotRaw
.whereType<Map>()
.map((entry) => MosaicPaletteSnapshotEntry.fromJson(
Map<String, dynamic>.from(entry)))
.toList(growable: false),
savedAt:
DateTime.tryParse(json['savedAt'] as String? ?? '') ?? DateTime.now(),
);
}
}