conversions.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <better/map.h>
  9. #include <folly/dynamic.h>
  10. #include <react/core/RawProps.h>
  11. #include <react/graphics/Color.h>
  12. #include <react/graphics/Geometry.h>
  13. namespace facebook {
  14. namespace react {
  15. #pragma mark - Color
  16. inline void fromRawValue(const RawValue &value, SharedColor &result) {
  17. float red;
  18. float green;
  19. float blue;
  20. float alpha;
  21. if (value.hasType<int>()) {
  22. auto argb = (int64_t)value;
  23. auto ratio = 256.f;
  24. alpha = ((argb >> 24) & 0xFF) / ratio;
  25. red = ((argb >> 16) & 0xFF) / ratio;
  26. green = ((argb >> 8) & 0xFF) / ratio;
  27. blue = (argb & 0xFF) / ratio;
  28. } else if (value.hasType<std::vector<float>>()) {
  29. auto items = (std::vector<float>)value;
  30. auto length = items.size();
  31. assert(length == 3 || length == 4);
  32. red = items.at(0);
  33. green = items.at(1);
  34. blue = items.at(2);
  35. alpha = length == 4 ? items.at(3) : 1.0;
  36. } else {
  37. abort();
  38. }
  39. result = colorFromComponents({red, green, blue, alpha});
  40. }
  41. #ifdef ANDROID
  42. inline folly::dynamic toDynamic(const SharedColor &color) {
  43. ColorComponents components = colorComponentsFromColor(color);
  44. auto ratio = 256.f;
  45. return (
  46. ((int)(components.alpha * ratio) & 0xff) << 24 |
  47. ((int)(components.red * ratio) & 0xff) << 16 |
  48. ((int)(components.green * ratio) & 0xff) << 8 |
  49. ((int)(components.blue * ratio) & 0xff));
  50. }
  51. #endif
  52. inline std::string toString(const SharedColor &value) {
  53. ColorComponents components = colorComponentsFromColor(value);
  54. auto ratio = 256.f;
  55. return "rgba(" + folly::to<std::string>(round(components.red * ratio)) +
  56. ", " + folly::to<std::string>(round(components.green * ratio)) + ", " +
  57. folly::to<std::string>(round(components.blue * ratio)) + ", " +
  58. folly::to<std::string>(round(components.alpha * ratio)) + ")";
  59. }
  60. #pragma mark - Geometry
  61. inline void fromRawValue(const RawValue &value, Point &result) {
  62. if (value.hasType<better::map<std::string, Float>>()) {
  63. auto map = (better::map<std::string, Float>)value;
  64. for (const auto &pair : map) {
  65. if (pair.first == "x") {
  66. result.x = pair.second;
  67. } else if (pair.first == "y") {
  68. result.y = pair.second;
  69. }
  70. }
  71. return;
  72. }
  73. if (value.hasType<std::vector<Float>>()) {
  74. auto array = (std::vector<Float>)value;
  75. assert(array.size() == 2);
  76. result = {array.at(0), array.at(1)};
  77. return;
  78. }
  79. abort();
  80. }
  81. inline void fromRawValue(const RawValue &value, Size &result) {
  82. if (value.hasType<better::map<std::string, Float>>()) {
  83. auto map = (better::map<std::string, Float>)value;
  84. for (const auto &pair : map) {
  85. if (pair.first == "width") {
  86. result.width = pair.second;
  87. } else if (pair.first == "height") {
  88. result.height = pair.second;
  89. }
  90. }
  91. return;
  92. }
  93. if (value.hasType<std::vector<Float>>()) {
  94. auto array = (std::vector<Float>)value;
  95. assert(array.size() == 2);
  96. result = {array.at(0), array.at(1)};
  97. return;
  98. }
  99. abort();
  100. }
  101. inline void fromRawValue(const RawValue &value, EdgeInsets &result) {
  102. if (value.hasType<Float>()) {
  103. auto number = (Float)value;
  104. result = {number, number, number, number};
  105. }
  106. if (value.hasType<better::map<std::string, Float>>()) {
  107. auto map = (better::map<std::string, Float>)value;
  108. for (const auto &pair : map) {
  109. if (pair.first == "top") {
  110. result.top = pair.second;
  111. } else if (pair.first == "left") {
  112. result.left = pair.second;
  113. } else if (pair.first == "bottom") {
  114. result.bottom = pair.second;
  115. } else if (pair.first == "right") {
  116. result.right = pair.second;
  117. }
  118. }
  119. return;
  120. }
  121. if (value.hasType<std::vector<Float>>()) {
  122. auto array = (std::vector<Float>)value;
  123. assert(array.size() == 4);
  124. result = {array.at(0), array.at(1), array.at(2), array.at(3)};
  125. return;
  126. }
  127. abort();
  128. }
  129. inline void fromRawValue(const RawValue &value, CornerInsets &result) {
  130. if (value.hasType<Float>()) {
  131. auto number = (Float)value;
  132. result = {number, number, number, number};
  133. }
  134. if (value.hasType<better::map<std::string, Float>>()) {
  135. auto map = (better::map<std::string, Float>)value;
  136. for (const auto &pair : map) {
  137. if (pair.first == "topLeft") {
  138. result.topLeft = pair.second;
  139. } else if (pair.first == "topRight") {
  140. result.topRight = pair.second;
  141. } else if (pair.first == "bottomLeft") {
  142. result.bottomLeft = pair.second;
  143. } else if (pair.first == "bottomRight") {
  144. result.bottomRight = pair.second;
  145. }
  146. }
  147. return;
  148. }
  149. if (value.hasType<std::vector<Float>>()) {
  150. auto array = (std::vector<Float>)value;
  151. assert(array.size() == 4);
  152. result = {array.at(0), array.at(1), array.at(2), array.at(3)};
  153. return;
  154. }
  155. abort();
  156. }
  157. inline std::string toString(const Point &point) {
  158. return "{" + folly::to<std::string>(point.x) + ", " +
  159. folly::to<std::string>(point.y) + "}";
  160. }
  161. inline std::string toString(const Size &size) {
  162. return "{" + folly::to<std::string>(size.width) + ", " +
  163. folly::to<std::string>(size.height) + "}";
  164. }
  165. inline std::string toString(const Rect &rect) {
  166. return "{" + toString(rect.origin) + ", " + toString(rect.size) + "}";
  167. }
  168. inline std::string toString(const EdgeInsets &edgeInsets) {
  169. return "{" + folly::to<std::string>(edgeInsets.left) + ", " +
  170. folly::to<std::string>(edgeInsets.top) + ", " +
  171. folly::to<std::string>(edgeInsets.right) + ", " +
  172. folly::to<std::string>(edgeInsets.bottom) + "}";
  173. }
  174. inline std::string toString(const CornerInsets &cornerInsets) {
  175. return "{" + folly::to<std::string>(cornerInsets.topLeft) + ", " +
  176. folly::to<std::string>(cornerInsets.topRight) + ", " +
  177. folly::to<std::string>(cornerInsets.bottomLeft) + ", " +
  178. folly::to<std::string>(cornerInsets.bottomRight) + "}";
  179. }
  180. } // namespace react
  181. } // namespace facebook