using UnityEngine;

public static class ColorExtensions {

    public static Color invisibleWhite = Color.white - Color.black;

    public static Color Add(this Color c1, Color c2){
        c1 += c2;
        c1.r = Mathf.Clamp(c1.r, 0.0f, 1.0f);
        c1.g = Mathf.Clamp(c1.g, 0.0f, 1.0f);
        c1.b = Mathf.Clamp(c1.b, 0.0f, 1.0f);
        c1.a = Mathf.Clamp(c1.a, 0.0f, 1.0f);
        return c1;
    }

    public static Color AddR(this Color c, float f){
        return c.SetR(c.r + f);
    }
    public static Color SetR(this Color c, float f){
        c.r = Mathf.Clamp(f, 0.0f, 1.0f);
        return c;
    }
    public static Color AddG(this Color c, float f){
        return c.SetG(c.g + f);
    }
    public static Color SetG(this Color c, float f){
        c.g = Mathf.Clamp(f, 0.0f, 1.0f);
        return c;
    }
    public static Color AddB(this Color c, float f){
        return c.SetB(c.b + f);
    }
    public static Color SetB(this Color c, float f){
        c.b = Mathf.Clamp(f, 0.0f, 1.0f);
        return c;
    }
    public static Color AddA(this Color c, float f){
        return c.SetA(c.a + f);
    }
    public static Color SetA(this Color c, float f){
        c.a = Mathf.Clamp(f, 0.0f, 1.0f);
        return c;
    }

    private static string Slice(this string aString, int num){
        return aString.Substring(num, aString.Length - 1);
    }

    public static string ToColorCode(this Color color){
        var color32 = (Color32)color;
        int x = (((256 + ((int)color32.r) << 8) + ((int)color32.g) << 8) + ((int)color32.b));
        return "#" + x.ToString("X").Slice(1);
    }
}
