/**
 * Copyright 2024 Phenix Real Time Solutions, Inc. Confidential and Proprietary. All Rights Reserved.
 */

export class ColorUtils {
  /**
   * Converts a hex color to RGBA format
   * @param hex - The hex color string (with or without #)
   * @param opacityPercentage - Opacity as percentage (1-100)
   * @returns RGBA string or null if invalid hex
   */
  static hexToRgba(hex: string, opacityPercentage: number): string | null {
    const rgbParsed = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);

    if (!rgbParsed) {
      return null;
    }

    const rgb = {
      r: parseInt(rgbParsed[1], 16),
      g: parseInt(rgbParsed[2], 16),
      b: parseInt(rgbParsed[3], 16)
    };

    const rgba =
      opacityPercentage > 100 || opacityPercentage < 1
        ? `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 1)`
        : `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${opacityPercentage / 100})`;

    return rgba;
  }
}
