AnPv2/CSharp/Utils/Color.cs

140 lines
4.4 KiB
C#
Executable File

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace AnP.Utils{
public class Color{
public int red = 0x00;
public int green = 0x00;
public int blue = 0x00;
public int alpha = 0xFF;
public Color(string code){
if(code.StartsWith("#")){
code = code.Substring(1);
if(code.Length == 3 || code.Length == 4)
code = string.Concat(code.Select(c => $"{c}{c}"));
code = code.PadLeft(8, 'F');
alpha = Convert.ToInt32(code.Substring(0, 2), 16);
red = Convert.ToInt32(code.Substring(2, 2), 16);
green = Convert.ToInt32(code.Substring(4, 2), 16);
blue = Convert.ToInt32(code.Substring(6, 2), 16);
}else if(code.StartsWith("rgb")){
string[] items = code.Substring(code.IndexOf("(") + 1, code.LastIndexOf(")") - code.IndexOf("(") - 1).Split(",");
red = items.Length > 0 ? Convert.ToInt32(items[0]) : 0;
green = items.Length > 1 ? Convert.ToInt32(items[1]) : 0;
blue = items.Length > 2 ? Convert.ToInt32(items[2]) : 0;
alpha = items.Length > 3 ? (
items[3].Contains(".") ? (int)(Convert.ToSingle(items[3]) * 0xFF) :
Convert.ToInt32(items[3])) : 0xFF;
}
}
public Color(int color){
alpha = 0xFF - ((color >> 24) & 0xFF);
red = (color >> 16) & 0xFF;
green = (color >> 8) & 0xFF;
blue = color & 0xFF;
}
public Color(IEnumerable inputs, string type = "rgba"){
object[] array = ((IEnumerable)inputs).Cast<object>().ToArray();
if(type == "rgb" || type == "rgba" || true){
red = array.Length > 0 ? (int)array[0] : 0;
green = array.Length > 1 ? (int)array[1] : 0;
blue = array.Length > 2 ? (int)array[2] : 0;
alpha = array.Length > 3 ? (
Check.is_integer(array[3]) ? (int)array[3] :
Check.is_float(array[3]) ? (int)((float)array[3] * 0xFF) :
0xFF) : 0xFF;
}
}
public Color(Color color){
red = color.red;
green = color.green;
blue = color.blue;
alpha = color.alpha;
}
public string to_rgba(){
return $"rgba({red}, {green}, {blue}, {(float)alpha / 0xFF:0.##})";
}
public string to_rgb(){
return $"rgb({red}, {green}, {blue})";
}
public string to_hex(bool with_alpha = false){
return $"#{(with_alpha ? $"{alpha:X2}" : "")}{red:X2}{green:X2}{blue:X2}";
}
public static Color mix(params object[] colors){
if(colors.Length == 0)
return new Color(0);
Color _base;
int l = colors.Length;
if(Check.is_string(colors[0]))
_base = new Color((string)colors[0]);
else if(Check.is_integer(colors[0]))
_base = new Color((int)colors[0]);
else if(Check.is_array(colors[0]))
_base = new Color((IEnumerable)colors[0]);
else if(colors[0] is Color)
_base = new Color((Color)colors[0]);
else
return new Color(0);
if(l != 1){
for(int i = 1; i < l; i ++){
Color subcolor;
if(Check.is_string(colors[i]))
subcolor = new Color((string)colors[i]);
else if(Check.is_integer(colors[i]))
subcolor = new Color((int)colors[i]);
else if(Check.is_array(colors[i]))
subcolor = new Color((IEnumerable)colors[i]);
else if(colors[i] is Color)
subcolor = new Color((Color)colors[i]);
else
continue;
_base.red += subcolor.red;
_base.green += subcolor.green;
_base.blue += subcolor.blue;
_base.alpha += subcolor.alpha;
}
_base.red /= l;
_base.green /= l;
_base.blue /= l;
_base.alpha /= l;
};
return _base;
}
}
}