/* * 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. */ #pragma once #include #include #include #include #include #include #include #include #include #include namespace facebook::react { inline SharedColor fromCSSColor(const CSSColor& cssColor) { return hostPlatformColorFromRGBA( cssColor.r, cssColor.g, cssColor.b, cssColor.a); } inline std::optional coerceAmount(const RawValue& value) { if (value.hasType()) { return (Float)value; } if (value.hasType()) { auto cssVal = parseCSSProperty((std::string)value); if (std::holds_alternative(cssVal)) { return std::get(cssVal).value; } else if (std::holds_alternative(cssVal)) { return std::get(cssVal).value / 100.0f; } } return {}; } inline std::optional coerceAngle(const RawValue& value) { if (value.hasType()) { return (Float)value; } if (value.hasType()) { auto cssVal = parseCSSProperty((std::string)value); if (std::holds_alternative(cssVal)) { return std::get(cssVal).degrees; } } return {}; } inline SharedColor coerceColor( const RawValue& value, const PropsParserContext& context) { if (value.hasType()) { auto cssColor = parseCSSProperty((std::string)value); if (!std::holds_alternative(cssColor)) { return {}; } return fromCSSColor(std::get(cssColor)); } SharedColor color; fromRawValue(context.contextContainer, context.surfaceId, value, color); return color; } inline std::optional coerceLength(const RawValue& value) { if (value.hasType()) { return (Float)value; } if (value.hasType()) { auto len = parseCSSProperty((std::string)value); if (!std::holds_alternative(len)) { return {}; } auto cssLen = std::get(len); if (cssLen.unit != CSSLengthUnit::Px) { return {}; } return cssLen.value; } return {}; } } // namespace facebook::react