From 634ad5c58068ddbf9a9e4d64110a65bd74cb0d86 Mon Sep 17 00:00:00 2001 From: DomCR Date: Sat, 28 Oct 2023 17:16:06 +0200 Subject: [PATCH] color writer --- ACadSharp/Color.cs | 20 +++++++++++++++-- .../DwgStreamWriters/DwgStreamWriterAC18.cs | 22 +++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/ACadSharp/Color.cs b/ACadSharp/Color.cs index 9166b017..792200b2 100644 --- a/ACadSharp/Color.cs +++ b/ACadSharp/Color.cs @@ -1,4 +1,5 @@ -using System; +using CSUtilities.Converters; +using System; namespace ACadSharp { @@ -323,6 +324,21 @@ public bool IsTrueColor } } + /// + /// Red component of the color + /// + public byte R { get { return this.GetRgb()[0]; } } + + /// + /// Green component of the color + /// + public byte G { get { return this.GetRgb()[1]; } } + + /// + /// Blue component of the color + /// + public byte B { get { return this.GetRgb()[2]; } } + /// /// Represents the actual stored color. Either a True Color or an indexed color. /// @@ -501,7 +517,7 @@ private static uint getInt24(byte[] array) private static ReadOnlySpan getRGBfromTrueColor(uint color) { - return new ReadOnlySpan(BitConverter.GetBytes(color), 0, 3); + return new ReadOnlySpan(LittleEndianConverter.Instance.GetBytes(color), 0, 3); } } } diff --git a/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterAC18.cs b/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterAC18.cs index f8123981..a19ab5be 100644 --- a/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterAC18.cs +++ b/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterAC18.cs @@ -1,4 +1,5 @@ -using System.IO; +using CSUtilities.Converters; +using System.IO; using System.Text; namespace ACadSharp.IO.DWG @@ -11,14 +12,27 @@ public DwgStreamWriterAC18(Stream stream, Encoding encoding) : base(stream, enco public override void WriteCmColor(Color value) { - //TODO: Finish writer color implementation - //CMC: //BS: color index(always 0) this.WriteBitShort(0); + byte[] arr = new byte[4]; + + if (value.IsTrueColor) + { + arr[0] = (byte)(value.R); + arr[1] = (byte)(value.G); + arr[2] = (byte)(value.B); + arr[3] = 0b1100_0010; + } + else + { + arr[3] = 0b1100_0011; + arr[0] = (byte)value.Index; + } + //BL: RGB value - this.WriteBitLong(0); + this.WriteBitLong(LittleEndianConverter.Instance.ToInt32(arr)); //RC: Color Byte this.WriteByte(0);