getHashFromMesh function
A function that encodes a mesh gradient into a hash string.
Useful for sharing mesh gradients between different platforms, appllications and contexts without the need to share the entire mesh data explcitly.
The hash is a base64 encoded string that contains the compressed binary
representation of the mesh gradient. The hash is prefixed with OM:
to
indicate that it is an O'Mesh hash.
The hash is generated by first encoding the mesh gradient into a binary format using the OMeshBinaryFormatCodec, then compressing the binary data using zlib compression, and finally encoding the compressed data into a unpadded base64 string.
Implementation
String getHashFromMesh(OMeshRect mesh) {
final bytes = OMeshBinaryFormatCodec.v1.encoder.convert(mesh);
final compressed = zlib.zlibCompress(Uint8List.fromList(bytes));
final hash = base64Url.encode(compressed);
final unpaded = _removePadding(hash);
final prefixed = '$_kPrefix$unpaded';
return prefixed;
}