From adf087178d773aed40e7f8eb815342f008d409c8 Mon Sep 17 00:00:00 2001 From: 7H3LaughingMan <7H3LaughingMan@proton.me> Date: Thu, 1 May 2025 16:52:25 -0500 Subject: [PATCH] TiledColor Struct --- .../UnitTests/TiledColorTests.cs | 225 +++++ src/DotTiled/Map.cs | 3 +- src/DotTiled/Properties/TiledColor.cs | 888 +++++++++++++++++- .../Serialization/Tmj/TmjReaderBase.Map.cs | 3 +- .../Tmj/TmjReaderBase.ObjectLayer.cs | 3 +- .../Tmj/TmjReaderBase.Properties.cs | 2 +- .../Serialization/Tmx/TmxReaderBase.Map.cs | 3 +- .../Tmx/TmxReaderBase.ObjectLayer.cs | 6 +- .../Tmx/TmxReaderBase.Properties.cs | 2 +- .../Tmx/TmxReaderBase.TileLayer.cs | 6 +- .../Tmx/TmxReaderBase.Tileset.cs | 2 +- 11 files changed, 1092 insertions(+), 51 deletions(-) create mode 100644 src/DotTiled.Tests/UnitTests/TiledColorTests.cs diff --git a/src/DotTiled.Tests/UnitTests/TiledColorTests.cs b/src/DotTiled.Tests/UnitTests/TiledColorTests.cs new file mode 100644 index 0000000..11cba9d --- /dev/null +++ b/src/DotTiled.Tests/UnitTests/TiledColorTests.cs @@ -0,0 +1,225 @@ +using System.Globalization; + +namespace DotTiled.Tests.UnitTests; + +public class TiledColorTests +{ + [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 PackedValueConstructor() + { + var tiledColor = new TiledColor(0x11223344); + + Assert.Equal(0x11, tiledColor.A); + Assert.Equal(0x22, tiledColor.R); + Assert.Equal(0x33, tiledColor.G); + Assert.Equal(0x44, tiledColor.B); + } + + [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..c3cfd3d 100644 --- a/src/DotTiled/Properties/TiledColor.cs +++ b/src/DotTiled/Properties/TiledColor.cs @@ -7,27 +7,831 @@ namespace DotTiled; /// /// Represents a Tiled color. /// -public class TiledColor : IParsable, IEquatable +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(0xFFF0F8FF); /// - /// 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(0xFFFAEBD7); + + /// + /// Aqua (A: 255, R:0, G:255, B: 255) + /// + public static TiledColor Aqua => new TiledColor(0xFF00FFFF); + + /// + /// Aquamarine (A: 255, R:127, G:255, B: 212) + /// + public static TiledColor Aquamarine => new TiledColor(0xFF7FFFD4); + + /// + /// Azure (A: 255, R:240, G:255, B: 255) + /// + public static TiledColor Azure => new TiledColor(0xFFF0FFFF); + + /// + /// Beige (A: 255, R:245, G:245, B: 220) + /// + public static TiledColor Beige => new TiledColor(0xFFF5F5DC); + + /// + /// Bisque (A: 255, R:255, G:228, B: 196) + /// + public static TiledColor Bisque => new TiledColor(0xFFFFE4C4); + + /// + /// Black (A: 255, R:0, G:0, B: 0) + /// + public static TiledColor Black => new TiledColor(0xFF000000); + + /// + /// Blanched Almond (A: 255, R:255, G:235, B: 205) + /// + public static TiledColor BlanchedAlmond => new TiledColor(0xFFFFEBCD); + + /// + /// Blue (A: 255, R:0, G:0, B: 255) + /// + public static TiledColor Blue => new TiledColor(0xFF0000FF); + + /// + /// Blue Violet (A: 255, R:138, G:43, B: 226) + /// + public static TiledColor BlueViolet => new TiledColor(0xFF8A2BE2); + + /// + /// Brown (A: 255, R:165, G:42, B: 42) + /// + public static TiledColor Brown => new TiledColor(0xFFA52A2A); + + /// + /// Burly Wood (A: 255, R:222, G:184, B: 135) + /// + public static TiledColor BurlyWood => new TiledColor(0xFFDEB887); + + /// + /// Cadet Blue (A: 255, R:95, G:158, B: 160) + /// + public static TiledColor CadetBlue => new TiledColor(0xFF5F9EA0); + + /// + /// Chartreuse (A: 255, R:127, G:255, B: 0) + /// + public static TiledColor Chartreuse => new TiledColor(0xFF7FFF00); + + /// + /// Chocolate (A: 255, R:210, G:105, B: 30) + /// + public static TiledColor Chocolate => new TiledColor(0xFFD2691E); + + /// + /// Coral (A: 255, R:255, G:127, B: 80) + /// + public static TiledColor Coral => new TiledColor(0xFFFF7F50); + + /// + /// Cornflower Blue (A: 255, R:100, G:149, B: 237) + /// + public static TiledColor CornflowerBlue => new TiledColor(0xFF6495ED); + + /// + /// Cornsilk (A: 255, R:255, G:248, B: 220) + /// + public static TiledColor Cornsilk => new TiledColor(0xFFFFF8DC); + + /// + /// Crimson (A: 255, R:220, G:20, B: 60) + /// + public static TiledColor Crimson => new TiledColor(0xFFDC143C); + + /// + /// Cyan (A: 255, R:0, G:255, B: 255) + /// + public static TiledColor Cyan => new TiledColor(0xFF00FFFF); + + /// + /// Dark Blue (A: 255, R:0, G:0, B: 139) + /// + public static TiledColor DarkBlue => new TiledColor(0xFF00008B); + + /// + /// Dark Cyan (A: 255, R:0, G:139, B: 139) + /// + public static TiledColor DarkCyan => new TiledColor(0xFF008B8B); + + /// + /// Dark Golden Rod (A: 255, R:184, G:134, B: 11) + /// + public static TiledColor DarkGoldenRod => new TiledColor(0xFFB8860B); + + /// + /// Dark Gray (A: 255, R:169, G:169, B: 169) + /// + public static TiledColor DarkGray => new TiledColor(0xFFA9A9A9); + + /// + /// Dark Grey (A: 255, R:169, G:169, B: 169) + /// + public static TiledColor DarkGrey => new TiledColor(0xFFA9A9A9); + + /// + /// Dark Green (A: 255, R:0, G:100, B: 0) + /// + public static TiledColor DarkGreen => new TiledColor(0xFF006400); + + /// + /// Dark Khaki (A: 255, R:189, G:183, B: 107) + /// + public static TiledColor DarkKhaki => new TiledColor(0xFFBDB76B); + + /// + /// Dark Magenta (A: 255, R:139, G:0, B: 139) + /// + public static TiledColor DarkMagenta => new TiledColor(0xFF8B008B); + + /// + /// Dark Olive Green (A: 255, R:85, G:107, B: 47) + /// + public static TiledColor DarkOliveGreen => new TiledColor(0xFF556B2F); + + /// + /// Dark Orange (A: 255, R:255, G:140, B: 0) + /// + public static TiledColor DarkOrange => new TiledColor(0xFFFF8C00); + + /// + /// Dark Orchid (A: 255, R:153, G:50, B: 204) + /// + public static TiledColor DarkOrchid => new TiledColor(0xFF9932CC); + + /// + /// Dark Red (A: 255, R:139, G:0, B: 0) + /// + public static TiledColor DarkRed => new TiledColor(0xFF8B0000); + + /// + /// Dark Salmon (A: 255, R:233, G:150, B: 122) + /// + public static TiledColor DarkSalmon => new TiledColor(0xFFE9967A); + + /// + /// Dark Sea Green (A: 255, R:143, G:188, B: 143) + /// + public static TiledColor DarkSeaGreen => new TiledColor(0xFF8FBC8F); + + /// + /// Dark Slate Blue (A: 255, R:72, G:61, B: 139) + /// + public static TiledColor DarkSlateBlue => new TiledColor(0xFF483D8B); + + /// + /// Dark Slate Gray (A: 255, R:47, G:79, B: 79) + /// + public static TiledColor DarkSlateGray => new TiledColor(0xFF2F4F4F); + + /// + /// Dark Slate Grey (A: 255, R:47, G:79, B: 79) + /// + public static TiledColor DarkSlateGrey => new TiledColor(0xFF2F4F4F); + + /// + /// Dark Turquoise (A: 255, R:0, G:206, B: 209) + /// + public static TiledColor DarkTurquoise => new TiledColor(0xFF00CED1); + + /// + /// Dark Violet (A: 255, R:148, G:0, B: 211) + /// + public static TiledColor DarkViolet => new TiledColor(0xFF9400D3); + + /// + /// Deep Pink (A: 255, R:255, G:20, B: 147) + /// + public static TiledColor DeepPink => new TiledColor(0xFFFF1493); + + /// + /// Deep Sky Blue (A: 255, R:0, G:191, B: 255) + /// + public static TiledColor DeepSkyBlue => new TiledColor(0xFF00BFFF); + + /// + /// Dim Gray (A: 255, R:105, G:105, B: 105) + /// + public static TiledColor DimGray => new TiledColor(0xFF696969); + + /// + /// Dim Grey (A: 255, R:105, G:105, B: 105) + /// + public static TiledColor DimGrey => new TiledColor(0xFF696969); + + /// + /// Dodger Blue (A: 255, R:30, G:144, B: 255) + /// + public static TiledColor DodgerBlue => new TiledColor(0xFF1E90FF); + + /// + /// Fire Brick (A: 255, R:178, G:34, B: 34) + /// + public static TiledColor FireBrick => new TiledColor(0xFFB22222); + + /// + /// Floral White (A: 255, R:255, G:250, B: 240) + /// + public static TiledColor FloralWhite => new TiledColor(0xFFFFFAF0); + + /// + /// Forest Green (A: 255, R:34, G:139, B: 34) + /// + public static TiledColor ForestGreen => new TiledColor(0xFF228B22); + + /// + /// Fuchsia (A: 255, R:255, G:0, B: 255) + /// + public static TiledColor Fuchsia => new TiledColor(0xFFFF00FF); + + /// + /// Gainsboro (A: 255, R:220, G:220, B: 220) + /// + public static TiledColor Gainsboro => new TiledColor(0xFFDCDCDC); + + /// + /// Ghost White (A: 255, R:248, G:248, B: 255) + /// + public static TiledColor GhostWhite => new TiledColor(0xFFF8F8FF); + + /// + /// Gold (A: 255, R:255, G:215, B: 0) + /// + public static TiledColor Gold => new TiledColor(0xFFFFD700); + + /// + /// Golden Rod (A: 255, R:218, G:165, B: 32) + /// + public static TiledColor GoldenRod => new TiledColor(0xFFDAA520); + + /// + /// Gray (A: 255, R:128, G:128, B: 128) + /// + public static TiledColor Gray => new TiledColor(0xFF808080); + + /// + /// Grey (A: 255, R:128, G:128, B: 128) + /// + public static TiledColor Grey => new TiledColor(0xFF808080); + + /// + /// Green (A: 255, R:0, G:128, B: 0) + /// + public static TiledColor Green => new TiledColor(0xFF008000); + + /// + /// Green Yellow (A: 255, R:173, G:255, B: 47) + /// + public static TiledColor GreenYellow => new TiledColor(0xFFADFF2F); + + /// + /// Honey Dew (A: 255, R:240, G:255, B: 240) + /// + public static TiledColor HoneyDew => new TiledColor(0xFFF0FFF0); + + /// + /// Hot Pink (A: 255, R:255, G:105, B: 180) + /// + public static TiledColor HotPink => new TiledColor(0xFFFF69B4); + + /// + /// Indian Red (A: 255, R:205, G:92, B: 92) + /// + public static TiledColor IndianRed => new TiledColor(0xFFCD5C5C); + + /// + /// Indigo (A: 255, R:75, G:0, B: 130) + /// + public static TiledColor Indigo => new TiledColor(0xFF4B0082); + + /// + /// Ivory (A: 255, R:255, G:255, B: 240) + /// + public static TiledColor Ivory => new TiledColor(0xFFFFFFF0); + + /// + /// Khaki (A: 255, R:240, G:230, B: 140) + /// + public static TiledColor Khaki => new TiledColor(0xFFF0E68C); + + /// + /// Lavender (A: 255, R:230, G:230, B: 250) + /// + public static TiledColor Lavender => new TiledColor(0xFFE6E6FA); + + /// + /// Lavender Blush (A: 255, R:255, G:240, B: 245) + /// + public static TiledColor LavenderBlush => new TiledColor(0xFFFFF0F5); + + /// + /// Lawn Green (A: 255, R:124, G:252, B: 0) + /// + public static TiledColor LawnGreen => new TiledColor(0xFF7CFC00); + + /// + /// Lemon Chiffon (A: 255, R:255, G:250, B: 205) + /// + public static TiledColor LemonChiffon => new TiledColor(0xFFFFFACD); + + /// + /// Light Blue (A: 255, R:173, G:216, B: 230) + /// + public static TiledColor LightBlue => new TiledColor(0xFFADD8E6); + + /// + /// Light Coral (A: 255, R:240, G:128, B: 128) + /// + public static TiledColor LightCoral => new TiledColor(0xFFF08080); + + /// + /// Light Cyan (A: 255, R:224, G:255, B: 255) + /// + public static TiledColor LightCyan => new TiledColor(0xFFE0FFFF); + + /// + /// Light Golden Rod Yellow (A: 255, R:250, G:250, B: 210) + /// + public static TiledColor LightGoldenRodYellow => new TiledColor(0xFFFAFAD2); + + /// + /// Light Gray (A: 255, R:211, G:211, B: 211) + /// + public static TiledColor LightGray => new TiledColor(0xFFD3D3D3); + + /// + /// Light Grey (A: 255, R:211, G:211, B: 211) + /// + public static TiledColor LightGrey => new TiledColor(0xFFD3D3D3); + + /// + /// Light Green (A: 255, R:144, G:238, B: 144) + /// + public static TiledColor LightGreen => new TiledColor(0xFF90EE90); + + /// + /// Light Pink (A: 255, R:255, G:182, B: 193) + /// + public static TiledColor LightPink => new TiledColor(0xFFFFB6C1); + + /// + /// Light Salmon (A: 255, R:255, G:160, B: 122) + /// + public static TiledColor LightSalmon => new TiledColor(0xFFFFA07A); + + /// + /// Light Sea Green (A: 255, R:32, G:178, B: 170) + /// + public static TiledColor LightSeaGreen => new TiledColor(0xFF20B2AA); + + /// + /// Light Sky Blue (A: 255, R:135, G:206, B: 250) + /// + public static TiledColor LightSkyBlue => new TiledColor(0xFF87CEFA); + + /// + /// Light Slate Gray (A: 255, R:119, G:136, B: 153) + /// + public static TiledColor LightSlateGray => new TiledColor(0xFF778899); + + /// + /// Light Slate Grey (A: 255, R:119, G:136, B: 153) + /// + public static TiledColor LightSlateGrey => new TiledColor(0xFF778899); + + /// + /// Light Steel Blue (A: 255, R:176, G:196, B: 222) + /// + public static TiledColor LightSteelBlue => new TiledColor(0xFFB0C4DE); + + /// + /// Light Yellow (A: 255, R:255, G:255, B: 224) + /// + public static TiledColor LightYellow => new TiledColor(0xFFFFFFE0); + + /// + /// Lime (A: 255, R:0, G:255, B: 0) + /// + public static TiledColor Lime => new TiledColor(0xFF00FF00); + + /// + /// Lime Green (A: 255, R:50, G:205, B: 50) + /// + public static TiledColor LimeGreen => new TiledColor(0xFF32CD32); + + /// + /// Linen (A: 255, R:250, G:240, B: 230) + /// + public static TiledColor Linen => new TiledColor(0xFFFAF0E6); + + /// + /// Magenta (A: 255, R:255, G:0, B: 255) + /// + public static TiledColor Magenta => new TiledColor(0xFFFF00FF); + + /// + /// Maroon (A: 255, R:128, G:0, B: 0) + /// + public static TiledColor Maroon => new TiledColor(0xFF800000); + + /// + /// Medium Aqua Marine (A: 255, R:102, G:205, B: 170) + /// + public static TiledColor MediumAquaMarine => new TiledColor(0xFF66CDAA); + + /// + /// Medium Blue (A: 255, R:0, G:0, B: 205) + /// + public static TiledColor MediumBlue => new TiledColor(0xFF0000CD); + + /// + /// Medium Orchid (A: 255, R:186, G:85, B: 211) + /// + public static TiledColor MediumOrchid => new TiledColor(0xFFBA55D3); + + /// + /// Medium Purple (A: 255, R:147, G:112, B: 219) + /// + public static TiledColor MediumPurple => new TiledColor(0xFF9370DB); + + /// + /// Medium Sea Green (A: 255, R:60, G:179, B: 113) + /// + public static TiledColor MediumSeaGreen => new TiledColor(0xFF3CB371); + + /// + /// Medium Slate Blue (A: 255, R:123, G:104, B: 238) + /// + public static TiledColor MediumSlateBlue => new TiledColor(0xFF7B68EE); + + /// + /// Medium Spring Green (A: 255, R:0, G:250, B: 154) + /// + public static TiledColor MediumSpringGreen => new TiledColor(0xFF00FA9A); + + /// + /// Medium Turquoise (A: 255, R:72, G:209, B: 204) + /// + public static TiledColor MediumTurquoise => new TiledColor(0xFF48D1CC); + + /// + /// Medium Violet Red (A: 255, R:199, G:21, B: 133) + /// + public static TiledColor MediumVioletRed => new TiledColor(0xFFC71585); + + /// + /// Midnight Blue (A: 255, R:25, G:25, B: 112) + /// + public static TiledColor MidnightBlue => new TiledColor(0xFF191970); + + /// + /// Mint Cream (A: 255, R:245, G:255, B: 250) + /// + public static TiledColor MintCream => new TiledColor(0xFFF5FFFA); + + /// + /// Misty Rose (A: 255, R:255, G:228, B: 225) + /// + public static TiledColor MistyRose => new TiledColor(0xFFFFE4E1); + + /// + /// Moccasin (A: 255, R:255, G:228, B: 181) + /// + public static TiledColor Moccasin => new TiledColor(0xFFFFE4B5); + + /// + /// Navajo White (A: 255, R:255, G:222, B: 173) + /// + public static TiledColor NavajoWhite => new TiledColor(0xFFFFDEAD); + + /// + /// Navy (A: 255, R:0, G:0, B: 128) + /// + public static TiledColor Navy => new TiledColor(0xFF000080); + + /// + /// Old Lace (A: 255, R:253, G:245, B: 230) + /// + public static TiledColor OldLace => new TiledColor(0xFFFDF5E6); + + /// + /// Olive (A: 255, R:128, G:128, B: 0) + /// + public static TiledColor Olive => new TiledColor(0xFF808000); + + /// + /// Olive Drab (A: 255, R:107, G:142, B: 35) + /// + public static TiledColor OliveDrab => new TiledColor(0xFF6B8E23); + + /// + /// Orange (A: 255, R:255, G:165, B: 0) + /// + public static TiledColor Orange => new TiledColor(0xFFFFA500); + + /// + /// Orange Red (A: 255, R:255, G:69, B: 0) + /// + public static TiledColor OrangeRed => new TiledColor(0xFFFF4500); + + /// + /// Orchid (A: 255, R:218, G:112, B: 214) + /// + public static TiledColor Orchid => new TiledColor(0xFFDA70D6); + + /// + /// Pale Golden Rod (A: 255, R:238, G:232, B: 170) + /// + public static TiledColor PaleGoldenRod => new TiledColor(0xFFEEE8AA); + + /// + /// Pale Green (A: 255, R:152, G:251, B: 152) + /// + public static TiledColor PaleGreen => new TiledColor(0xFF98FB98); + + /// + /// Pale Turquoise (A: 255, R:175, G:238, B: 238) + /// + public static TiledColor PaleTurquoise => new TiledColor(0xFFAFEEEE); + + /// + /// Pale Violet Red (A: 255, R:219, G:112, B: 147) + /// + public static TiledColor PaleVioletRed => new TiledColor(0xFFDB7093); + + /// + /// Papaya Whip (A: 255, R:255, G:239, B: 213) + /// + public static TiledColor PapayaWhip => new TiledColor(0xFFFFEFD5); + + /// + /// Peach Puff (A: 255, R:255, G:218, B: 185) + /// + public static TiledColor PeachPuff => new TiledColor(0xFFFFDAB9); + + /// + /// Peru (A: 255, R:205, G:133, B: 63) + /// + public static TiledColor Peru => new TiledColor(0xFFCD853F); + + /// + /// Pink (A: 255, R:255, G:192, B: 203) + /// + public static TiledColor Pink => new TiledColor(0xFFFFC0CB); + + /// + /// Plum (A: 255, R:221, G:160, B: 221) + /// + public static TiledColor Plum => new TiledColor(0xFFDDA0DD); + + /// + /// Powder Blue (A: 255, R:176, G:224, B: 230) + /// + public static TiledColor PowderBlue => new TiledColor(0xFFB0E0E6); + + /// + /// Purple (A: 255, R:128, G:0, B: 128) + /// + public static TiledColor Purple => new TiledColor(0xFF800080); + + /// + /// Rebecca Purple (A: 255, R:102, G:51, B: 153) + /// + public static TiledColor RebeccaPurple => new TiledColor(0xFF663399); + + /// + /// Red (A: 255, R:255, G:0, B: 0) + /// + public static TiledColor Red => new TiledColor(0xFFFF0000); + + /// + /// Rosy Brown (A: 255, R:188, G:143, B: 143) + /// + public static TiledColor RosyBrown => new TiledColor(0xFFBC8F8F); + + /// + /// Royal Blue (A: 255, R:65, G:105, B: 225) + /// + public static TiledColor RoyalBlue => new TiledColor(0xFF4169E1); + + /// + /// Saddle Brown (A: 255, R:139, G:69, B: 19) + /// + public static TiledColor SaddleBrown => new TiledColor(0xFF8B4513); + + /// + /// Salmon (A: 255, R:250, G:128, B: 114) + /// + public static TiledColor Salmon => new TiledColor(0xFFFA8072); + + /// + /// Sandy Brown (A: 255, R:244, G:164, B: 96) + /// + public static TiledColor SandyBrown => new TiledColor(0xFFF4A460); + + /// + /// Sea Green (A: 255, R:46, G:139, B: 87) + /// + public static TiledColor SeaGreen => new TiledColor(0xFF2E8B57); + + /// + /// Sea Shell (A: 255, R:255, G:245, B: 238) + /// + public static TiledColor SeaShell => new TiledColor(0xFFFFF5EE); + + /// + /// Sienna (A: 255, R:160, G:82, B: 45) + /// + public static TiledColor Sienna => new TiledColor(0xFFA0522D); + + /// + /// Silver (A: 255, R:192, G:192, B: 192) + /// + public static TiledColor Silver => new TiledColor(0xFFC0C0C0); + + /// + /// Sky Blue (A: 255, R:135, G:206, B: 235) + /// + public static TiledColor SkyBlue => new TiledColor(0xFF87CEEB); + + /// + /// Slate Blue (A: 255, R:106, G:90, B: 205) + /// + public static TiledColor SlateBlue => new TiledColor(0xFF6A5ACD); + + /// + /// Slate Gray (A: 255, R:112, G:128, B: 144) + /// + public static TiledColor SlateGray => new TiledColor(0xFF708090); + + /// + /// Slate Grey (A: 255, R:112, G:128, B: 144) + /// + public static TiledColor SlateGrey => new TiledColor(0xFF708090); + + /// + /// Snow (A: 255, R:255, G:250, B: 250) + /// + public static TiledColor Snow => new TiledColor(0xFFFFFAFA); + + /// + /// Spring Green (A: 255, R:0, G:255, B: 127) + /// + public static TiledColor SpringGreen => new TiledColor(0xFF00FF7F); + + /// + /// Steel Blue (A: 255, R:70, G:130, B: 180) + /// + public static TiledColor SteelBlue => new TiledColor(0xFF4682B4); + + /// + /// Tan (A: 255, R:210, G:180, B: 140) + /// + public static TiledColor Tan => new TiledColor(0xFFD2B48C); + + /// + /// Teal (A: 255, R:0, G:128, B: 128) + /// + public static TiledColor Teal => new TiledColor(0xFF008080); + + /// + /// Thistle (A: 255, R:216, G:191, B: 216) + /// + public static TiledColor Thistle => new TiledColor(0xFFD8BFD8); + + /// + /// Tomato (A: 255, R:255, G:99, B: 71) + /// + public static TiledColor Tomato => new TiledColor(0xFFFF6347); + + /// + /// Turquoise (A: 255, R:64, G:224, B: 208) + /// + public static TiledColor Turquoise => new TiledColor(0xFF40E0D0); + + /// + /// Violet (A: 255, R:238, G:130, B: 238) + /// + public static TiledColor Violet => new TiledColor(0xFFEE82EE); + + /// + /// Wheat (A: 255, R:245, G:222, B: 179) + /// + public static TiledColor Wheat => new TiledColor(0xFFF5DEB3); + + /// + /// White (A: 255, R:255, G:255, B: 255) + /// + public static TiledColor White => new TiledColor(0xFFFFFFFF); + + /// + /// White Smoke (A: 255, R:245, G:245, B: 245) + /// + public static TiledColor WhiteSmoke => new TiledColor(0xFFF5F5F5); + + /// + /// Yellow (A: 255, R:255, G:255, B: 0) + /// + public static TiledColor Yellow => new TiledColor(0xFFFFFF00); + + /// + /// Yellow Green (A: 255, R:154, G:205, B: 50) + /// + public static TiledColor YellowGreen => new TiledColor(0xFF9ACD32); + + #endregion Static TiledColors + + // Stored as 32-bit unsigned integert in the following format. + // |-------|-------|-------|------- + // A R G B + private uint _packedValue; + + /// + /// Constructs an ARGB color from a packed value. + /// The value is a 32-bit unsigned integer with B in the least significant octet. + /// + /// The packed value. + public TiledColor(uint packedValue) + { + _packedValue = packedValue; + } + + /// + /// 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) + { + _packedValue = + ((uint)alpha << 24) | + ((uint)red << 16) | + ((uint)green << 8) | + ((uint)blue << 0); + } + + /// + /// Gets or sets the red component. + /// + public byte R + { + readonly get => (byte)(_packedValue >> 16); + set => _packedValue = (_packedValue & 0xFF00FFFF) | ((uint)value << 16); + } + + /// + /// Gets or sets the green component. + /// + public byte G + { + readonly get => (byte)(_packedValue >> 8); + set => _packedValue = (_packedValue & 0xFFFF00FF) | ((uint)value << 8); + } + + /// + /// Gets or sets the blue component. + /// + public byte B + { + readonly get => (byte)(_packedValue >> 0); + set => _packedValue = (_packedValue & 0xFFFFFF00) | ((uint)value << 0); + } + + /// + /// Gets or sets the alpha component. + /// + public byte A + { + readonly get => (byte)(_packedValue >> 24); + set => _packedValue = (_packedValue & 0x00FFFFFF) | ((uint)value << 24); + } /// /// Attempts to parse the specified string into a . Expects strings in the format #RRGGBB or #AARRGGBB. @@ -39,8 +843,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 +874,56 @@ 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._packedValue == right._packedValue; + } - 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._packedValue != right._packedValue; } /// - public override bool Equals(object obj) => obj is TiledColor other && Equals(other); + public readonly bool Equals(TiledColor other) => _packedValue == other._packedValue; /// - 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() => _packedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"#{A:x2}{R:x2}{G:x2}{B:x2}"; } 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");