private static System.Drawing.Color CalculateAverageBoundariesColor(Bitmap bm) { int width = bm.Width; int height = bm.Height; // cutting corners, will fail on anything else but 32 and 24 bit images int bppModifier = bm.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb ? 4 : 3; BitmapData srcData = bm.LockBits(new System.Drawing.Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, bm.PixelFormat); try { PixelColors avgColorNew; PixelColors avgColorOld = GetAverageColor(width, height, srcData, bppModifier); while (true) { avgColorNew = GetAverageColor(width, height, srcData, bppModifier, avgColorOld); if (!avgColorNew.IsDiffer(avgColorOld, 5)) { break; } avgColorOld = avgColorNew; } return(System.Drawing.Color.FromArgb(avgColorNew.Red, avgColorNew.Green, avgColorNew.Blue)); } finally { bm.UnlockBits(srcData); } }
private void UpdateStatistics(Collision2D collision) { PixelColors cc = collision.gameObject.GetComponent <PixelController>().getColor(); //Debug.Log(cc); //Debug.Log(Stats); Stats.Colors.Add(cc); Stats._stats.Add(new Statistic { EPixColor = cc, iCount = 1, fTTL = collision.gameObject.GetComponent <PixelController>().getTTL(), ftimeStamp = Time.time, }); if (collision.gameObject.GetComponent <PixelController>().getType() == PixelType.speed) { Stats.fXtraSpeed++; } List <string> output = new List <string>(); foreach (var e in Stats._stats) { output.Add(e.EPixColor + "$" + e.iCount + "$" + e.fTTL + "$" + e.ftimeStamp + ";") ; } GameManager.WriteData(output, output.Count); }
private void SpawnPixel(PixelColors pc) { var x = UnityEngine.Random.Range(minx, maxx); var y = UnityEngine.Random.Range(miny, maxy); GameObject GO = Instantiate(PixelTemplate); PixelController pctrl = GO.GetComponent <PixelController>(); GO.GetComponent <PixelController>().AdjustSettings2(pc, 1); GO.transform.position = new Vector2(x, y); }
/* * private void CreateColorDic() * { * Color_Dic = new Dictionary<PixelColors, Color>(); * Color_Dic.Add(PixelColors.blue, Color.blue); * Color_Dic.Add(PixelColors.cyan, Color.cyan); * Color_Dic.Add(PixelColors.green, Color.green); * Color_Dic.Add(PixelColors.pink, Color.magenta); * Color_Dic.Add(PixelColors.red, Color.red); * Color_Dic.Add(PixelColors.gold, new Color(0.95f, 0.75f, 0)); * } */ public void AdjustSettings2(PixelColors pc, float size) { Debug.Log("vorher " + EPixelColor); cPixelHue = GameManager.Color_Dic[pc]; EPixelColor = pc; Debug.Log("nacher" + EPixelColor); tPixelTimer = 0f; //gameObject.tag = "Pixel"; renderer.material.SetColor("_MainColor", cPixelHue); //renderer.material.SetColor("_MainColor", cPixelHue); RB.mass = baseMass + (baseMass * massInertia * (size - 1)); cPixelTransparent = new Color(cPixelHue.r, cPixelHue.g, cPixelHue.b, 0); }
public bool IsDiffer(PixelColors color, int diff) { if (Math.Abs(this.Red - color.Red) > diff) { return(true); } if (Math.Abs(this.Green - color.Green) > diff) { return(true); } if (Math.Abs(this.Blue - color.Blue) > diff) { return(true); } return(false); }
private static PixelColors GetAverageColor(int width, int height, BitmapData srcData, int bppModifier, PixelColors oldAvgColor = null) { PixelColors avgColor = new PixelColors(); double[] totals = new double[] { 0, 0, 0 }; double red = 0; double green = 0; double blue = 0; int stride = srcData.Stride; IntPtr Scan0 = srcData.Scan0; unsafe { byte *p = (byte *)(void *)Scan0; for (int heightIndex = 0; heightIndex < height; heightIndex++) { for (int widthIndex = 0; widthIndex < width;) { int idx = (heightIndex * stride) + widthIndex * bppModifier; if (oldAvgColor != null) { if (oldAvgColor.Red != 0) { if (p[idx + 2] / oldAvgColor.Red < 0.5 || p[idx + 2] / oldAvgColor.Red > 2) { p[idx + 2] = (byte)oldAvgColor.Red; } } if (oldAvgColor.Green != 0) { if (p[idx + 1] / oldAvgColor.Green < 0.5 || p[idx + 1] / oldAvgColor.Green > 2) { p[idx + 1] = (byte)oldAvgColor.Green; } } if (oldAvgColor.Blue != 0) { if (p[idx] / oldAvgColor.Blue < 0.5 || p[idx] / oldAvgColor.Blue > 2) { p[idx] = (byte)oldAvgColor.Blue; } } } red = p[idx + 2]; green = p[idx + 1]; blue = p[idx]; totals[2] += red; totals[1] += green; totals[0] += blue; if ((heightIndex == 0) || (heightIndex == height - 1)) { widthIndex++; } else if (widthIndex == 0) { widthIndex = width - 1; } else { break; } } } int count = 2 * width + 2 * (height - 2); avgColor.Red = (int)(totals[2] / count); avgColor.Green = (int)(totals[1] / count); avgColor.Blue = (int)(totals[0] / count); return(avgColor); } }
public static Bitmap ChangeColor(this Bitmap bmp, PixelColors color) { const int INCREMENTO = 4; Bitmap bmpResultado = bmp.Clone(PixelFormat.Format32bppArgb);//asi hago que todas las imagenes tengan el mismo formato :D TratarImg metodo = null; unsafe { switch (color) { case PixelColors.Red: metodo = ToRojo; break; case PixelColors.Green: metodo =ToVerde; break; case PixelColors.Blue: metodo = ToAzul; break; case PixelColors.Sepia: metodo = ToSepia; break; case PixelColors.GrayScale: metodo = ToEscalaDeGrises; break; case PixelColors.Inverted: metodo = ToInvertido; break; } bmpResultado.TrataBytes((ptrBytesBmpResultado) => { for (byte* ptrFin=ptrBytesBmpResultado+bmp.Width*bmp.Height*INCREMENTO; ptrBytesBmpResultado!=ptrFin; ptrBytesBmpResultado += INCREMENTO) { metodo(ptrBytesBmpResultado); } }); } return bmpResultado; }