diff --git a/src/DotTiled.Tests/UnitTests/TiledColorTests.cs b/src/DotTiled.Tests/UnitTests/TiledColorTests.cs new file mode 100644 index 0000000..c9ad1f8 --- /dev/null +++ b/src/DotTiled.Tests/UnitTests/TiledColorTests.cs @@ -0,0 +1,236 @@ +using System.Globalization; + +namespace DotTiled.Tests.UnitTests; + +public class TiledColorTests +{ + [Fact] + public void TiledColor_Size() => Assert.Equal(4, System.Runtime.InteropServices.Marshal.SizeOf()); + + [Fact] + public void DefaultConstructor() + { + var tiledColor = new TiledColor(); + + Assert.Equal(0x00, tiledColor.A); + Assert.Equal(0x00, tiledColor.R); + Assert.Equal(0x00, tiledColor.G); + Assert.Equal(0x00, tiledColor.B); + } + + [Fact] + public void UInt32_Explicit_Cast() + { + TiledColor tiledColor = (TiledColor)0x11223344u; + + Assert.Equal(0x11, tiledColor.A); + Assert.Equal(0x22, tiledColor.R); + Assert.Equal(0x33, tiledColor.G); + Assert.Equal(0x44, tiledColor.B); + } + + [Fact] + public void TiledColor_Explicit_Cast() + { + var tiledColor = new TiledColor(0x11, 0x22, 0x33, 0x44); + + Assert.Equal(0x11223344u, (uint)tiledColor); + } + + [Fact] + public void NonAlphaConstructor() + { + var tiledColor = new TiledColor(0x11, 0x22, 0x33); + + Assert.Equal(0xFF, tiledColor.A); + Assert.Equal(0x11, tiledColor.R); + Assert.Equal(0x22, tiledColor.G); + Assert.Equal(0x33, tiledColor.B); + } + + [Fact] + public void AlphaConstructor() + { + var tiledColor = new TiledColor(0x11, 0x22, 0x33, 0x44); + + Assert.Equal(0x11, tiledColor.A); + Assert.Equal(0x22, tiledColor.R); + Assert.Equal(0x33, tiledColor.G); + Assert.Equal(0x44, tiledColor.B); + } + + [Fact] + public void NonAlphaParsing() + { + var tiledColor = TiledColor.Parse("#112233", CultureInfo.InvariantCulture); + + Assert.Equal(0x11, tiledColor.R); + Assert.Equal(0x22, tiledColor.G); + Assert.Equal(0x33, tiledColor.B); + } + + [Fact] + public void AlphaParsing() + { + var tiledColor = TiledColor.Parse("#11223344", CultureInfo.InvariantCulture); + + Assert.Equal(0x11, tiledColor.A); + Assert.Equal(0x22, tiledColor.R); + Assert.Equal(0x33, tiledColor.G); + Assert.Equal(0x44, tiledColor.B); + } + + [Fact] + public void Static_TiledColors_ToString() + { + Assert.Equal("#00000000", TiledColor.Transparent.ToString()); + Assert.Equal("#fff0f8ff", TiledColor.AliceBlue.ToString()); + Assert.Equal("#fffaebd7", TiledColor.AntiqueWhite.ToString()); + Assert.Equal("#ff00ffff", TiledColor.Aqua.ToString()); + Assert.Equal("#ff7fffd4", TiledColor.Aquamarine.ToString()); + Assert.Equal("#fff0ffff", TiledColor.Azure.ToString()); + Assert.Equal("#fff5f5dc", TiledColor.Beige.ToString()); + Assert.Equal("#ffffe4c4", TiledColor.Bisque.ToString()); + Assert.Equal("#ff000000", TiledColor.Black.ToString()); + Assert.Equal("#ffffebcd", TiledColor.BlanchedAlmond.ToString()); + Assert.Equal("#ff0000ff", TiledColor.Blue.ToString()); + Assert.Equal("#ff8a2be2", TiledColor.BlueViolet.ToString()); + Assert.Equal("#ffa52a2a", TiledColor.Brown.ToString()); + Assert.Equal("#ffdeb887", TiledColor.BurlyWood.ToString()); + Assert.Equal("#ff5f9ea0", TiledColor.CadetBlue.ToString()); + Assert.Equal("#ff7fff00", TiledColor.Chartreuse.ToString()); + Assert.Equal("#ffd2691e", TiledColor.Chocolate.ToString()); + Assert.Equal("#ffff7f50", TiledColor.Coral.ToString()); + Assert.Equal("#ff6495ed", TiledColor.CornflowerBlue.ToString()); + Assert.Equal("#fffff8dc", TiledColor.Cornsilk.ToString()); + Assert.Equal("#ffdc143c", TiledColor.Crimson.ToString()); + Assert.Equal("#ff00ffff", TiledColor.Cyan.ToString()); + Assert.Equal("#ff00008b", TiledColor.DarkBlue.ToString()); + Assert.Equal("#ff008b8b", TiledColor.DarkCyan.ToString()); + Assert.Equal("#ffb8860b", TiledColor.DarkGoldenRod.ToString()); + Assert.Equal("#ffa9a9a9", TiledColor.DarkGray.ToString()); + Assert.Equal("#ffa9a9a9", TiledColor.DarkGrey.ToString()); + Assert.Equal("#ff006400", TiledColor.DarkGreen.ToString()); + Assert.Equal("#ffbdb76b", TiledColor.DarkKhaki.ToString()); + Assert.Equal("#ff8b008b", TiledColor.DarkMagenta.ToString()); + Assert.Equal("#ff556b2f", TiledColor.DarkOliveGreen.ToString()); + Assert.Equal("#ffff8c00", TiledColor.DarkOrange.ToString()); + Assert.Equal("#ff9932cc", TiledColor.DarkOrchid.ToString()); + Assert.Equal("#ff8b0000", TiledColor.DarkRed.ToString()); + Assert.Equal("#ffe9967a", TiledColor.DarkSalmon.ToString()); + Assert.Equal("#ff8fbc8f", TiledColor.DarkSeaGreen.ToString()); + Assert.Equal("#ff483d8b", TiledColor.DarkSlateBlue.ToString()); + Assert.Equal("#ff2f4f4f", TiledColor.DarkSlateGray.ToString()); + Assert.Equal("#ff2f4f4f", TiledColor.DarkSlateGrey.ToString()); + Assert.Equal("#ff00ced1", TiledColor.DarkTurquoise.ToString()); + Assert.Equal("#ff9400d3", TiledColor.DarkViolet.ToString()); + Assert.Equal("#ffff1493", TiledColor.DeepPink.ToString()); + Assert.Equal("#ff00bfff", TiledColor.DeepSkyBlue.ToString()); + Assert.Equal("#ff696969", TiledColor.DimGray.ToString()); + Assert.Equal("#ff696969", TiledColor.DimGrey.ToString()); + Assert.Equal("#ff1e90ff", TiledColor.DodgerBlue.ToString()); + Assert.Equal("#ffb22222", TiledColor.FireBrick.ToString()); + Assert.Equal("#fffffaf0", TiledColor.FloralWhite.ToString()); + Assert.Equal("#ff228b22", TiledColor.ForestGreen.ToString()); + Assert.Equal("#ffff00ff", TiledColor.Fuchsia.ToString()); + Assert.Equal("#ffdcdcdc", TiledColor.Gainsboro.ToString()); + Assert.Equal("#fff8f8ff", TiledColor.GhostWhite.ToString()); + Assert.Equal("#ffffd700", TiledColor.Gold.ToString()); + Assert.Equal("#ffdaa520", TiledColor.GoldenRod.ToString()); + Assert.Equal("#ff808080", TiledColor.Gray.ToString()); + Assert.Equal("#ff808080", TiledColor.Grey.ToString()); + Assert.Equal("#ff008000", TiledColor.Green.ToString()); + Assert.Equal("#ffadff2f", TiledColor.GreenYellow.ToString()); + Assert.Equal("#fff0fff0", TiledColor.HoneyDew.ToString()); + Assert.Equal("#ffff69b4", TiledColor.HotPink.ToString()); + Assert.Equal("#ffcd5c5c", TiledColor.IndianRed.ToString()); + Assert.Equal("#ff4b0082", TiledColor.Indigo.ToString()); + Assert.Equal("#fffffff0", TiledColor.Ivory.ToString()); + Assert.Equal("#fff0e68c", TiledColor.Khaki.ToString()); + Assert.Equal("#ffe6e6fa", TiledColor.Lavender.ToString()); + Assert.Equal("#fffff0f5", TiledColor.LavenderBlush.ToString()); + Assert.Equal("#ff7cfc00", TiledColor.LawnGreen.ToString()); + Assert.Equal("#fffffacd", TiledColor.LemonChiffon.ToString()); + Assert.Equal("#ffadd8e6", TiledColor.LightBlue.ToString()); + Assert.Equal("#fff08080", TiledColor.LightCoral.ToString()); + Assert.Equal("#ffe0ffff", TiledColor.LightCyan.ToString()); + Assert.Equal("#fffafad2", TiledColor.LightGoldenRodYellow.ToString()); + Assert.Equal("#ffd3d3d3", TiledColor.LightGray.ToString()); + Assert.Equal("#ffd3d3d3", TiledColor.LightGrey.ToString()); + Assert.Equal("#ff90ee90", TiledColor.LightGreen.ToString()); + Assert.Equal("#ffffb6c1", TiledColor.LightPink.ToString()); + Assert.Equal("#ffffa07a", TiledColor.LightSalmon.ToString()); + Assert.Equal("#ff20b2aa", TiledColor.LightSeaGreen.ToString()); + Assert.Equal("#ff87cefa", TiledColor.LightSkyBlue.ToString()); + Assert.Equal("#ff778899", TiledColor.LightSlateGray.ToString()); + Assert.Equal("#ff778899", TiledColor.LightSlateGrey.ToString()); + Assert.Equal("#ffb0c4de", TiledColor.LightSteelBlue.ToString()); + Assert.Equal("#ffffffe0", TiledColor.LightYellow.ToString()); + Assert.Equal("#ff00ff00", TiledColor.Lime.ToString()); + Assert.Equal("#ff32cd32", TiledColor.LimeGreen.ToString()); + Assert.Equal("#fffaf0e6", TiledColor.Linen.ToString()); + Assert.Equal("#ffff00ff", TiledColor.Magenta.ToString()); + Assert.Equal("#ff800000", TiledColor.Maroon.ToString()); + Assert.Equal("#ff66cdaa", TiledColor.MediumAquaMarine.ToString()); + Assert.Equal("#ff0000cd", TiledColor.MediumBlue.ToString()); + Assert.Equal("#ffba55d3", TiledColor.MediumOrchid.ToString()); + Assert.Equal("#ff9370db", TiledColor.MediumPurple.ToString()); + Assert.Equal("#ff3cb371", TiledColor.MediumSeaGreen.ToString()); + Assert.Equal("#ff7b68ee", TiledColor.MediumSlateBlue.ToString()); + Assert.Equal("#ff00fa9a", TiledColor.MediumSpringGreen.ToString()); + Assert.Equal("#ff48d1cc", TiledColor.MediumTurquoise.ToString()); + Assert.Equal("#ffc71585", TiledColor.MediumVioletRed.ToString()); + Assert.Equal("#ff191970", TiledColor.MidnightBlue.ToString()); + Assert.Equal("#fff5fffa", TiledColor.MintCream.ToString()); + Assert.Equal("#ffffe4e1", TiledColor.MistyRose.ToString()); + Assert.Equal("#ffffe4b5", TiledColor.Moccasin.ToString()); + Assert.Equal("#ffffdead", TiledColor.NavajoWhite.ToString()); + Assert.Equal("#ff000080", TiledColor.Navy.ToString()); + Assert.Equal("#fffdf5e6", TiledColor.OldLace.ToString()); + Assert.Equal("#ff808000", TiledColor.Olive.ToString()); + Assert.Equal("#ff6b8e23", TiledColor.OliveDrab.ToString()); + Assert.Equal("#ffffa500", TiledColor.Orange.ToString()); + Assert.Equal("#ffff4500", TiledColor.OrangeRed.ToString()); + Assert.Equal("#ffda70d6", TiledColor.Orchid.ToString()); + Assert.Equal("#ffeee8aa", TiledColor.PaleGoldenRod.ToString()); + Assert.Equal("#ff98fb98", TiledColor.PaleGreen.ToString()); + Assert.Equal("#ffafeeee", TiledColor.PaleTurquoise.ToString()); + Assert.Equal("#ffdb7093", TiledColor.PaleVioletRed.ToString()); + Assert.Equal("#ffffefd5", TiledColor.PapayaWhip.ToString()); + Assert.Equal("#ffffdab9", TiledColor.PeachPuff.ToString()); + Assert.Equal("#ffcd853f", TiledColor.Peru.ToString()); + Assert.Equal("#ffffc0cb", TiledColor.Pink.ToString()); + Assert.Equal("#ffdda0dd", TiledColor.Plum.ToString()); + Assert.Equal("#ffb0e0e6", TiledColor.PowderBlue.ToString()); + Assert.Equal("#ff800080", TiledColor.Purple.ToString()); + Assert.Equal("#ff663399", TiledColor.RebeccaPurple.ToString()); + Assert.Equal("#ffff0000", TiledColor.Red.ToString()); + Assert.Equal("#ffbc8f8f", TiledColor.RosyBrown.ToString()); + Assert.Equal("#ff4169e1", TiledColor.RoyalBlue.ToString()); + Assert.Equal("#ff8b4513", TiledColor.SaddleBrown.ToString()); + Assert.Equal("#fffa8072", TiledColor.Salmon.ToString()); + Assert.Equal("#fff4a460", TiledColor.SandyBrown.ToString()); + Assert.Equal("#ff2e8b57", TiledColor.SeaGreen.ToString()); + Assert.Equal("#fffff5ee", TiledColor.SeaShell.ToString()); + Assert.Equal("#ffa0522d", TiledColor.Sienna.ToString()); + Assert.Equal("#ffc0c0c0", TiledColor.Silver.ToString()); + Assert.Equal("#ff87ceeb", TiledColor.SkyBlue.ToString()); + Assert.Equal("#ff6a5acd", TiledColor.SlateBlue.ToString()); + Assert.Equal("#ff708090", TiledColor.SlateGray.ToString()); + Assert.Equal("#ff708090", TiledColor.SlateGrey.ToString()); + Assert.Equal("#fffffafa", TiledColor.Snow.ToString()); + Assert.Equal("#ff00ff7f", TiledColor.SpringGreen.ToString()); + Assert.Equal("#ff4682b4", TiledColor.SteelBlue.ToString()); + Assert.Equal("#ffd2b48c", TiledColor.Tan.ToString()); + Assert.Equal("#ff008080", TiledColor.Teal.ToString()); + Assert.Equal("#ffd8bfd8", TiledColor.Thistle.ToString()); + Assert.Equal("#ffff6347", TiledColor.Tomato.ToString()); + Assert.Equal("#ff40e0d0", TiledColor.Turquoise.ToString()); + Assert.Equal("#ffee82ee", TiledColor.Violet.ToString()); + Assert.Equal("#fff5deb3", TiledColor.Wheat.ToString()); + Assert.Equal("#ffffffff", TiledColor.White.ToString()); + Assert.Equal("#fff5f5f5", TiledColor.WhiteSmoke.ToString()); + Assert.Equal("#ffffff00", TiledColor.Yellow.ToString()); + Assert.Equal("#ff9acd32", TiledColor.YellowGreen.ToString()); + } +} diff --git a/src/DotTiled/Map.cs b/src/DotTiled/Map.cs index b3ca0ce..cd60d6e 100644 --- a/src/DotTiled/Map.cs +++ b/src/DotTiled/Map.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Globalization; namespace DotTiled; @@ -172,7 +171,7 @@ public class Map : HasPropertiesBase /// /// The background color of the map. /// - public TiledColor BackgroundColor { get; set; } = TiledColor.Parse("#00000000", CultureInfo.InvariantCulture); + public TiledColor BackgroundColor { get; set; } = TiledColor.Transparent; /// /// Stores the next available ID for new layers. This number is used to prevent reuse of the same ID after layers have been removed. diff --git a/src/DotTiled/Properties/TiledColor.cs b/src/DotTiled/Properties/TiledColor.cs index cebc1f6..4808039 100644 --- a/src/DotTiled/Properties/TiledColor.cs +++ b/src/DotTiled/Properties/TiledColor.cs @@ -1,33 +1,813 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Globalization; +using System.Runtime.InteropServices; namespace DotTiled; /// /// Represents a Tiled color. /// -public class TiledColor : IParsable, IEquatable +[StructLayout(LayoutKind.Explicit)] +public struct TiledColor : IParsable, IEquatable { - /// - /// The red component of the color. - /// - public required byte R { get; set; } + #region Static_TiledColors /// - /// The green component of the color. + /// Transparent (A:0, R:0, G:0, B:0) /// - public required byte G { get; set; } + public static TiledColor Transparent => new TiledColor(); /// - /// The blue component of the color. + /// Alice Blue (A: 255, R:240, G:248, B:255) /// - public required byte B { get; set; } + public static TiledColor AliceBlue => new TiledColor(0xF0, 0xF8, 0xFF); /// - /// The alpha component of the color. + /// Antique White (A: 255, R:250, G:235, B:215) /// - public byte A { get; set; } = 255; + public static TiledColor AntiqueWhite => new TiledColor(0xFA, 0xEB, 0xD7); + + /// + /// Aqua (A: 255, R:0, G:255, B:255) + /// + public static TiledColor Aqua => new TiledColor(0x00, 0xFF, 0xFF); + + /// + /// Aquamarine (A: 255, R:127, G:255, B:212) + /// + public static TiledColor Aquamarine => new TiledColor(0x7F, 0xFF, 0xD4); + + /// + /// Azure (A: 255, R:240, G:255, B:255) + /// + public static TiledColor Azure => new TiledColor(0xF0, 0xFF, 0xFF); + + /// + /// Beige (A: 255, R:245, G:245, B:220) + /// + public static TiledColor Beige => new TiledColor(0xF5, 0xF5, 0xDC); + + /// + /// Bisque (A: 255, R:255, G:228, B:196) + /// + public static TiledColor Bisque => new TiledColor(0xFF, 0xE4, 0xC4); + + /// + /// Black (A: 255, R:0, G:0, B:0) + /// + public static TiledColor Black => new TiledColor(0x00, 0x00, 0x00); + + /// + /// Blanched Almond (A: 255, R:255, G:235, B:205) + /// + public static TiledColor BlanchedAlmond => new TiledColor(0xFF, 0xEB, 0xCD); + + /// + /// Blue (A: 255, R:0, G:0, B:255) + /// + public static TiledColor Blue => new TiledColor(0x00, 0x00, 0xFF); + + /// + /// Blue Violet (A: 255, R:138, G:43, B:226) + /// + public static TiledColor BlueViolet => new TiledColor(0x8A, 0x2B, 0xE2); + + /// + /// Brown (A: 255, R:165, G:42, B:42) + /// + public static TiledColor Brown => new TiledColor(0xA5, 0x2A, 0x2A); + + /// + /// Burly Wood (A: 255, R:222, G:184, B:135) + /// + public static TiledColor BurlyWood => new TiledColor(0xDE, 0xB8, 0x87); + + /// + /// Cadet Blue (A: 255, R:95, G:158, B:160) + /// + public static TiledColor CadetBlue => new TiledColor(0x5F, 0x9E, 0xA0); + + /// + /// Chartreuse (A: 255, R:127, G:255, B:0) + /// + public static TiledColor Chartreuse => new TiledColor(0x7F, 0xFF, 0x00); + + /// + /// Chocolate (A: 255, R:210, G:105, B:30) + /// + public static TiledColor Chocolate => new TiledColor(0xD2, 0x69, 0x1E); + + /// + /// Coral (A: 255, R:255, G:127, B:80) + /// + public static TiledColor Coral => new TiledColor(0xFF, 0x7F, 0x50); + + /// + /// Cornflower Blue (A: 255, R:100, G:149, B:237) + /// + public static TiledColor CornflowerBlue => new TiledColor(0x64, 0x95, 0xED); + + /// + /// Cornsilk (A: 255, R:255, G:248, B:220) + /// + public static TiledColor Cornsilk => new TiledColor(0xFF, 0xF8, 0xDC); + + /// + /// Crimson (A: 255, R:220, G:20, B:60) + /// + public static TiledColor Crimson => new TiledColor(0xDC, 0x14, 0x3C); + + /// + /// Cyan (A: 255, R:0, G:255, B:255) + /// + public static TiledColor Cyan => new TiledColor(0x00, 0xFF, 0xFF); + + /// + /// Dark Blue (A: 255, R:0, G:0, B:139) + /// + public static TiledColor DarkBlue => new TiledColor(0x00, 0x00, 0x8B); + + /// + /// Dark Cyan (A: 255, R:0, G:139, B:139) + /// + public static TiledColor DarkCyan => new TiledColor(0x00, 0x8B, 0x8B); + + /// + /// Dark Golden Rod (A: 255, R:184, G:134, B:11) + /// + public static TiledColor DarkGoldenRod => new TiledColor(0xB8, 0x86, 0x0B); + + /// + /// Dark Gray (A: 255, R:169, G:169, B:169) + /// + public static TiledColor DarkGray => new TiledColor(0xA9, 0xA9, 0xA9); + + /// + /// Dark Grey (A: 255, R:169, G:169, B:169) + /// + public static TiledColor DarkGrey => new TiledColor(0xA9, 0xA9, 0xA9); + + /// + /// Dark Green (A: 255, R:0, G:100, B:0) + /// + public static TiledColor DarkGreen => new TiledColor(0x00, 0x64, 0x00); + + /// + /// Dark Khaki (A: 255, R:189, G:183, B:107) + /// + public static TiledColor DarkKhaki => new TiledColor(0xBD, 0xB7, 0x6B); + + /// + /// Dark Magenta (A: 255, R:139, G:0, B:139) + /// + public static TiledColor DarkMagenta => new TiledColor(0x8B, 0x00, 0x8B); + + /// + /// Dark Olive Green (A: 255, R:85, G:107, B:47) + /// + public static TiledColor DarkOliveGreen => new TiledColor(0x55, 0x6B, 0x2F); + + /// + /// Dark Orange (A: 255, R:255, G:140, B:0) + /// + public static TiledColor DarkOrange => new TiledColor(0xFF, 0x8C, 0x00); + + /// + /// Dark Orchid (A: 255, R:153, G:50, B:204) + /// + public static TiledColor DarkOrchid => new TiledColor(0x99, 0x32, 0xCC); + + /// + /// Dark Red (A: 255, R:139, G:0, B:0) + /// + public static TiledColor DarkRed => new TiledColor(0x8B, 0x00, 0x00); + + /// + /// Dark Salmon (A: 255, R:233, G:150, B:122) + /// + public static TiledColor DarkSalmon => new TiledColor(0xE9, 0x96, 0x7A); + + /// + /// Dark Sea Green (A: 255, R:143, G:188, B:143) + /// + public static TiledColor DarkSeaGreen => new TiledColor(0x8F, 0xBC, 0x8F); + + /// + /// Dark Slate Blue (A: 255, R:72, G:61, B:139) + /// + public static TiledColor DarkSlateBlue => new TiledColor(0x48, 0x3D, 0x8B); + + /// + /// Dark Slate Gray (A: 255, R:47, G:79, B:79) + /// + public static TiledColor DarkSlateGray => new TiledColor(0x2F, 0x4F, 0x4F); + + /// + /// Dark Slate Grey (A: 255, R:47, G:79, B:79) + /// + public static TiledColor DarkSlateGrey => new TiledColor(0x2F, 0x4F, 0x4F); + + /// + /// Dark Turquoise (A: 255, R:0, G:206, B:209) + /// + public static TiledColor DarkTurquoise => new TiledColor(0x00, 0xCE, 0xD1); + + /// + /// Dark Violet (A: 255, R:148, G:0, B:211) + /// + public static TiledColor DarkViolet => new TiledColor(0x94, 0x00, 0xD3); + + /// + /// Deep Pink (A: 255, R:255, G:20, B:147) + /// + public static TiledColor DeepPink => new TiledColor(0xFF, 0x14, 0x93); + + /// + /// Deep Sky Blue (A: 255, R:0, G:191, B:255) + /// + public static TiledColor DeepSkyBlue => new TiledColor(0x00, 0xBF, 0xFF); + + /// + /// Dim Gray (A: 255, R:105, G:105, B:105) + /// + public static TiledColor DimGray => new TiledColor(0x69, 0x69, 0x69); + + /// + /// Dim Grey (A: 255, R:105, G:105, B:105) + /// + public static TiledColor DimGrey => new TiledColor(0x69, 0x69, 0x69); + + /// + /// Dodger Blue (A: 255, R:30, G:144, B:255) + /// + public static TiledColor DodgerBlue => new TiledColor(0x1E, 0x90, 0xFF); + + /// + /// Fire Brick (A: 255, R:178, G:34, B:34) + /// + public static TiledColor FireBrick => new TiledColor(0xB2, 0x22, 0x22); + + /// + /// Floral White (A: 255, R:255, G:250, B:240) + /// + public static TiledColor FloralWhite => new TiledColor(0xFF, 0xFA, 0xF0); + + /// + /// Forest Green (A: 255, R:34, G:139, B:34) + /// + public static TiledColor ForestGreen => new TiledColor(0x22, 0x8B, 0x22); + + /// + /// Fuchsia (A: 255, R:255, G:0, B:255) + /// + public static TiledColor Fuchsia => new TiledColor(0xFF, 0x00, 0xFF); + + /// + /// Gainsboro (A: 255, R:220, G:220, B:220) + /// + public static TiledColor Gainsboro => new TiledColor(0xDC, 0xDC, 0xDC); + + /// + /// Ghost White (A: 255, R:248, G:248, B:255) + /// + public static TiledColor GhostWhite => new TiledColor(0xF8, 0xF8, 0xFF); + + /// + /// Gold (A: 255, R:255, G:215, B:0) + /// + public static TiledColor Gold => new TiledColor(0xFF, 0xD7, 0x00); + + /// + /// Golden Rod (A: 255, R:218, G:165, B:32) + /// + public static TiledColor GoldenRod => new TiledColor(0xDA, 0xA5, 0x20); + + /// + /// Gray (A: 255, R:128, G:128, B:128) + /// + public static TiledColor Gray => new TiledColor(0x80, 0x80, 0x80); + + /// + /// Grey (A: 255, R:128, G:128, B:128) + /// + public static TiledColor Grey => new TiledColor(0x80, 0x80, 0x80); + + /// + /// Green (A: 255, R:0, G:128, B:0) + /// + public static TiledColor Green => new TiledColor(0x00, 0x80, 0x00); + + /// + /// Green Yellow (A: 255, R:173, G:255, B:47) + /// + public static TiledColor GreenYellow => new TiledColor(0xAD, 0xFF, 0x2F); + + /// + /// Honey Dew (A: 255, R:240, G:255, B:240) + /// + public static TiledColor HoneyDew => new TiledColor(0xF0, 0xFF, 0xF0); + + /// + /// Hot Pink (A: 255, R:255, G:105, B:180) + /// + public static TiledColor HotPink => new TiledColor(0xFF, 0x69, 0xB4); + + /// + /// Indian Red (A: 255, R:205, G:92, B:92) + /// + public static TiledColor IndianRed => new TiledColor(0xCD, 0x5C, 0x5C); + + /// + /// Indigo (A: 255, R:75, G:0, B:130) + /// + public static TiledColor Indigo => new TiledColor(0x4B, 0x00, 0x82); + + /// + /// Ivory (A: 255, R:255, G:255, B:240) + /// + public static TiledColor Ivory => new TiledColor(0xFF, 0xFF, 0xF0); + + /// + /// Khaki (A: 255, R:240, G:230, B:140) + /// + public static TiledColor Khaki => new TiledColor(0xF0, 0xE6, 0x8C); + + /// + /// Lavender (A: 255, R:230, G:230, B:250) + /// + public static TiledColor Lavender => new TiledColor(0xE6, 0xE6, 0xFA); + + /// + /// Lavender Blush (A: 255, R:255, G:240, B:245) + /// + public static TiledColor LavenderBlush => new TiledColor(0xFF, 0xF0, 0xF5); + + /// + /// Lawn Green (A: 255, R:124, G:252, B:0) + /// + public static TiledColor LawnGreen => new TiledColor(0x7C, 0xFC, 0x00); + + /// + /// Lemon Chiffon (A: 255, R:255, G:250, B:205) + /// + public static TiledColor LemonChiffon => new TiledColor(0xFF, 0xFA, 0xCD); + + /// + /// Light Blue (A: 255, R:173, G:216, B:230) + /// + public static TiledColor LightBlue => new TiledColor(0xAD, 0xD8, 0xE6); + + /// + /// Light Coral (A: 255, R:240, G:128, B:128) + /// + public static TiledColor LightCoral => new TiledColor(0xF0, 0x80, 0x80); + + /// + /// Light Cyan (A: 255, R:224, G:255, B:255) + /// + public static TiledColor LightCyan => new TiledColor(0xE0, 0xFF, 0xFF); + + /// + /// Light Golden Rod Yellow (A: 255, R:250, G:250, B:210) + /// + public static TiledColor LightGoldenRodYellow => new TiledColor(0xFA, 0xFA, 0xD2); + + /// + /// Light Gray (A: 255, R:211, G:211, B:211) + /// + public static TiledColor LightGray => new TiledColor(0xD3, 0xD3, 0xD3); + + /// + /// Light Grey (A: 255, R:211, G:211, B:211) + /// + public static TiledColor LightGrey => new TiledColor(0xD3, 0xD3, 0xD3); + + /// + /// Light Green (A: 255, R:144, G:238, B:144) + /// + public static TiledColor LightGreen => new TiledColor(0x90, 0xEE, 0x90); + + /// + /// Light Pink (A: 255, R:255, G:182, B:193) + /// + public static TiledColor LightPink => new TiledColor(0xFF, 0xB6, 0xC1); + + /// + /// Light Salmon (A: 255, R:255, G:160, B:122) + /// + public static TiledColor LightSalmon => new TiledColor(0xFF, 0xA0, 0x7A); + + /// + /// Light Sea Green (A: 255, R:32, G:178, B:170) + /// + public static TiledColor LightSeaGreen => new TiledColor(0x20, 0xB2, 0xAA); + + /// + /// Light Sky Blue (A: 255, R:135, G:206, B:250) + /// + public static TiledColor LightSkyBlue => new TiledColor(0x87, 0xCE, 0xFA); + + /// + /// Light Slate Gray (A: 255, R:119, G:136, B:153) + /// + public static TiledColor LightSlateGray => new TiledColor(0x77, 0x88, 0x99); + + /// + /// Light Slate Grey (A: 255, R:119, G:136, B:153) + /// + public static TiledColor LightSlateGrey => new TiledColor(0x77, 0x88, 0x99); + + /// + /// Light Steel Blue (A: 255, R:176, G:196, B:222) + /// + public static TiledColor LightSteelBlue => new TiledColor(0xB0, 0xC4, 0xDE); + + /// + /// Light Yellow (A: 255, R:255, G:255, B:224) + /// + public static TiledColor LightYellow => new TiledColor(0xFF, 0xFF, 0xE0); + + /// + /// Lime (A: 255, R:0, G:255, B:0) + /// + public static TiledColor Lime => new TiledColor(0x00, 0xFF, 0x00); + + /// + /// Lime Green (A: 255, R:50, G:205, B:50) + /// + public static TiledColor LimeGreen => new TiledColor(0x32, 0xCD, 0x32); + + /// + /// Linen (A: 255, R:250, G:240, B:230) + /// + public static TiledColor Linen => new TiledColor(0xFA, 0xF0, 0xE6); + + /// + /// Magenta (A: 255, R:255, G:0, B:255) + /// + public static TiledColor Magenta => new TiledColor(0xFF, 0x00, 0xFF); + + /// + /// Maroon (A: 255, R:128, G:0, B:0) + /// + public static TiledColor Maroon => new TiledColor(0x80, 0x00, 0x00); + + /// + /// Medium Aqua Marine (A: 255, R:102, G:205, B:170) + /// + public static TiledColor MediumAquaMarine => new TiledColor(0x66, 0xCD, 0xAA); + + /// + /// Medium Blue (A: 255, R:0, G:0, B:205) + /// + public static TiledColor MediumBlue => new TiledColor(0x00, 0x00, 0xCD); + + /// + /// Medium Orchid (A: 255, R:186, G:85, B:211) + /// + public static TiledColor MediumOrchid => new TiledColor(0xBA, 0x55, 0xD3); + + /// + /// Medium Purple (A: 255, R:147, G:112, B:219) + /// + public static TiledColor MediumPurple => new TiledColor(0x93, 0x70, 0xDB); + + /// + /// Medium Sea Green (A: 255, R:60, G:179, B:113) + /// + public static TiledColor MediumSeaGreen => new TiledColor(0x3C, 0xB3, 0x71); + + /// + /// Medium Slate Blue (A: 255, R:123, G:104, B:238) + /// + public static TiledColor MediumSlateBlue => new TiledColor(0x7B, 0x68, 0xEE); + + /// + /// Medium Spring Green (A: 255, R:0, G:250, B:154) + /// + public static TiledColor MediumSpringGreen => new TiledColor(0x00, 0xFA, 0x9A); + + /// + /// Medium Turquoise (A: 255, R:72, G:209, B:204) + /// + public static TiledColor MediumTurquoise => new TiledColor(0x48, 0xD1, 0xCC); + + /// + /// Medium Violet Red (A: 255, R:199, G:21, B:133) + /// + public static TiledColor MediumVioletRed => new TiledColor(0xC7, 0x15, 0x85); + + /// + /// Midnight Blue (A: 255, R:25, G:25, B:112) + /// + public static TiledColor MidnightBlue => new TiledColor(0x19, 0x19, 0x70); + + /// + /// Mint Cream (A: 255, R:245, G:255, B:250) + /// + public static TiledColor MintCream => new TiledColor(0xF5, 0xFF, 0xFA); + + /// + /// Misty Rose (A: 255, R:255, G:228, B:225) + /// + public static TiledColor MistyRose => new TiledColor(0xFF, 0xE4, 0xE1); + + /// + /// Moccasin (A: 255, R:255, G:228, B:181) + /// + public static TiledColor Moccasin => new TiledColor(0xFF, 0xE4, 0xB5); + + /// + /// Navajo White (A: 255, R:255, G:222, B:173) + /// + public static TiledColor NavajoWhite => new TiledColor(0xFF, 0xDE, 0xAD); + + /// + /// Navy (A: 255, R:0, G:0, B:128) + /// + public static TiledColor Navy => new TiledColor(0x00, 0x00, 0x80); + + /// + /// Old Lace (A: 255, R:253, G:245, B:230) + /// + public static TiledColor OldLace => new TiledColor(0xFD, 0xF5, 0xE6); + + /// + /// Olive (A: 255, R:128, G:128, B:0) + /// + public static TiledColor Olive => new TiledColor(0x80, 0x80, 0x00); + + /// + /// Olive Drab (A: 255, R:107, G:142, B:35) + /// + public static TiledColor OliveDrab => new TiledColor(0x6B, 0x8E, 0x23); + + /// + /// Orange (A: 255, R:255, G:165, B:0) + /// + public static TiledColor Orange => new TiledColor(0xFF, 0xA5, 0x00); + + /// + /// Orange Red (A: 255, R:255, G:69, B:0) + /// + public static TiledColor OrangeRed => new TiledColor(0xFF, 0x45, 0x00); + + /// + /// Orchid (A: 255, R:218, G:112, B:214) + /// + public static TiledColor Orchid => new TiledColor(0xDA, 0x70, 0xD6); + + /// + /// Pale Golden Rod (A: 255, R:238, G:232, B:170) + /// + public static TiledColor PaleGoldenRod => new TiledColor(0xEE, 0xE8, 0xAA); + + /// + /// Pale Green (A: 255, R:152, G:251, B:152) + /// + public static TiledColor PaleGreen => new TiledColor(0x98, 0xFB, 0x98); + + /// + /// Pale Turquoise (A: 255, R:175, G:238, B:238) + /// + public static TiledColor PaleTurquoise => new TiledColor(0xAF, 0xEE, 0xEE); + + /// + /// Pale Violet Red (A: 255, R:219, G:112, B:147) + /// + public static TiledColor PaleVioletRed => new TiledColor(0xDB, 0x70, 0x93); + + /// + /// Papaya Whip (A: 255, R:255, G:239, B:213) + /// + public static TiledColor PapayaWhip => new TiledColor(0xFF, 0xEF, 0xD5); + + /// + /// Peach Puff (A: 255, R:255, G:218, B:185) + /// + public static TiledColor PeachPuff => new TiledColor(0xFF, 0xDA, 0xB9); + + /// + /// Peru (A: 255, R:205, G:133, B:63) + /// + public static TiledColor Peru => new TiledColor(0xCD, 0x85, 0x3F); + + /// + /// Pink (A: 255, R:255, G:192, B:203) + /// + public static TiledColor Pink => new TiledColor(0xFF, 0xC0, 0xCB); + + /// + /// Plum (A: 255, R:221, G:160, B:221) + /// + public static TiledColor Plum => new TiledColor(0xDD, 0xA0, 0xDD); + + /// + /// Powder Blue (A: 255, R:176, G:224, B:230) + /// + public static TiledColor PowderBlue => new TiledColor(0xB0, 0xE0, 0xE6); + + /// + /// Purple (A: 255, R:128, G:0, B:128) + /// + public static TiledColor Purple => new TiledColor(0x80, 0x00, 0x80); + + /// + /// Rebecca Purple (A: 255, R:102, G:51, B:153) + /// + public static TiledColor RebeccaPurple => new TiledColor(0x66, 0x33, 0x99); + + /// + /// Red (A: 255, R:255, G:0, B:0) + /// + public static TiledColor Red => new TiledColor(0xFF, 0x00, 0x00); + + /// + /// Rosy Brown (A: 255, R:188, G:143, B:143) + /// + public static TiledColor RosyBrown => new TiledColor(0xBC, 0x8F, 0x8F); + + /// + /// Royal Blue (A: 255, R:65, G:105, B:225) + /// + public static TiledColor RoyalBlue => new TiledColor(0x41, 0x69, 0xE1); + + /// + /// Saddle Brown (A: 255, R:139, G:69, B:19) + /// + public static TiledColor SaddleBrown => new TiledColor(0x8B, 0x45, 0x13); + + /// + /// Salmon (A: 255, R:250, G:128, B:114) + /// + public static TiledColor Salmon => new TiledColor(0xFA, 0x80, 0x72); + + /// + /// Sandy Brown (A: 255, R:244, G:164, B:96) + /// + public static TiledColor SandyBrown => new TiledColor(0xF4, 0xA4, 0x60); + + /// + /// Sea Green (A: 255, R:46, G:139, B:87) + /// + public static TiledColor SeaGreen => new TiledColor(0x2E, 0x8B, 0x57); + + /// + /// Sea Shell (A: 255, R:255, G:245, B:238) + /// + public static TiledColor SeaShell => new TiledColor(0xFF, 0xF5, 0xEE); + + /// + /// Sienna (A: 255, R:160, G:82, B:45) + /// + public static TiledColor Sienna => new TiledColor(0xA0, 0x52, 0x2D); + + /// + /// Silver (A: 255, R:192, G:192, B:192) + /// + public static TiledColor Silver => new TiledColor(0xC0, 0xC0, 0xC0); + + /// + /// Sky Blue (A: 255, R:135, G:206, B:235) + /// + public static TiledColor SkyBlue => new TiledColor(0x87, 0xCE, 0xEB); + + /// + /// Slate Blue (A: 255, R:106, G:90, B:205) + /// + public static TiledColor SlateBlue => new TiledColor(0x6A, 0x5A, 0xCD); + + /// + /// Slate Gray (A: 255, R:112, G:128, B:144) + /// + public static TiledColor SlateGray => new TiledColor(0x70, 0x80, 0x90); + + /// + /// Slate Grey (A: 255, R:112, G:128, B:144) + /// + public static TiledColor SlateGrey => new TiledColor(0x70, 0x80, 0x90); + + /// + /// Snow (A: 255, R:255, G:250, B:250) + /// + public static TiledColor Snow => new TiledColor(0xFF, 0xFA, 0xFA); + + /// + /// Spring Green (A: 255, R:0, G:255, B:127) + /// + public static TiledColor SpringGreen => new TiledColor(0x00, 0xFF, 0x7F); + + /// + /// Steel Blue (A: 255, R:70, G:130, B:180) + /// + public static TiledColor SteelBlue => new TiledColor(0x46, 0x82, 0xB4); + + /// + /// Tan (A: 255, R:210, G:180, B:140) + /// + public static TiledColor Tan => new TiledColor(0xD2, 0xB4, 0x8C); + + /// + /// Teal (A: 255, R:0, G:128, B:128) + /// + public static TiledColor Teal => new TiledColor(0x00, 0x80, 0x80); + + /// + /// Thistle (A: 255, R:216, G:191, B:216) + /// + public static TiledColor Thistle => new TiledColor(0xD8, 0xBF, 0xD8); + + /// + /// Tomato (A: 255, R:255, G:99, B:71) + /// + public static TiledColor Tomato => new TiledColor(0xFF, 0x63, 0x47); + + /// + /// Turquoise (A: 255, R:64, G:224, B:208) + /// + public static TiledColor Turquoise => new TiledColor(0x40, 0xE0, 0xD0); + + /// + /// Violet (A: 255, R:238, G:130, B:238) + /// + public static TiledColor Violet => new TiledColor(0xEE, 0x82, 0xEE); + + /// + /// Wheat (A: 255, R:245, G:222, B:179) + /// + public static TiledColor Wheat => new TiledColor(0xF5, 0xDE, 0xB3); + + /// + /// White (A: 255, R:255, G:255, B:255) + /// + public static TiledColor White => new TiledColor(0xFF, 0xFF, 0xFF); + + /// + /// White Smoke (A: 255, R:245, G:245, B:245) + /// + public static TiledColor WhiteSmoke => new TiledColor(0xF5, 0xF5, 0xF5); + + /// + /// Yellow (A: 255, R:255, G:255, B:0) + /// + public static TiledColor Yellow => new TiledColor(0xFF, 0xFF, 0x00); + + /// + /// Yellow Green (A: 255, R:154, G:205, B:50) + /// + public static TiledColor YellowGreen => new TiledColor(0x9A, 0xCD, 0x32); + + + + #endregion Static_TiledColors + + /// + /// Constructs an ARGB color from scalars representing red, green, and blue values. + /// + /// + /// + /// + public TiledColor(byte red, byte green, byte blue) : this(0xFF, red, green, blue) { } + + /// + /// Constructs an ARGB color from scalars representing alpha, red, green, and blue values. + /// + /// + /// + /// + /// + public TiledColor(byte alpha, byte red, byte green, byte blue) + { + A = alpha; + R = red; + G = green; + B = blue; + } + + /// + /// Gets or sets the alpha component. + /// + [field: FieldOffset(0)] + public byte A { get; set; } + + /// + /// Gets or sets the red component. + /// + [field: FieldOffset(1)] + public byte R { get; set; } + + /// + /// Gets or sets the green component. + /// + [field: FieldOffset(2)] + public byte G { get; set; } + + /// + /// Gets or sets the blue component. + /// + [field: FieldOffset(3)] + public byte B { get; set; } /// /// Attempts to parse the specified string into a . Expects strings in the format #RRGGBB or #AARRGGBB. @@ -39,8 +819,10 @@ public class TiledColor : IParsable, IEquatable /// Thrown in case the provided string is not in a valid format. public static TiledColor Parse(string s, IFormatProvider provider) { - _ = TryParse(s, provider, out var result); - return result ?? throw new FormatException($"Invalid format for TiledColor: {s}"); + if (TryParse(s, provider, out var result)) + return result; + + throw new FormatException($"Invalid format for TiledColor: {s}"); } /// @@ -68,42 +850,74 @@ public class TiledColor : IParsable, IEquatable if (s.Length == 7) { - result = new TiledColor - { - R = byte.Parse(s[1..3], NumberStyles.HexNumber, provider), - G = byte.Parse(s[3..5], NumberStyles.HexNumber, provider), - B = byte.Parse(s[5..7], NumberStyles.HexNumber, provider) - }; + result = new TiledColor( + byte.Parse(s[1..3], NumberStyles.HexNumber, provider), + byte.Parse(s[3..5], NumberStyles.HexNumber, provider), + byte.Parse(s[5..7], NumberStyles.HexNumber, provider) + ); } else { - result = new TiledColor - { - A = byte.Parse(s[1..3], NumberStyles.HexNumber, provider), - R = byte.Parse(s[3..5], NumberStyles.HexNumber, provider), - G = byte.Parse(s[5..7], NumberStyles.HexNumber, provider), - B = byte.Parse(s[7..9], NumberStyles.HexNumber, provider) - }; + result = new TiledColor( + byte.Parse(s[1..3], NumberStyles.HexNumber, provider), + byte.Parse(s[3..5], NumberStyles.HexNumber, provider), + byte.Parse(s[5..7], NumberStyles.HexNumber, provider), + byte.Parse(s[7..9], NumberStyles.HexNumber, provider) + ); } return true; } - /// - public bool Equals(TiledColor other) + /// + /// Compares whether two instances are equal. + /// + /// instance on the left of the equal sign. + /// instance on the right of the equal sign. + /// true if the instances are equal; false otherwise. + public static bool operator ==(TiledColor left, TiledColor right) { - if (other is null) - return false; + return left.A == right.A && left.R == right.R && left.G == left.G && left.B == left.B; + } - return R == other.R && G == other.G && B == other.B && A == other.A; + /// + /// Compares where two instances are not equal. + /// + /// instance on the left of the not equal sign. + /// instance on the right of the not equal sign. + /// true if the instances are not equal; false otherwise. + public static bool operator !=(TiledColor left, TiledColor right) + { + return left.A != right.A || left.R != right.R || left.G != right.G || left.B != right.B; } /// - public override bool Equals(object obj) => obj is TiledColor other && Equals(other); + public readonly bool Equals(TiledColor other) => A == other.A && R == other.R && G == other.G && B == other.B; /// - public override int GetHashCode() => HashCode.Combine(R, G, B, A); + public override readonly bool Equals(object obj) => obj is TiledColor other && Equals(other); /// - public override string ToString() => $"#{A:x2}{R:x2}{G:x2}{B:x2}"; + public override readonly int GetHashCode() => (A, R, G, B).GetHashCode(); + + /// + public override readonly string ToString() => $"#{A:x2}{R:x2}{G:x2}{B:x2}"; + + /// + /// Explicit conversion from a to a . + /// + /// + public static explicit operator uint(TiledColor value) + { + return ((uint)value.A << 24) | ((uint)value.R << 16) | ((uint)value.G << 8) | ((uint)value.B << 0); + } + + /// + /// Explicit conversion from a to a . + /// + /// + public static explicit operator TiledColor(uint value) + { + return new((byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)(value >> 0)); + } } diff --git a/src/DotTiled/Serialization/Tmj/TmjReaderBase.Map.cs b/src/DotTiled/Serialization/Tmj/TmjReaderBase.Map.cs index dfe7eee..5480681 100644 --- a/src/DotTiled/Serialization/Tmj/TmjReaderBase.Map.cs +++ b/src/DotTiled/Serialization/Tmj/TmjReaderBase.Map.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Globalization; using System.Text.Json; namespace DotTiled.Serialization.Tmj; @@ -47,7 +46,7 @@ public abstract partial class TmjReaderBase }); var parallaxOriginX = element.GetOptionalProperty("parallaxoriginx").GetValueOr(0f); var parallaxOriginY = element.GetOptionalProperty("parallaxoriginy").GetValueOr(0f); - var backgroundColor = element.GetOptionalPropertyParseable("backgroundcolor").GetValueOr(TiledColor.Parse("#00000000", CultureInfo.InvariantCulture)); + var backgroundColor = element.GetOptionalPropertyParseable("backgroundcolor").GetValueOr(TiledColor.Transparent); var nextLayerID = element.GetRequiredProperty("nextlayerid"); var nextObjectID = element.GetRequiredProperty("nextobjectid"); var infinite = element.GetOptionalProperty("infinite").GetValueOr(false); diff --git a/src/DotTiled/Serialization/Tmj/TmjReaderBase.ObjectLayer.cs b/src/DotTiled/Serialization/Tmj/TmjReaderBase.ObjectLayer.cs index 6eb5f8a..d5f47fb 100644 --- a/src/DotTiled/Serialization/Tmj/TmjReaderBase.ObjectLayer.cs +++ b/src/DotTiled/Serialization/Tmj/TmjReaderBase.ObjectLayer.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Numerics; using System.Text.Json; @@ -257,7 +256,7 @@ public abstract partial class TmjReaderBase internal static TextObject ReadText(JsonElement element) { var bold = element.GetOptionalProperty("bold").GetValueOr(false); - var color = element.GetOptionalPropertyParseable("color").GetValueOr(TiledColor.Parse("#000000", CultureInfo.InvariantCulture)); + var color = element.GetOptionalPropertyParseable("color").GetValueOr(TiledColor.Black); var fontfamily = element.GetOptionalProperty("fontfamily").GetValueOr("sans-serif"); var halign = element.GetOptionalPropertyParseable("halign", s => s switch { diff --git a/src/DotTiled/Serialization/Tmj/TmjReaderBase.Properties.cs b/src/DotTiled/Serialization/Tmj/TmjReaderBase.Properties.cs index 1ad6e53..2daf767 100644 --- a/src/DotTiled/Serialization/Tmj/TmjReaderBase.Properties.cs +++ b/src/DotTiled/Serialization/Tmj/TmjReaderBase.Properties.cs @@ -36,7 +36,7 @@ public abstract partial class TmjReaderBase PropertyType.Int => new IntProperty { Name = name, Value = e.GetRequiredProperty("value") }, PropertyType.Float => new FloatProperty { Name = name, Value = e.GetRequiredProperty("value") }, PropertyType.Bool => new BoolProperty { Name = name, Value = e.GetRequiredProperty("value") }, - PropertyType.Color => new ColorProperty { Name = name, Value = e.GetRequiredPropertyParseable("value", s => s == "" ? default : TiledColor.Parse(s, CultureInfo.InvariantCulture)) }, + PropertyType.Color => new ColorProperty { Name = name, Value = e.GetRequiredPropertyParseable>("value", s => s == "" ? new Optional() : TiledColor.Parse(s, CultureInfo.InvariantCulture)) }, PropertyType.File => new FileProperty { Name = name, Value = e.GetRequiredProperty("value") }, PropertyType.Object => new ObjectProperty { Name = name, Value = e.GetRequiredProperty("value") }, PropertyType.Class => throw new JsonException("Class property must have a property type"), diff --git a/src/DotTiled/Serialization/Tmx/TmxReaderBase.Map.cs b/src/DotTiled/Serialization/Tmx/TmxReaderBase.Map.cs index 9f48633..813a286 100644 --- a/src/DotTiled/Serialization/Tmx/TmxReaderBase.Map.cs +++ b/src/DotTiled/Serialization/Tmx/TmxReaderBase.Map.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; namespace DotTiled.Serialization.Tmx; @@ -51,7 +50,7 @@ public abstract partial class TmxReaderBase }); var parallaxOriginX = _reader.GetOptionalAttributeParseable("parallaxoriginx").GetValueOr(0.0f); var parallaxOriginY = _reader.GetOptionalAttributeParseable("parallaxoriginy").GetValueOr(0.0f); - var backgroundColor = _reader.GetOptionalAttributeClass("backgroundcolor").GetValueOr(TiledColor.Parse("#00000000", CultureInfo.InvariantCulture)); + var backgroundColor = _reader.GetOptionalAttributeParseable("backgroundcolor").GetValueOr(TiledColor.Transparent); var nextLayerID = _reader.GetRequiredAttributeParseable("nextlayerid"); var nextObjectID = _reader.GetRequiredAttributeParseable("nextobjectid"); var infinite = _reader.GetOptionalAttributeParseable("infinite").GetValueOr(0) == 1; diff --git a/src/DotTiled/Serialization/Tmx/TmxReaderBase.ObjectLayer.cs b/src/DotTiled/Serialization/Tmx/TmxReaderBase.ObjectLayer.cs index b6fa3dc..5a3ec0c 100644 --- a/src/DotTiled/Serialization/Tmx/TmxReaderBase.ObjectLayer.cs +++ b/src/DotTiled/Serialization/Tmx/TmxReaderBase.ObjectLayer.cs @@ -20,12 +20,12 @@ public abstract partial class TmxReaderBase var height = _reader.GetOptionalAttributeParseable("height").GetValueOr(0); var opacity = _reader.GetOptionalAttributeParseable("opacity").GetValueOr(1.0f); var visible = _reader.GetOptionalAttributeParseable("visible").GetValueOr(1) == 1; - var tintColor = _reader.GetOptionalAttributeClass("tintcolor"); + var tintColor = _reader.GetOptionalAttributeParseable("tintcolor"); var offsetX = _reader.GetOptionalAttributeParseable("offsetx").GetValueOr(0.0f); var offsetY = _reader.GetOptionalAttributeParseable("offsety").GetValueOr(0.0f); var parallaxX = _reader.GetOptionalAttributeParseable("parallaxx").GetValueOr(1.0f); var parallaxY = _reader.GetOptionalAttributeParseable("parallaxy").GetValueOr(1.0f); - var color = _reader.GetOptionalAttributeClass("color"); + var color = _reader.GetOptionalAttributeParseable("color"); var drawOrder = _reader.GetOptionalAttributeEnum("draworder", s => s switch { "topdown" => DrawOrder.TopDown, @@ -245,7 +245,7 @@ public abstract partial class TmxReaderBase var fontFamily = _reader.GetOptionalAttribute("fontfamily").GetValueOr("sans-serif"); var pixelSize = _reader.GetOptionalAttributeParseable("pixelsize").GetValueOr(16); var wrap = _reader.GetOptionalAttributeParseable("wrap").GetValueOr(0) == 1; - var color = _reader.GetOptionalAttributeClass("color").GetValueOr(TiledColor.Parse("#000000", CultureInfo.InvariantCulture)); + var color = _reader.GetOptionalAttributeParseable("color").GetValueOr(TiledColor.Black); var bold = _reader.GetOptionalAttributeParseable("bold").GetValueOr(0) == 1; var italic = _reader.GetOptionalAttributeParseable("italic").GetValueOr(0) == 1; var underline = _reader.GetOptionalAttributeParseable("underline").GetValueOr(0) == 1; diff --git a/src/DotTiled/Serialization/Tmx/TmxReaderBase.Properties.cs b/src/DotTiled/Serialization/Tmx/TmxReaderBase.Properties.cs index 94b730b..73abfb8 100644 --- a/src/DotTiled/Serialization/Tmx/TmxReaderBase.Properties.cs +++ b/src/DotTiled/Serialization/Tmx/TmxReaderBase.Properties.cs @@ -45,7 +45,7 @@ public abstract partial class TmxReaderBase PropertyType.Int => new IntProperty { Name = name, Value = r.GetRequiredAttributeParseable("value") }, PropertyType.Float => new FloatProperty { Name = name, Value = r.GetRequiredAttributeParseable("value") }, PropertyType.Bool => new BoolProperty { Name = name, Value = r.GetRequiredAttributeParseable("value") }, - PropertyType.Color => new ColorProperty { Name = name, Value = r.GetRequiredAttributeParseable("value", s => s == "" ? default : TiledColor.Parse(s, CultureInfo.InvariantCulture)) }, + PropertyType.Color => new ColorProperty { Name = name, Value = r.GetRequiredAttributeParseable>("value", s => s == "" ? new Optional() : TiledColor.Parse(s, CultureInfo.InvariantCulture)) }, PropertyType.File => new FileProperty { Name = name, Value = r.GetRequiredAttribute("value") }, PropertyType.Object => new ObjectProperty { Name = name, Value = r.GetRequiredAttributeParseable("value") }, PropertyType.Class => throw new XmlException("Class property must have a property type"), diff --git a/src/DotTiled/Serialization/Tmx/TmxReaderBase.TileLayer.cs b/src/DotTiled/Serialization/Tmx/TmxReaderBase.TileLayer.cs index fc7fcff..64e492a 100644 --- a/src/DotTiled/Serialization/Tmx/TmxReaderBase.TileLayer.cs +++ b/src/DotTiled/Serialization/Tmx/TmxReaderBase.TileLayer.cs @@ -16,7 +16,7 @@ public abstract partial class TmxReaderBase var height = _reader.GetRequiredAttributeParseable("height"); var opacity = _reader.GetOptionalAttributeParseable("opacity").GetValueOr(1.0f); var visible = _reader.GetOptionalAttributeParseable("visible").GetValueOr(1) == 1; - var tintColor = _reader.GetOptionalAttributeClass("tintcolor"); + var tintColor = _reader.GetOptionalAttributeParseable("tintcolor"); var offsetX = _reader.GetOptionalAttributeParseable("offsetx").GetValueOr(0.0f); var offsetY = _reader.GetOptionalAttributeParseable("offsety").GetValueOr(0.0f); var parallaxX = _reader.GetOptionalAttributeParseable("parallaxx").GetValueOr(1.0f); @@ -63,7 +63,7 @@ public abstract partial class TmxReaderBase var y = _reader.GetOptionalAttributeParseable("y").GetValueOr(0); var opacity = _reader.GetOptionalAttributeParseable("opacity").GetValueOr(1f); var visible = _reader.GetOptionalAttributeParseable("visible").GetValueOr(true); - var tintColor = _reader.GetOptionalAttributeClass("tintcolor"); + var tintColor = _reader.GetOptionalAttributeParseable("tintcolor"); var offsetX = _reader.GetOptionalAttributeParseable("offsetx").GetValueOr(0.0f); var offsetY = _reader.GetOptionalAttributeParseable("offsety").GetValueOr(0.0f); var parallaxX = _reader.GetOptionalAttributeParseable("parallaxx").GetValueOr(1.0f); @@ -110,7 +110,7 @@ public abstract partial class TmxReaderBase var @class = _reader.GetOptionalAttribute("class").GetValueOr(""); var opacity = _reader.GetOptionalAttributeParseable("opacity").GetValueOr(1.0f); var visible = _reader.GetOptionalAttributeParseable("visible").GetValueOr(1) == 1; - var tintColor = _reader.GetOptionalAttributeClass("tintcolor"); + var tintColor = _reader.GetOptionalAttributeParseable("tintcolor"); var offsetX = _reader.GetOptionalAttributeParseable("offsetx").GetValueOr(0f); var offsetY = _reader.GetOptionalAttributeParseable("offsety").GetValueOr(0f); var parallaxX = _reader.GetOptionalAttributeParseable("parallaxx").GetValueOr(1f); diff --git a/src/DotTiled/Serialization/Tmx/TmxReaderBase.Tileset.cs b/src/DotTiled/Serialization/Tmx/TmxReaderBase.Tileset.cs index 20238ec..445581f 100644 --- a/src/DotTiled/Serialization/Tmx/TmxReaderBase.Tileset.cs +++ b/src/DotTiled/Serialization/Tmx/TmxReaderBase.Tileset.cs @@ -125,7 +125,7 @@ public abstract partial class TmxReaderBase _ => throw new InvalidOperationException($"Unknown image format '{s}'") }); var source = _reader.GetOptionalAttribute("source"); - var transparentColor = _reader.GetOptionalAttributeClass("trans"); + var transparentColor = _reader.GetOptionalAttributeParseable("trans"); var width = _reader.GetOptionalAttributeParseable("width"); var height = _reader.GetOptionalAttributeParseable("height");