package com.iqser.red.service.persistence.management.v1.processor.utils;

import java.awt.Color;
import java.util.Locale;
import java.util.regex.Pattern;

import com.iqser.red.service.persistence.management.v1.processor.exception.BadRequestException;

import lombok.experimental.UtilityClass;

@UtilityClass
public final class ColorUtils {

    public static final Pattern COLOR_PATTERN = Pattern.compile("^#[\\da-f]{6,6}$");


    public static void validateColor(String color) {

        if (color == null) {
            throw new BadRequestException("Color not set");
        }
        if (!COLOR_PATTERN.matcher(color.toLowerCase(Locale.ROOT)).matches()) {
            throw new BadRequestException("The specified color: " + color + " is malformed.");
        }
    }

    public static float[] convertColor(String hex) {

        Color color = Color.decode(hex);
        return new float[]{color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f};
    }

}
