using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsApp2.Methods;
namespace WindowsFormsApp2.Color
{
public struct Color
// entrada do color metodo
{
private ColorMethod colorMethod;
public ColorMethod ColorMethod
{
get { return colorMethod; }
}
//colorRGB
private byte r;
private byte g;
private byte b;
public byte R
{
get { return r; }
set { r = value; }
}
public byte G { get { return g; }
}
public byte B { get { return b; }
}
// construtor
private Color(byte r,byte g,byte b)
{
colorMethod = ColorMethod.ByColor;
this.r = r;
this.g = g;
this.b = b;
}
public string Name
{
get
{
switch (colorMethod)
{
case ColorMethod.ByLayer:
return "ByLayer";
case ColorMethod.ByBlock:
return "ByBlock";
case ColorMethod.None:
return "None";
case ColorMethod.ByColor:
return string.Format("{0},{1},{2}", r, g, b);
default:
return "";
}
}
}
public static Color FromRGB(byte r, byte g, byte b)
{
return new Color(r, g, b);
}
public static Color FromColor(System.Drawing.Color color)
{
return new Color(color.R, color.G, color.B);
}
public static Color ByLayer
{
get
{
Color color = new Color();
color.colorMethod = ColorMethod.ByLayer;
color.r = 255;
color.g = 255;
color.b = 255;
return color;
}
}
public static Color ByBlock
{
get
{
Color color = new Color();
color.colorMethod = ColorMethod.ByBlock;
color.r = 255;
color.g = 255;
color.b = 255;
return color;
}
}
public override string ToString()
{
return string.Format("{0}:{1},{2},{3}", colorMethod.ToString(), r, g, b);
}
internal static bool TryParse(string str, out Color result)
{
string[] arr = str.Split(':');
if (arr.Length != 2)
{
result = Color.ByLayer;
return false;
}
//
ColorMethod colorMethod = (ColorMethod)Enum.Parse(typeof(WindowsFormsApp2.Color.Color), arr[0], true);
//
string[] rgb = arr[1].Split(',');
if (rgb.Length != 3)
{
result = Color.ByLayer;
return false;
}
byte red = 0;
byte green = 0;
byte blue = 0;
if (byte.TryParse(rgb[0], out red)
&& byte.TryParse(rgb[1], out green)
&& byte.TryParse(rgb[2], out blue))
{
result = new Color();
result.colorMethod = colorMethod;
result.r = red;
result.g = green;
result.b = blue;
return true;
}
else
{
result = Color.ByLayer;
return false;
}
}
public override bool Equals(object obj)
{
if (!(obj is Color))
return false;
return Equals((Color)obj);
}
public bool Equals(Color rhs)
{
if (colorMethod != rhs.colorMethod)
{
return false;
}
switch (colorMethod)
{
case ColorMethod.ByColor:
return r == rhs.r
&& g == rhs.g
&& b == rhs.b;
case ColorMethod.ByBlock:
case ColorMethod.ByLayer:
case ColorMethod.None:
default:
return true;
}
}
public override int GetHashCode()
{
return colorMethod.GetHashCode();
}
public static bool operator ==(Color lhs, Color rhs)
{
return lhs.Equals(rhs);
}
public static bool operator !=(Color lhs, Color rhs)
{
return !(lhs == rhs);
}
}
}