diff --git a/packages/react-native/ReactCommon/react/renderer/core/graphicsConversions.h b/packages/react-native/ReactCommon/react/renderer/core/graphicsConversions.h index 04c8930e9198..8e1ff0a04739 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/graphicsConversions.h +++ b/packages/react-native/ReactCommon/react/renderer/core/graphicsConversions.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -29,29 +30,7 @@ inline void fromRawValue( const PropsParserContext& context, const RawValue& value, SharedColor& result) { - ColorComponents colorComponents = {0, 0, 0, 0}; - - if (value.hasType()) { - auto argb = (int64_t)value; - auto ratio = 255.f; - colorComponents.alpha = ((argb >> 24) & 0xFF) / ratio; - colorComponents.red = ((argb >> 16) & 0xFF) / ratio; - colorComponents.green = ((argb >> 8) & 0xFF) / ratio; - colorComponents.blue = (argb & 0xFF) / ratio; - } else if (value.hasType>()) { - auto items = (std::vector)value; - auto length = items.size(); - react_native_expect(length == 3 || length == 4); - colorComponents.red = items.at(0); - colorComponents.green = items.at(1); - colorComponents.blue = items.at(2); - colorComponents.alpha = length == 4 ? items.at(3) : 1.0f; - } else { - result = parsePlatformColor(context, value); - return; - } - - result = colorFromComponents(colorComponents); + fromRawValue(context.contextContainer, context.surfaceId, value, result); } #ifdef ANDROID diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/fromRawValueShared.h b/packages/react-native/ReactCommon/react/renderer/graphics/fromRawValueShared.h new file mode 100644 index 000000000000..fbc07288fa31 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/graphics/fromRawValueShared.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include +#include + +#pragma once + +namespace facebook::react { +using parsePlatformColorFn = + SharedColor (*)(const ContextContainer&, int32_t, const RawValue&); + +inline void fromRawValueShared( + const ContextContainer& contextContainer, + int32_t surfaceId, + const RawValue& value, + SharedColor& result, + parsePlatformColorFn parsePlatformColor) { + ColorComponents colorComponents = {0, 0, 0, 0}; + + if (value.hasType()) { + auto argb = (int64_t)value; + auto ratio = 255.f; + colorComponents.alpha = ((argb >> 24) & 0xFF) / ratio; + colorComponents.red = ((argb >> 16) & 0xFF) / ratio; + colorComponents.green = ((argb >> 8) & 0xFF) / ratio; + colorComponents.blue = (argb & 0xFF) / ratio; + + result = colorFromComponents(colorComponents); + } else if (value.hasType>()) { + auto items = (std::vector)value; + auto length = items.size(); + react_native_expect(length == 3 || length == 4); + colorComponents.red = items.at(0); + colorComponents.green = items.at(1); + colorComponents.blue = items.at(2); + colorComponents.alpha = length == 4 ? items.at(3) : 1.0f; + + result = colorFromComponents(colorComponents); + } else { + result = parsePlatformColor(contextContainer, surfaceId, value); + } +} +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/platform/android/react/renderer/graphics/PlatformColorParser.h b/packages/react-native/ReactCommon/react/renderer/graphics/platform/android/react/renderer/graphics/PlatformColorParser.h index 379bc9cf6b9f..ca11a1c7deef 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/platform/android/react/renderer/graphics/PlatformColorParser.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/android/react/renderer/graphics/PlatformColorParser.h @@ -8,23 +8,25 @@ #pragma once #include -#include -#include +#include +#include #include +#include +#include #include namespace facebook::react { inline SharedColor parsePlatformColor( - const PropsParserContext& context, + const ContextContainer& contextContainer, + int32_t surfaceId, const RawValue& value) { ColorComponents colorComponents = {0, 0, 0, 0}; if (value.hasType< std::unordered_map>>()) { const auto& fabricUIManager = - context.contextContainer.at>( - "FabricUIManager"); + contextContainer.at>("FabricUIManager"); static auto getColorFromJava = fabricUIManager->getClass() ->getMethod)>("getColor"); @@ -37,8 +39,8 @@ inline SharedColor parsePlatformColor( for (int i = 0; i < resourcePaths.size(); i++) { javaResourcePaths->setElement(i, *jni::make_jstring(resourcePaths[i])); } - auto color = getColorFromJava( - fabricUIManager, context.surfaceId, *javaResourcePaths); + auto color = + getColorFromJava(fabricUIManager, surfaceId, *javaResourcePaths); auto argb = (int64_t)color; auto ratio = 255.f; @@ -51,4 +53,13 @@ inline SharedColor parsePlatformColor( return {colorFromComponents(colorComponents)}; } +inline void fromRawValue( + const ContextContainer& contextContainer, + int32_t surfaceId, + const RawValue& value, + SharedColor& result) { + fromRawValueShared( + contextContainer, surfaceId, value, result, parsePlatformColor); +} + } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/PlatformColorParser.h b/packages/react-native/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/PlatformColorParser.h index 698b6b16d159..0526f892f48f 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/PlatformColorParser.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/PlatformColorParser.h @@ -7,15 +7,18 @@ #pragma once -#include -#include +#include +#include #include +#include +#include namespace facebook::react { inline SharedColor parsePlatformColor( - const PropsParserContext& context, - const RawValue& value) { + const ContextContainer& /*contextContainer*/, + int32_t /*surfaceId*/, + const RawValue& /*value*/) { float alpha = 0; float red = 0; float green = 0; @@ -24,4 +27,13 @@ inline SharedColor parsePlatformColor( return {colorFromComponents({red, green, blue, alpha})}; } +inline void fromRawValue( + const ContextContainer& contextContainer, + int32_t surfaceId, + const RawValue& value, + SharedColor& result) { + fromRawValueShared( + contextContainer, surfaceId, value, result, parsePlatformColor); +} + } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.h b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.h index 32bcc883f468..d18798e7e600 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.h @@ -7,16 +7,19 @@ #pragma once -#include -#include +#include +#include #include #include +#include +#include #include namespace facebook::react { inline SharedColor parsePlatformColor( - const PropsParserContext& context, + const ContextContainer& /*contextContainer*/, + int32_t /*surfaceId*/, const RawValue& value) { if (value.hasType>()) { auto items = (std::unordered_map)value; @@ -31,4 +34,13 @@ inline SharedColor parsePlatformColor( return clearColor(); } +inline void fromRawValue( + const ContextContainer& contextContainer, + int32_t surfaceId, + const RawValue& value, + SharedColor& result) { + fromRawValueShared( + contextContainer, surfaceId, value, result, parsePlatformColor); +} + } // namespace facebook::react