diff options
author | Andreas Kling <kling@serenityos.org> | 2020-06-12 21:07:52 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-12 21:28:55 +0200 |
commit | fdfda6dec20101013bb33633e657f06ef2a1ea96 (patch) | |
tree | 2157f8281cd9bc33a6984455c4831c397d2bd30c /Libraries/LibGfx/Color.cpp | |
parent | 15f4043a7a80f52c0fa05c4e69771e758464cd20 (diff) | |
download | serenity-fdfda6dec20101013bb33633e657f06ef2a1ea96.zip |
AK: Make string-to-number conversion helpers return Optional
Get rid of the weird old signature:
- int StringType::to_int(bool& ok) const
And replace it with sensible new signature:
- Optional<int> StringType::to_int() const
Diffstat (limited to 'Libraries/LibGfx/Color.cpp')
-rw-r--r-- | Libraries/LibGfx/Color.cpp | 42 |
1 files changed, 9 insertions, 33 deletions
diff --git a/Libraries/LibGfx/Color.cpp b/Libraries/LibGfx/Color.cpp index 3ababe371c..59f596ce21 100644 --- a/Libraries/LibGfx/Color.cpp +++ b/Libraries/LibGfx/Color.cpp @@ -139,22 +139,11 @@ static Optional<Color> parse_rgb_color(const StringView& string) if (parts.size() != 3) return {}; - bool ok; - auto r = parts[0].to_int(ok); - if (!ok) - return {}; - auto g = parts[1].to_int(ok); - if (!ok) - return {}; - auto b = parts[2].to_int(ok); - if (!ok) - return {}; + auto r = parts[0].to_uint().value_or(256); + auto g = parts[1].to_uint().value_or(256); + auto b = parts[2].to_uint().value_or(256); - if (r < 0 || r > 255) - return {}; - if (g < 0 || g > 255) - return {}; - if (b < 0 || b > 255) + if (r > 255 || g > 255 || b > 255) return {}; return Color(r, g, b); @@ -171,27 +160,14 @@ static Optional<Color> parse_rgba_color(const StringView& string) if (parts.size() != 4) return {}; - bool ok; - auto r = parts[0].to_int(ok); - if (!ok) - return {}; - auto g = parts[1].to_int(ok); - if (!ok) - return {}; - auto b = parts[2].to_int(ok); - if (!ok) - return {}; + auto r = parts[0].to_int().value_or(256); + auto g = parts[1].to_int().value_or(256); + auto b = parts[2].to_int().value_or(256); double alpha = strtod(parts[3].to_string().characters(), nullptr); - int a = alpha * 255; + unsigned a = alpha * 255; - if (r < 0 || r > 255) - return {}; - if (g < 0 || g > 255) - return {}; - if (b < 0 || b > 255) - return {}; - if (a < 0 || a > 255) + if (r > 255 || g > 255 || b > 255 || a > 255) return {}; return Color(r, g, b, a); |