From 8ff343f0538a59824c3bcedd72f1f99f1ca23ca7 Mon Sep 17 00:00:00 2001
From: Alejandro Perea <alexpro820@gmail.com>
Date: Fri, 4 Mar 2022 12:30:19 +0100
Subject: [PATCH] Close #177 (#180)

---
 assets/tiled_group_layers.tmx |  2 +-
 src/properties.rs             | 13 ++++++-------
 tests/lib.rs                  |  7 ++++++-
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/assets/tiled_group_layers.tmx b/assets/tiled_group_layers.tmx
index b027a4e..7a93fcc 100644
--- a/assets/tiled_group_layers.tmx
+++ b/assets/tiled_group_layers.tmx
@@ -18,7 +18,7 @@
  </layer>
  <group id="3" name="group-1">
   <properties>
-   <property name="key" value="value4"/>
+   <property name="key" type="color" value="#12345678"/>
   </properties>
   <layer id="5" name="tile-2" width="8" height="8">
    <properties>
diff --git a/src/properties.rs b/src/properties.rs
index 7fda017..5c481f8 100644
--- a/src/properties.rs
+++ b/src/properties.rs
@@ -58,7 +58,7 @@ impl FromStr for Color {
 }
 
 /// Represents a custom property's value.
-/// 
+///
 /// Also read the [TMX docs](https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tmx-properties).
 #[derive(Debug, PartialEq, Clone)]
 pub enum PropertyValue {
@@ -69,7 +69,7 @@ pub enum PropertyValue {
     /// A signed integer value. Corresponds to the `int` property type.
     IntValue(i32),
     /// A color value. Corresponds to the `color` property type.
-    ColorValue(u32),
+    ColorValue(Color),
     /// A string value. Corresponds to the `string` property type.
     StringValue(String),
     /// A filepath value. Corresponds to the `file` property type.
@@ -102,12 +102,11 @@ impl PropertyValue {
                     description: err.to_string(),
                 }),
             },
-            "color" if value.len() > 1 => match u32::from_str_radix(&value[1..], 16) {
-                Ok(color) => Ok(PropertyValue::ColorValue(color)),
-                Err(err) => Err(TiledError::InvalidPropertyValue {
-                    description: err.to_string(),
+            "color" if value.len() > 1 => Color::from_str(&value)
+                .map(|color| PropertyValue::ColorValue(color))
+                .map_err(|_| TiledError::InvalidPropertyValue {
+                    description: "Couldn't parse color".to_string(),
                 }),
-            },
             "string" => Ok(PropertyValue::StringValue(value)),
             "object" => match value.parse() {
                 Ok(val) => Ok(PropertyValue::ObjectValue(val)),
diff --git a/tests/lib.rs b/tests/lib.rs
index 1a405ff..01b2d0f 100644
--- a/tests/lib.rs
+++ b/tests/lib.rs
@@ -377,7 +377,12 @@ fn test_group_layers() {
         layer_tile_1.properties().get("key")
     );
     assert_eq!(
-        Some(&PropertyValue::StringValue("value4".to_string())),
+        Some(&PropertyValue::ColorValue(Color {
+            alpha: 0x12,
+            red: 0x34,
+            green: 0x56,
+            blue: 0x78
+        })),
         layer_group_1.properties().get("key")
     );
     assert_eq!(
-- 
GitLab