package net.ranides.assira.ui.model;

public class CssHexCode {

    private final String value;

    private CssHexCode(String value) {
        this.value = value;
    }

    public static CssHexCode of(String value) {
        return new CssHexCode(value);
    }

    public int components() {
        switch (value.length()) {
            case 4:
            case 7:
                return 3;
            case 5:
            case 9:
                return 4;
            default:
                throw new IllegalArgumentException("Invalid hex code: " + value);
        }
    }

    public int component(int index) {
        switch (value.length()) {
            case 4:
            case 5:
                return 17 * Integer.parseInt(value.substring(1+index,2+index), 16);
            case 7:
            case 9:
                return Integer.parseInt(value.substring(1+2*index,3+2*index), 16);
            default:
                throw new IllegalArgumentException("Invalid hex code: " + value);
        }
    }
}
