using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZTImage;
using ZTImage.Collections;
namespace RichCreator.Utility.Structs
{
///
/// 颜色数组
///
public class ColorArray
{
public static ColorArray Empty = default(ColorArray);
public ColorArray(Int32 processMode,ZTPoint basePosition,ZTSize size,List colors)
{
this.ProcessMode = processMode;
this.BasePosition = basePosition;
this.Size = size;
this.Colors = colors;
}
///
/// 原始位置
///
public ZTPoint BasePosition;
///
/// 处理模式
/// 0:原图
/// 1:灰度
/// 2:二值化
/// 3:hsv float
///
public Int32 ProcessMode;
///
/// 二值化的阀值
///
public Int32 ThresholdValue;
///
/// 大小
///
public ZTSize Size;
///
/// 颜色列表
///
public List Colors;
///
/// 在图像数据中比对
///
///
///
///
///
public bool Compare(byte[,,] datas, Int32 x, Int32 y)
{
Int32 ax = 0, ay = 0;
for (int i = 0; i < this.Colors.Count; i++)
{
ax = x + this.Colors[i].Offset.X;
ay = y + this.Colors[i].Offset.Y;
if (!this.Colors[i].Compare(datas[ay, ax, 0], datas[ay, ax, 1], datas[ay, ax, 2]))
{
return false;
}
}
return true;
}
///
/// 从字符串生成二值化识别方式
///
/// 形如:1,2,23,231,33$2,3,1,2,3$....每个组用$分隔,逗号分割的为:x,y,r,g,b
///
public static ColorArray FromThresholdString(Int32 thresholdValue,string colorsString)
{
return FromColorString(2, thresholdValue, colorsString);
}
///
/// 从字符串生成二值化识别方式
///
/// 形如:1,2,23,231,33$2,3,1,2,3$....每个组用$分隔,逗号分割的为:x,y,r,g,b
///
public static ColorArray FromColorString(Int32 processMode,Int32 thresholdValue, string colorsString)
{
MultiList arrayItem = SplitColorsString(colorsString);
Int32 x, y, right, bottom;
RelocationColorArrayPosition(arrayItem, out x, out y, out right, out bottom);
List items = new List();
for (int i = 0; i < arrayItem.Count; i++)
{
items.Add(new ZTColorArrayItem(new ZTPoint(arrayItem[i].Item1, arrayItem[i].Item2), new ZTColor(arrayItem[i].Item3, arrayItem[i].Item4, arrayItem[i].Item5)));
}
ColorArray array = new ColorArray(processMode, new ZTPoint(x, y), new ZTSize(right + 1, bottom + 1), items);
array.ThresholdValue = thresholdValue;
return array;
}
///
/// 从字符串生成hsv方式的识别颜色数组
///
///
///
///
public static ColorArray FromHsvFloatString(float hOffset,float sOffset,float vOffset,string colorString)
{
hOffset = Math.Abs(hOffset);
sOffset = Math.Abs(sOffset);
vOffset = Math.Abs(vOffset);
MultiList arrayItem = SplitColorsString(colorString);
Int32 basePositionX, basePositionY, right, bottom;
RelocationColorArrayPosition(arrayItem, out basePositionX, out basePositionY, out right, out bottom);
List items = new List();
for (int i = 0; i < arrayItem.Count; i++)
{
items.Add(new ZTHsvFloatColorArrayItem(hOffset,sOffset,vOffset,new ZTPoint(arrayItem[i].Item1, arrayItem[i].Item2), new ZTColor(arrayItem[i].Item3, arrayItem[i].Item4, arrayItem[i].Item5)));
}
ColorArray array = new ColorArray(3, new ZTPoint(basePositionX, basePositionY), new ZTSize(right + 1, bottom + 1), items);
return array;
}
///
/// 分隔原始颜色字符串
///
/// 形如:1,2,23,231,33$2,3,1,2,3$....每个组用$分隔,逗号分割的为:x,y,r,g,b
///
private static MultiList SplitColorsString(string colorArrayString)
{
MultiList list = new MultiList();
if (string.IsNullOrEmpty(colorArrayString))
{
return list;
}
string[] colors = colorArrayString.Split(new char[] { '$' },StringSplitOptions.RemoveEmptyEntries);
Int32 x,y;
Int32 r, g, b;
for (int i = 0; i < colors.Length; i++)
{
string[] items = colors[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
x=TypeConverter.StringToInt(items[0], -1);
if (x < 0)
{
continue;
}
y = TypeConverter.StringToInt(items[1], -1);
if (y < 0)
{
continue;
}
r = TypeConverter.StringToInt(items[2], -1);
if (r < 0&&r>255)
{
continue;
}
g = TypeConverter.StringToInt(items[3], -1);
if (g < 0&&g>255)
{
continue;
}
b = TypeConverter.StringToInt(items[4], -1);
if (b < 0&&b>255)
{
continue;
}
list.Add(x,y,(byte)r,(byte)g,(byte)b);
}
return list;
}
///
/// 格式化array的坐标
///
///
private static void RelocationColorArrayPosition(MultiList colorArray,out Int32 x,out Int32 y,out Int32 right,out Int32 bottom)
{
x=Int32.MaxValue;
y=Int32.MaxValue;
right = 0;
bottom = 0;
for (int i = 0; i < colorArray.Count; i++)
{
if (colorArray[i].Item1 < x)
{
x = colorArray[i].Item1;
}
if (colorArray[i].Item2 < y)
{
y=colorArray[i].Item2;
}
}
for (int i = 0; i < colorArray.Count; i++)
{
MultiObject item = colorArray[i];
colorArray[i] = new MultiObject(item.Item1 - x, item.Item2 - y, item.Item3, item.Item4, item.Item5);
if (colorArray[i].Item1 > right)
{
right = colorArray[i].Item1;
}
if (colorArray[i].Item2 > bottom)
{
bottom = colorArray[i].Item2;
}
}
}
}
}