export const isHexCompatible = (hexColor?: string): boolean => {
  if (hexColor === undefined) {
    return true
  }

  const hexColorRegex = /^#([\dA-Fa-f]{6}|[\dA-Fa-f]{3})$/
  return hexColorRegex.test(hexColor)
}

export const rgbToHex = (rgb: string): string => {
  // Extract the integers by matching against a regex
  const result = rgb.match(/\d+/g)

  if (!result) {
    return '#000000' // Set default color to #000000 if the RGB string is invalid
  }

  const [red, green, blue] = result.map(Number)

  // Ensure the values are valid
  if (
    red < 0 ||
    red > 255 ||
    green < 0 ||
    green > 255 ||
    blue < 0 ||
    blue > 255
  ) {
    return '#000000' // Set default color to #000000 if the RGB values are invalid
  }

  // Convert each component to hex
  const redHex = red.toString(16).padStart(2, '0')
  const greenHex = green.toString(16).padStart(2, '0')
  const blueHex = blue.toString(16).padStart(2, '0')

  // Return the combined string
  return `#${redHex}${greenHex}${blueHex}`
}
