diff --git a/aegisub/libaegisub/common/option_visit.cpp b/aegisub/libaegisub/common/option_visit.cpp index 83a13e7be..b1d8bd82c 100644 --- a/aegisub/libaegisub/common/option_visit.cpp +++ b/aegisub/libaegisub/common/option_visit.cpp @@ -31,6 +31,8 @@ #include #include +#include + namespace agi { ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name, bool ignore_errors, bool replace) @@ -117,7 +119,12 @@ void ConfigVisitor::Visit(const json::Double& number) { } void ConfigVisitor::Visit(const json::String& string) { - if (string.size() && (string.find("rgb(") == 0 || string[0] == '#' || string[0] == '&')) { + size_t size = string.size(); + if ((size == 4 && string[0] == '#') || + (size == 7 && string[0] == '#') || + (size >= 10 && boost::starts_with(string, "rgb(")) || + ((size == 9 || size == 10) && boost::starts_with(string, "&H"))) + { AddOptionValue(new OptionValueColor(name, string)); } else { AddOptionValue(new OptionValueString(name, string)); diff --git a/aegisub/tests/libaegisub_option.cpp b/aegisub/tests/libaegisub_option.cpp index 2a22646d4..9210986e9 100644 --- a/aegisub/tests/libaegisub_option.cpp +++ b/aegisub/tests/libaegisub_option.cpp @@ -1,4 +1,4 @@ - // Copyright (c) 2011, Thomas Goyne +// Copyright (c) 2011, Thomas Goyne // // Permission to use, copy, modify, and distribute this software for any // purpose with or without fee is hereby granted, provided that the above @@ -261,3 +261,21 @@ TEST_F(lagi_option, empty_array_decays_to_first_used_type) { EXPECT_THROW(opt.Get("arr")->GetListInt(), agi::OptionValueErrorInvalidListType); } } + +#define CHECK_TYPE(str, type) \ + do { \ + agi::Options opt("", "{ \"" str "\" : \"" str "\" }", agi::Options::FLUSH_SKIP); \ + EXPECT_NO_THROW(opt.Get(str)->Get##type()); \ + } while (false) + +TEST_F(lagi_option, color_vs_string) { + CHECK_TYPE("#", String); + CHECK_TYPE("#a", String); + CHECK_TYPE("#abc", Color); + CHECK_TYPE("#aabbcc", Color); + CHECK_TYPE("#aabb", String); + + CHECK_TYPE("&", String); + CHECK_TYPE("&H000000&", Color); + CHECK_TYPE("&H00000000", Color); +}