2018-03-19 02:04:13 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-05-14 22:43:48 +00:00
|
|
|
#pragma once
|
2018-03-19 02:04:13 +00:00
|
|
|
|
2018-05-14 22:43:48 +00:00
|
|
|
#include <folly/dynamic.h>
|
|
|
|
#include <fabric/graphics/Color.h>
|
|
|
|
#include <fabric/graphics/Geometry.h>
|
2018-04-27 00:51:31 +00:00
|
|
|
|
2018-03-19 02:04:13 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2018-05-14 22:43:48 +00:00
|
|
|
#pragma mark - Color
|
|
|
|
|
|
|
|
inline void fromDynamic(const folly::dynamic &value, SharedColor &result) {
|
2018-03-19 02:04:13 +00:00
|
|
|
float red;
|
|
|
|
float green;
|
|
|
|
float blue;
|
|
|
|
float alpha;
|
|
|
|
|
|
|
|
if (value.isNumber()) {
|
|
|
|
auto argb = value.asInt();
|
2018-09-10 23:33:48 +00:00
|
|
|
auto ratio = 256.f;
|
2018-03-19 02:04:13 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:43:48 +00:00
|
|
|
result = colorFromComponents({red, green, blue, alpha});
|
2018-03-19 02:04:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 22:43:48 +00:00
|
|
|
inline std::string toString(const SharedColor &value) {
|
2018-03-19 02:04:13 +00:00
|
|
|
ColorComponents components = colorComponentsFromColor(value);
|
2018-09-10 23:33:48 +00:00
|
|
|
auto ratio = 256.f;
|
2018-03-19 02:04:13 +00:00
|
|
|
return "rgba(" +
|
|
|
|
folly::to<std::string>(round(components.red * ratio)) + ", " +
|
|
|
|
folly::to<std::string>(round(components.green * ratio)) + ", " +
|
|
|
|
folly::to<std::string>(round(components.blue * ratio)) + ", " +
|
|
|
|
folly::to<std::string>(round(components.alpha * ratio)) + ")";
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:43:48 +00:00
|
|
|
#pragma mark - Geometry
|
|
|
|
|
|
|
|
inline void fromDynamic(const folly::dynamic &value, Point &result) {
|
2018-05-22 02:59:05 +00:00
|
|
|
if (value.isObject()) {
|
|
|
|
result = Point {(Float)value["x"].asDouble(), (Float)value["y"].asDouble()};
|
|
|
|
return;
|
|
|
|
}
|
2018-05-14 22:43:48 +00:00
|
|
|
if (value.isArray()) {
|
|
|
|
result = Point {(Float)value[0].asDouble(), (Float)value[1].asDouble()};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void fromDynamic(const folly::dynamic &value, Size &result) {
|
2018-05-22 02:59:05 +00:00
|
|
|
if (value.isObject()) {
|
|
|
|
result = Size {(Float)value["width"].asDouble(), (Float)value["height"].asDouble()};
|
|
|
|
return;
|
|
|
|
}
|
2018-05-14 22:43:48 +00:00
|
|
|
if (value.isArray()) {
|
|
|
|
result = Size {(Float)value[0].asDouble(), (Float)value[1].asDouble()};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2018-05-16 06:32:38 +00:00
|
|
|
inline void fromDynamic(const folly::dynamic &value, EdgeInsets &result) {
|
2018-06-18 04:36:00 +00:00
|
|
|
if (value.isNumber()) {
|
|
|
|
const Float number = value.asDouble();
|
|
|
|
result = EdgeInsets {number, number, number, number};
|
|
|
|
return;
|
|
|
|
}
|
2018-05-22 02:59:05 +00:00
|
|
|
if (value.isObject()) {
|
|
|
|
result = EdgeInsets {
|
|
|
|
(Float)value["top"].asDouble(),
|
|
|
|
(Float)value["left"].asDouble(),
|
|
|
|
(Float)value["bottom"].asDouble(),
|
|
|
|
(Float)value["right"].asDouble()
|
|
|
|
};
|
|
|
|
return;
|
|
|
|
}
|
2018-05-16 06:32:38 +00:00
|
|
|
if (value.isArray()) {
|
|
|
|
result = EdgeInsets {
|
|
|
|
(Float)value[0].asDouble(),
|
|
|
|
(Float)value[1].asDouble(),
|
|
|
|
(Float)value[2].asDouble(),
|
|
|
|
(Float)value[3].asDouble()
|
|
|
|
};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2018-06-15 18:25:13 +00:00
|
|
|
inline void fromDynamic(const folly::dynamic &value, CornerInsets &result) {
|
2018-06-18 04:36:00 +00:00
|
|
|
if (value.isNumber()) {
|
|
|
|
const Float number = value.asDouble();
|
|
|
|
result = CornerInsets {number, number, number, number};
|
|
|
|
return;
|
|
|
|
}
|
2018-06-15 18:25:13 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:43:48 +00:00
|
|
|
inline std::string toString(const Point &point) {
|
2018-04-27 00:51:31 +00:00
|
|
|
return "{" + folly::to<std::string>(point.x) + ", " + folly::to<std::string>(point.y) + "}";
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:43:48 +00:00
|
|
|
inline std::string toString(const Size &size) {
|
2018-04-27 00:51:31 +00:00
|
|
|
return "{" + folly::to<std::string>(size.width) + ", " + folly::to<std::string>(size.height) + "}";
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:43:48 +00:00
|
|
|
inline std::string toString(const Rect &rect) {
|
|
|
|
return "{" + toString(rect.origin) + ", " + toString(rect.size) + "}";
|
2018-04-27 00:51:31 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 22:43:48 +00:00
|
|
|
inline std::string toString(const EdgeInsets &edgeInsets) {
|
2018-04-27 00:51:31 +00:00
|
|
|
return "{" +
|
|
|
|
folly::to<std::string>(edgeInsets.left) + ", " +
|
|
|
|
folly::to<std::string>(edgeInsets.top) + ", " +
|
|
|
|
folly::to<std::string>(edgeInsets.right) + ", " +
|
|
|
|
folly::to<std::string>(edgeInsets.bottom) + "}";
|
|
|
|
}
|
|
|
|
|
2018-06-15 18:25:13 +00:00
|
|
|
inline std::string toString(const CornerInsets &cornerInsets) {
|
|
|
|
return "{" +
|
|
|
|
folly::to<std::string>(cornerInsets.topLeft) + ", " +
|
|
|
|
folly::to<std::string>(cornerInsets.topRight) + ", " +
|
|
|
|
folly::to<std::string>(cornerInsets.bottomLeft) + ", " +
|
|
|
|
folly::to<std::string>(cornerInsets.bottomRight) + "}";
|
|
|
|
}
|
|
|
|
|
2018-03-19 02:04:13 +00:00
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|