package be.seeseepuff.allowanceplanner.util;

public class ColourUtil {
    private ColourUtil() {
    }

    public static int convertStringToColour(String colourStr) {
        if (colourStr == null || colourStr.isEmpty()) {
            return 0xFF0000; // Default colour
        }
        if (colourStr.charAt(0) == '#') {
            colourStr = colourStr.substring(1);
        }
        if (colourStr.length() != 6 && colourStr.length() != 3) {
            throw new IllegalArgumentException("colour must be a valid hex string");
        }
        int colour = Integer.parseInt(colourStr, 16);
        if (colourStr.length() == 3) {
            int r = (colour & 0xF00) >> 8;
            int g = (colour & 0x0F0) >> 4;
            int b = (colour & 0x00F);
            colour = (r << 20) | (g << 12) | (b << 4);
        }
        return colour;
    }

    public static String convertColourToString(Integer colour) {
        if (colour == null) {
            return "";
        }
        return String.format("#%06X", colour);
    }
}
