function colorToRgba(input: string): string {
  let int32Color = normalizeColor(input);
  if (int32Color === null) {
    return input;
  }

  int32Color = int32Color || 0;

  const r = (int32Color & 0xff000000) >>> 24;
  const g = (int32Color & 0x00ff0000) >>> 16;
  const b = (int32Color & 0x0000ff00) >>> 8;
  const a = (int32Color & 0x000000ff) / 255;

  return `rgba(${r}, ${g}, ${b}, ${a})`;
}
Beispiel #2
0
const processColor = (color: ?(string | number), opacity: number = 1) => {
  if (
    color === undefined ||
    color === null ||
    (opacity === 1 && typeof color === 'string' && color.charAt(0) !== '#')
  ) {
    return color;
  }

  // convert number and hex
  const int32Color = normalizeColor(color);
  if (int32Color === null) {
    return undefined;
  }

  // convert 0xrrggbbaa into rgba
  const rgba = normalizeColor.rgba(int32Color);
  rgba.a = rgba.a.toFixed(1);
  const { r, g, b, a } = rgba;
  return `rgba(${r},${g},${b},${a * opacity})`;
};