status-web/packages/components/utils/get-color-with-opacity.ts

22 lines
516 B
TypeScript

import { opacity as colorOpacity } from '../consts/opacity'
export type RGBAColor = `rgba(${string} / ${string}%)`
export type HexColor = `#${string}`
type Opacity = keyof typeof colorOpacity
export function getColorWithOpacity(
color: RGBAColor | HexColor,
opacity: Opacity
): string {
if (color.startsWith('rgba')) {
return color
.replace(' / ', '/')
.replace(/\s/g, ',')
.replace(/(\d+)%/, `${colorOpacity[opacity].decimal}`)
}
return `${color}${colorOpacity[opacity].hex}`
}