/** Converts a hex color + 0–100 opacity to rgba (editor/viewer convention). */
export const getColorWithOpacity = (fill?: string, opacity?: number): string => {
  if (!fill) return "#000000";
  if (opacity === undefined || opacity >= 100) return fill;

  const hex = fill.replace("#", "");
  if (!/^[0-9A-Fa-f]{3,8}$/.test(hex)) return fill;

  const paddedHex =
    hex.length === 3
      ? hex
          .split("")
          .map((c) => c + c)
          .join("")
      : hex.length === 4
        ? hex
            .split("")
            .map((c) => c + c)
            .join("")
        : hex.padEnd(6, "0").slice(0, 6);

  const r = parseInt(paddedHex.substring(0, 2), 16);
  const g = parseInt(paddedHex.substring(2, 4), 16);
  const b = parseInt(paddedHex.substring(4, 6), 16);
  const alpha = opacity / 100;

  return `rgba(${r}, ${g}, ${b}, ${alpha})`;
};
