/** * Copyright (c) 2015-present, Facebook, Inc. * * 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 namespace facebook { namespace react { #pragma mark - Color inline void fromDynamic(const folly::dynamic &value, SharedColor &result) { float red; float green; float blue; float alpha; if (value.isNumber()) { auto argb = value.asInt(); auto ratio = 256.f; alpha = ((argb >> 24) & 0xFF) / ratio; red = ((argb >> 16) & 0xFF) / ratio; green = ((argb >> 8) & 0xFF) / ratio; blue = (argb & 0xFF) / ratio; } else if (value.isArray()) { auto size = value.size(); assert(size == 3 || size == 4); red = value[0].asDouble(); green = value[1].asDouble(); blue = value[2].asDouble(); alpha = size == 4 ? value[3].asDouble() : 1.0; } else { abort(); } result = colorFromComponents({red, green, blue, alpha}); } inline std::string toString(const SharedColor &value) { ColorComponents components = colorComponentsFromColor(value); auto ratio = 256.f; return "rgba(" + folly::to(round(components.red * ratio)) + ", " + folly::to(round(components.green * ratio)) + ", " + folly::to(round(components.blue * ratio)) + ", " + folly::to(round(components.alpha * ratio)) + ")"; } #pragma mark - Geometry inline void fromDynamic(const folly::dynamic &value, Point &result) { if (value.isObject()) { result = Point {(Float)value["x"].asDouble(), (Float)value["y"].asDouble()}; return; } if (value.isArray()) { result = Point {(Float)value[0].asDouble(), (Float)value[1].asDouble()}; return; } abort(); } inline void fromDynamic(const folly::dynamic &value, Size &result) { if (value.isObject()) { result = Size {(Float)value["width"].asDouble(), (Float)value["height"].asDouble()}; return; } if (value.isArray()) { result = Size {(Float)value[0].asDouble(), (Float)value[1].asDouble()}; return; } abort(); } inline void fromDynamic(const folly::dynamic &value, EdgeInsets &result) { if (value.isNumber()) { const Float number = value.asDouble(); result = EdgeInsets {number, number, number, number}; return; } if (value.isObject()) { result = EdgeInsets { (Float)value["top"].asDouble(), (Float)value["left"].asDouble(), (Float)value["bottom"].asDouble(), (Float)value["right"].asDouble() }; return; } if (value.isArray()) { result = EdgeInsets { (Float)value[0].asDouble(), (Float)value[1].asDouble(), (Float)value[2].asDouble(), (Float)value[3].asDouble() }; return; } abort(); } inline void fromDynamic(const folly::dynamic &value, CornerInsets &result) { if (value.isNumber()) { const Float number = value.asDouble(); result = CornerInsets {number, number, number, number}; return; } if (value.isObject()) { result = CornerInsets { (Float)value["topLeft"].asDouble(), (Float)value["topRight"].asDouble(), (Float)value["bottomLeft"].asDouble(), (Float)value["bottomRight"].asDouble() }; return; } if (value.isArray()) { result = CornerInsets { (Float)value[0].asDouble(), (Float)value[1].asDouble(), (Float)value[2].asDouble(), (Float)value[3].asDouble() }; return; } abort(); } inline std::string toString(const Point &point) { return "{" + folly::to(point.x) + ", " + folly::to(point.y) + "}"; } inline std::string toString(const Size &size) { return "{" + folly::to(size.width) + ", " + folly::to(size.height) + "}"; } inline std::string toString(const Rect &rect) { return "{" + toString(rect.origin) + ", " + toString(rect.size) + "}"; } inline std::string toString(const EdgeInsets &edgeInsets) { return "{" + folly::to(edgeInsets.left) + ", " + folly::to(edgeInsets.top) + ", " + folly::to(edgeInsets.right) + ", " + folly::to(edgeInsets.bottom) + "}"; } inline std::string toString(const CornerInsets &cornerInsets) { return "{" + folly::to(cornerInsets.topLeft) + ", " + folly::to(cornerInsets.topRight) + ", " + folly::to(cornerInsets.bottomLeft) + ", " + folly::to(cornerInsets.bottomRight) + "}"; } } // namespace react } // namespace facebook