Exemplo n.º 1
0
        public static void Write(this IBinaryWriteStream writer, Color color, ColorBinaryType binaryType)
        {
            switch (binaryType)
            {
            case ColorBinaryType.NoAlpha:
                writer.Write(color.R);
                writer.Write(color.G);
                writer.Write(color.B);
                break;

            case ColorBinaryType.Alpha:
                writer.Write(color.R);
                writer.Write(color.G);
                writer.Write(color.B);
                writer.Write(color.A);
                break;

            case ColorBinaryType.NoAlphaFloat:
                writer.Write((float)(color.R / 255d));
                writer.Write((float)(color.G / 255d));
                writer.Write((float)(color.B / 255d));
                break;

            case ColorBinaryType.AlphaFloat:
                writer.Write((float)(color.R / 255d));
                writer.Write((float)(color.G / 255d));
                writer.Write((float)(color.B / 255d));
                writer.Write((float)(color.A / 255d));
                break;

            default:
                break;
            }
        }
Exemplo n.º 2
0
        public static Action <MutagenWriter, Color> GetWriter(ColorBinaryType binaryType)
        {
            switch (binaryType)
            {
            case ColorBinaryType.NoAlpha:
                return((w, c) =>
                {
                    w.Write(c, ColorBinaryType.NoAlpha);
                });

            case ColorBinaryType.Alpha:
                return((w, c) =>
                {
                    w.Write(c, ColorBinaryType.Alpha);
                });

            case ColorBinaryType.NoAlphaFloat:
                return((w, c) =>
                {
                    w.Write(c, ColorBinaryType.NoAlphaFloat);
                });

            case ColorBinaryType.AlphaFloat:
                return((w, c) =>
                {
                    w.Write(c, ColorBinaryType.AlphaFloat);
                });

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 3
0
 public void WriteNullable(
     MutagenWriter writer,
     Color?item,
     ColorBinaryType binaryType)
 {
     this.WriteNullable(
         writer,
         item,
         write: GetWriter(binaryType));
 }
Exemplo n.º 4
0
 public bool Parse(
     MutagenFrame reader,
     [MaybeNullWhen(false)] out Color item,
     ColorBinaryType binaryType)
 {
     if (binaryType != ColorBinaryType.NoAlpha)
     {
         throw new NotImplementedException();
     }
     item = this.Parse(reader);
     return(true);
 }
Exemplo n.º 5
0
 public void Write(
     MutagenWriter writer,
     Color item,
     RecordType header,
     ColorBinaryType binaryType)
 {
     this.Write(
         writer,
         item,
         header,
         write: GetWriter(binaryType));
 }
Exemplo n.º 6
0
 public static void WriteNullable(
     this PrimitiveBinaryTranslation <Color, MutagenFrame, MutagenWriter> transl,
     MutagenWriter writer,
     Color?item,
     RecordType header,
     ColorBinaryType binaryType)
 {
     transl.WriteNullable(
         writer,
         item,
         header,
         write: ColorBinaryTranslation.GetWriter(binaryType));
 }
Exemplo n.º 7
0
 public bool Parse(
     MutagenFrame frame,
     [MaybeNullWhen(false)] out Color item,
     ColorBinaryType binaryType)
 {
     if (binaryType != ColorBinaryType.NoAlpha)
     {
         throw new NotImplementedException();
     }
     return(this.Parse(
                frame,
                out item));
 }
Exemplo n.º 8
0
        /// <summary>
        /// Reads a color from the binary stream.
        /// The stream will be advanced 4 bytes (or 3 if only 3 remain).
        /// If the stream has more than 3 bytes, the 4th byte will be interpreted as alpha.
        /// Will throw an exception if there is not at least 3 bytes remaining.
        /// </summary>
        /// <param name="stream">Stream to read from</param>
        /// <param name="binaryType">Format to read the color as</param>
        /// <returns>Bytes converted to a Color object</returns>
        public static Color ReadColor(this IBinaryReadStream stream, ColorBinaryType binaryType)
        {
            switch (binaryType)
            {
            case ColorBinaryType.NoAlpha:
                return(ReadColor(stream.ReadSpan(3), binaryType));

            case ColorBinaryType.Alpha:
                return(ReadColor(stream.ReadSpan(4), binaryType));

            case ColorBinaryType.NoAlphaFloat:
                return(ReadColor(stream.ReadSpan(12), binaryType));

            case ColorBinaryType.AlphaFloat:
                return(ReadColor(stream.ReadSpan(16), binaryType));

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Extracts a color from binary span.
        /// If the span more than 3 bytes, the 4th byte will be interpreted as alpha.
        /// Will throw an exception if there is not at least 3 bytes.
        /// </summary>
        /// <param name="span">Span to read from</param>
        /// <param name="binaryType">Format to read the color as</param>
        /// <returns>Bytes converted to a Color object</returns>
        public static Color ReadColor(this ReadOnlySpan <byte> span, ColorBinaryType binaryType)
        {
            switch (binaryType)
            {
            case ColorBinaryType.NoAlpha:
                return(Color.FromArgb(
                           red: span[0],
                           green: span[1],
                           blue: span[2],
                           alpha: 0));

            case ColorBinaryType.Alpha:
                return(Color.FromArgb(
                           red: span[0],
                           green: span[1],
                           blue: span[2],
                           alpha: span[3]));

            case ColorBinaryType.NoAlphaFloat:
                return(Color.FromArgb(
                           red: GetColorByte(SpanExt.GetFloat(span.Slice(0, 4))),
                           green: GetColorByte(SpanExt.GetFloat(span.Slice(4, 4))),
                           blue: GetColorByte(SpanExt.GetFloat(span.Slice(8, 4))),
                           alpha: 0));

            case ColorBinaryType.AlphaFloat:
                return(Color.FromArgb(
                           red: GetColorByte(SpanExt.GetFloat(span.Slice(0, 4))),
                           green: GetColorByte(SpanExt.GetFloat(span.Slice(4, 4))),
                           blue: GetColorByte(SpanExt.GetFloat(span.Slice(8, 4))),
                           alpha: GetColorByte(SpanExt.GetFloat(span.Slice(12, 4)))));

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// Extracts a color from binary span.
 /// If the span more than 3 bytes, the 4th byte will be interpreted as alpha.
 /// Will throw an exception if there is not at least 3 bytes.
 /// </summary>
 /// <param name="span">Span to read from</param>
 /// <returns>Bytes converted to a Color object</returns>
 public static Color ReadColor(this ReadOnlyMemorySlice <byte> span, ColorBinaryType binaryType)
 {
     return(span.Span.ReadColor(binaryType));
 }
Exemplo n.º 11
0
 public Color Parse(
     MutagenFrame reader,
     ColorBinaryType binaryType)
 {
     return(reader.ReadColor(binaryType));
 }
Exemplo n.º 12
0
 public Color Parse(
     MutagenFrame frame,
     ColorBinaryType binaryType)
 {
     return(frame.ReadColor(binaryType));
 }