@@ -41,7 +41,7 @@ static SpriteManifest::Format parseFormat(const nlohmann::json& jFormat, const b
4141 else if (value == " palette" && hasImageFile)
4242 return SpriteManifest::Format::paletteImage;
4343 else if (value == " palette" )
44- return SpriteManifest::Format::paletteHex ;
44+ return SpriteManifest::Format::paletteArray ;
4545 else
4646 throw std::runtime_error (" Unknown format" );
4747}
@@ -117,6 +117,43 @@ static std::optional<Range> parseRange(std::string_view s)
117117 return std::nullopt ;
118118}
119119
120+ static BGRColour parseJSONPaletteColour (const std::string& s)
121+ {
122+ uint8_t r = 0 ;
123+ uint8_t g = 0 ;
124+ uint8_t b = 0 ;
125+ if (s[0 ] == ' #' && s.size () == 7 )
126+ {
127+ // Expect #RRGGBB
128+ r = std::stoul (s.substr (1 , 2 ), nullptr , 16 ) & 0xFF ;
129+ g = std::stoul (s.substr (3 , 2 ), nullptr , 16 ) & 0xFF ;
130+ b = std::stoul (s.substr (5 , 2 ), nullptr , 16 ) & 0xFF ;
131+ }
132+ return { b, g, r };
133+ }
134+
135+ static std::vector<BGRColour> parseColours (const nlohmann::json& jPalette)
136+ {
137+ if (!jPalette.is_array ())
138+ std::runtime_error (" parseColours expects parameter jPalette to be object" );
139+
140+ auto jColours = jPalette[" colours" ];
141+ std::vector<BGRColour> out;
142+
143+ auto numColours = jColours.size ();
144+ out.reserve (numColours * 3 );
145+
146+ for (auto & jColour : jColours)
147+ {
148+ if (!jColour.is_string ())
149+ std::runtime_error (" Encountered a value that is not a string!" );
150+
151+ out.push_back (parseJSONPaletteColour (jColour.get <std::string>()));
152+ }
153+
154+ return out;
155+ }
156+
120157static SpriteManifest::Entry parseEntry (const nlohmann::json& jEntry)
121158{
122159 SpriteManifest::Entry result;
@@ -151,13 +188,21 @@ static SpriteManifest::Entry parseEntry(const nlohmann::json& jEntry)
151188 result.path = jEntry.at (" path" ).get <std::string>();
152189 if (jEntry.contains (" format" ))
153190 result.format = parseFormat (jEntry[" format" ], jEntry.contains (" path" ));
191+
154192 if (jEntry.contains (" palette" ))
155193 result.palette = parsePalette (jEntry[" palette" ]);
194+ if (jEntry.contains (" colours" ))
195+ result.colours = parseColours (jEntry[" colours" ]);
196+
197+ if (result.format == SpriteManifest::Format::automatic && !result.colours .empty ())
198+ result.format = SpriteManifest::Format::paletteArray;
156199
157200 if (jEntry.contains (" x" ))
158201 result.offsetX = jEntry[" x" ];
159202 else if (jEntry.contains (" x_offset" ))
160203 result.offsetX = jEntry[" x_offset" ];
204+ else if (jEntry.contains (" index" )) // palette startIndex
205+ result.offsetX = jEntry[" index" ]; // palette startIndex
161206
162207 if (jEntry.contains (" y" ))
163208 result.offsetY = jEntry[" y" ];
0 commit comments