From 56369e818286fa42b37ebea1c08959ec4b47513f Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Fri, 16 Sep 2022 11:40:22 -0700 Subject: [PATCH] Add Color::MakeRGBA8 (flutter/engine#36200) --- engine/src/flutter/impeller/geometry/color.h | 7 ++++++- .../impeller/geometry/geometry_unittests.cc | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/impeller/geometry/color.h b/engine/src/flutter/impeller/geometry/color.h index e2a32fb851..623cb510be 100644 --- a/engine/src/flutter/impeller/geometry/color.h +++ b/engine/src/flutter/impeller/geometry/color.h @@ -41,11 +41,16 @@ struct Color { constexpr Color() {} - Color(const ColorHSB& hsbColor); + explicit Color(const ColorHSB& hsbColor); constexpr Color(Scalar r, Scalar g, Scalar b, Scalar a) : red(r), green(g), blue(b), alpha(a) {} + static constexpr Color MakeRGBA8(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { + return Color(static_cast(r) / 255, static_cast(g) / 255, + static_cast(b) / 255, static_cast(a) / 255); + } + constexpr bool operator==(const Color& c) const { return red == c.red && green == c.green && blue == c.blue && alpha == c.alpha; diff --git a/engine/src/flutter/impeller/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/geometry/geometry_unittests.cc index 324e607daf..9b64c1d834 100644 --- a/engine/src/flutter/impeller/geometry/geometry_unittests.cc +++ b/engine/src/flutter/impeller/geometry/geometry_unittests.cc @@ -964,6 +964,26 @@ TEST(GeometryTest, ColorLerp) { } } +TEST(GeometryTest, ColorMakeRGBA8) { + { + Color a = Color::MakeRGBA8(0, 0, 0, 0); + Color b = Color::BlackTransparent(); + ASSERT_COLOR_NEAR(a, b); + } + + { + Color a = Color::MakeRGBA8(255, 255, 255, 255); + Color b = Color::White(); + ASSERT_COLOR_NEAR(a, b); + } + + { + Color a = Color::MakeRGBA8(63, 127, 191, 127); + Color b(0.247059, 0.498039, 0.74902, 0.498039); + ASSERT_COLOR_NEAR(a, b); + } +} + TEST(GeometryTest, CanConvertBetweenDegressAndRadians) { { auto deg = Degrees{90.0};