1/**
2 * Copyright (c) Expo.
3 * Copyright (c) Nicolas Gallagher.
4 *
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8import { processColor } from 'react-native';
9
10const isWebColor = (color: string): boolean =>
11  color === 'currentcolor' ||
12  color === 'currentColor' ||
13  color === 'inherit' ||
14  color.indexOf('var(') === 0;
15
16export function normalizeColor(color?: number | string, opacity: number = 1): void | string {
17  if (color == null) return;
18
19  if (typeof color === 'string' && isWebColor(color)) {
20    return color;
21  }
22
23  const colorInt = processColor(color) as number | undefined;
24  if (colorInt != null) {
25    const r = (colorInt >> 16) & 255;
26    const g = (colorInt >> 8) & 255;
27    const b = colorInt & 255;
28    const a = ((colorInt >> 24) & 255) / 255;
29    const alpha = (a * opacity).toFixed(2);
30    return `rgba(${r},${g},${b},${alpha})`;
31  }
32}
33