You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* @flow */
  2. import Platform from '../react/Platform';
  3. import { ColorPalette } from './components';
  4. declare type StyleSheet = Object;
  5. export type StyleType = StyleSheet | Array<StyleSheet>;
  6. /**
  7. * RegExp pattern for long HEX color format.
  8. */
  9. const HEX_LONG_COLOR_FORMAT
  10. = /^#([0-9A-F]{2,2})([0-9A-F]{2,2})([0-9A-F]{2,2})$/i;
  11. /**
  12. * RegExp pattern for short HEX color format.
  13. */
  14. const HEX_SHORT_COLOR_FORMAT
  15. = /^#([0-9A-F]{1,1})([0-9A-F]{1,1})([0-9A-F]{1,1})$/i;
  16. /**
  17. * RegExp pattern for RGB color format.
  18. */
  19. const RGB_COLOR_FORMAT = /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/i;
  20. /**
  21. * RegExp pattern for RGBA color format.
  22. */
  23. const RGBA_COLOR_FORMAT
  24. = /^rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*([0-9.]+)\)$/i;
  25. /**
  26. * The list of the well-known style properties which may not be numbers on Web
  27. * but must be numbers on React Native.
  28. *
  29. * @private
  30. */
  31. const _WELL_KNOWN_NUMBER_PROPERTIES = [ 'height', 'width' ];
  32. /**
  33. * Combines the given 2 styles into a single one.
  34. *
  35. * @param {StyleType} a - An object or array of styles.
  36. * @param {StyleType} b - An object or array of styles.
  37. * @private
  38. * @returns {StyleType} - The merged styles.
  39. */
  40. export function combineStyles(a: StyleType, b: StyleType): StyleType {
  41. const result: Array<StyleSheet> = [];
  42. if (a) {
  43. if (Array.isArray(a)) {
  44. result.push(...a);
  45. } else {
  46. result.push(a);
  47. }
  48. }
  49. if (b) {
  50. if (Array.isArray(b)) {
  51. result.push(...b);
  52. } else {
  53. result.push(b);
  54. }
  55. }
  56. return result;
  57. }
  58. /**
  59. * Create a style sheet using the provided style definitions.
  60. *
  61. * @param {StyleSheet} styles - A dictionary of named style definitions.
  62. * @param {StyleSheet} [overrides={}] - Optional set of additional (often
  63. * platform-dependent/specific) style definitions that will override the base
  64. * (often platform-independent) styles.
  65. * @returns {StyleSheet}
  66. */
  67. export function createStyleSheet(
  68. styles: StyleSheet, overrides: StyleSheet = {}): StyleSheet {
  69. const combinedStyles = {};
  70. for (const k of Object.keys(styles)) {
  71. combinedStyles[k]
  72. = _shimStyles({
  73. ...styles[k],
  74. ...overrides[k]
  75. });
  76. }
  77. return combinedStyles;
  78. }
  79. /**
  80. * Works around a bug in react-native or react-native-webrtc on Android which
  81. * causes Views overlaying RTCView to be clipped. Even though we (may) display
  82. * multiple RTCViews, it is enough to apply the fix only to a View with a
  83. * bounding rectangle containing all RTCviews and their overlaying Views.
  84. *
  85. * @param {StyleSheet} styles - An object which represents a stylesheet.
  86. * @public
  87. * @returns {StyleSheet}
  88. */
  89. export function fixAndroidViewClipping<T: StyleSheet>(styles: T): T {
  90. if (Platform.OS === 'android') {
  91. styles.borderColor = ColorPalette.appBackground;
  92. styles.borderWidth = 1;
  93. }
  94. return styles;
  95. }
  96. /**
  97. * Returns an rgba format of the provided color if it's in hex or rgb format.
  98. *
  99. * NOTE: The function will return the same color if it's not in one of those
  100. * two formats (e.g. 'white').
  101. *
  102. * @param {string} color - The string representation of the color in rgb or hex
  103. * format.
  104. * @param {number} alpha - The alpha value to apply.
  105. * @returns {string}
  106. */
  107. export function getRGBAFormat(color: string, alpha: number): string {
  108. let match = color.match(HEX_LONG_COLOR_FORMAT);
  109. if (match) {
  110. return `#${match[1]}${match[2]}${match[3]}${_getAlphaInHex(alpha)}`;
  111. }
  112. match = color.match(HEX_SHORT_COLOR_FORMAT);
  113. if (match) {
  114. return `#${match[1]}${match[1]}${match[2]}${match[2]}${match[3]}${
  115. match[3]}${_getAlphaInHex(alpha)}`;
  116. }
  117. match = color.match(RGB_COLOR_FORMAT);
  118. if (match) {
  119. return `rgba(${match[1]}, ${match[2]}, ${match[3]}, ${alpha})`;
  120. }
  121. return color;
  122. }
  123. /**
  124. * Decides if a color is light or dark based on the ITU-R BT.709 and W3C
  125. * recommendations.
  126. *
  127. * NOTE: Please see https://www.w3.org/TR/WCAG20/#relativeluminancedef.
  128. *
  129. * @param {string} color - The color in rgb, rgba or hex format.
  130. * @returns {boolean}
  131. */
  132. export function isDarkColor(color: string): boolean {
  133. const rgb = _getRGBObjectFormat(color);
  134. return ((_getColorLuminance(rgb.r) * 0.2126)
  135. + (_getColorLuminance(rgb.g) * 0.7152)
  136. + (_getColorLuminance(rgb.b) * 0.0722)) <= 0.179;
  137. }
  138. /**
  139. * Converts an [0..1] alpha value into HEX.
  140. *
  141. * @param {number} alpha - The alpha value to convert.
  142. * @returns {string}
  143. */
  144. function _getAlphaInHex(alpha: number): string {
  145. return Number(Math.round(255 * alpha)).toString(16)
  146. .padStart(2, '0');
  147. }
  148. /**
  149. * Calculated the color luminance component for an individual color channel.
  150. *
  151. * NOTE: Please see https://www.w3.org/TR/WCAG20/#relativeluminancedef.
  152. *
  153. * @param {number} c - The color which we need the individual luminance
  154. * for.
  155. * @returns {number}
  156. */
  157. function _getColorLuminance(c: number): number {
  158. return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
  159. }
  160. /**
  161. * Parses a color string into an object containing the RGB values as numbers.
  162. *
  163. * NOTE: Object properties are not alpha-sorted for sanity.
  164. *
  165. * @param {string} color - The color to convert.
  166. * @returns {{
  167. * r: number,
  168. * g: number,
  169. * b: number
  170. * }}
  171. */
  172. function _getRGBObjectFormat(color: string): {r: number, g: number, b: number} {
  173. let match = color.match(HEX_LONG_COLOR_FORMAT);
  174. if (match) {
  175. return {
  176. r: parseInt(match[1], 16) / 255.0,
  177. g: parseInt(match[2], 16) / 255.0,
  178. b: parseInt(match[3], 16) / 255.0
  179. };
  180. }
  181. match = color.match(HEX_SHORT_COLOR_FORMAT);
  182. if (match) {
  183. return {
  184. r: parseInt(`${match[1]}${match[1]}`, 16) / 255.0,
  185. g: parseInt(`${match[2]}${match[2]}`, 16) / 255.0,
  186. b: parseInt(`${match[3]}${match[3]}`, 16) / 255.0
  187. };
  188. }
  189. match = color.match(RGB_COLOR_FORMAT) || color.match(RGBA_COLOR_FORMAT);
  190. if (match) {
  191. return {
  192. r: parseInt(match[1], 10) / 255.0,
  193. g: parseInt(match[2], 10) / 255.0,
  194. b: parseInt(match[3], 10) / 255.0
  195. };
  196. }
  197. return {
  198. r: 0,
  199. g: 0,
  200. b: 0
  201. };
  202. }
  203. /**
  204. * Shims style properties to work correctly on native. Allows us to minimize the
  205. * number of style declarations that need to be set or overridden for specific
  206. * platforms.
  207. *
  208. * @param {StyleSheet} styles - An object which represents a stylesheet.
  209. * @private
  210. * @returns {StyleSheet}
  211. */
  212. function _shimStyles<T: StyleSheet>(styles: T): T {
  213. // Certain style properties may not be numbers on Web but must be numbers on
  214. // React Native. For example, height and width may be expressed in percent
  215. // on Web but React Native will not understand them and we will get errors
  216. // (at least during development). Convert such well-known properties to
  217. // numbers if possible; otherwise, remove them to avoid runtime errors.
  218. for (const k of _WELL_KNOWN_NUMBER_PROPERTIES) {
  219. const v = styles[k];
  220. const typeofV = typeof v;
  221. if (typeofV !== 'undefined' && typeofV !== 'number') {
  222. const numberV = Number(v);
  223. if (Number.isNaN(numberV)) {
  224. delete styles[k];
  225. } else {
  226. styles[k] = numberV;
  227. }
  228. }
  229. }
  230. return styles;
  231. }