public byte[] Save(IEnumerable <Color> colors) { var ms = new MemoryStream(); using (var bw = new BinaryWriterX(ms, true, ByteOrder.BigEndian)) foreach (var color in colors) { ushort val = 0; if (color.A == 255) { val |= 1 << 15; val |= (ushort)(Support.ChangeBitDepth(color.R, 8, 5) << 10); val |= (ushort)(Support.ChangeBitDepth(color.G, 8, 5) << 5); val |= (ushort)Support.ChangeBitDepth(color.B, 8, 5); } else { val |= (ushort)(Support.ChangeBitDepth(color.A, 8, 3) << 12); val |= (ushort)(Support.ChangeBitDepth(color.R, 8, 4) << 8); val |= (ushort)(Support.ChangeBitDepth(color.G, 8, 4) << 4); val |= (ushort)Support.ChangeBitDepth(color.B, 8, 4); } bw.Write(val); } return(ms.ToArray()); }
public IEnumerable <Color> Load(byte[] input) { using (var br = new BinaryReaderX(new MemoryStream(input), ByteOrder.BigEndian)) { while (br.BaseStream.Position < br.BaseStream.Length) { var value = br.ReadUInt16(); if ((value & 0x8000) == 0x8000) { yield return(Color.FromArgb( 255, Support.ChangeBitDepth((value >> 10) & 0x1f, 5, 8), Support.ChangeBitDepth((value >> 5) & 0x1f, 5, 8), Support.ChangeBitDepth(value & 0x1f, 5, 8) )); } else { yield return(Color.FromArgb( Support.ChangeBitDepth((value >> 12) & 0x7, 3, 8), Support.ChangeBitDepth((value >> 8) & 0xf, 4, 8), Support.ChangeBitDepth((value >> 4) & 0xf, 4, 8), Support.ChangeBitDepth(value & 0xf, 4, 8) )); } } } }