package main

import (
	"errors"
	"fmt"
)

func ConvertStringToColour(colourStr string) (int, error) {
	if len(colourStr) == 0 {
		return 0xFF0000, nil // Default colour if no string is provided
	}
	if colourStr[0] == '#' {
		colourStr = colourStr[1:]
	}
	if len(colourStr) != 6 && len(colourStr) != 3 {
		return 0, errors.New("colour must be a valid hex string")
	}
	var colour int
	_, err := fmt.Sscanf(colourStr, "%x", &colour)
	if err != nil {
		return 0, fmt.Errorf("invalid colour format: %v", err)
	}
	if len(colourStr) == 3 {
		r := (colour & 0xF00) >> 8
		g := (colour & 0x0F0) >> 4
		b := (colour & 0x00F) >> 0
		colour = (r << 16 << 4) | (g << 8 << 4) | (b << 0 << 4)
	}
	return colour, nil
}

func ConvertColourToString(colour int) string {
	return fmt.Sprintf("#%06X", colour)
}
