/// <summary>色情報を入力する</summary> /// <param name="buf">カラー配列</param> private void SetColor(byte[] buf) { Random rnd = new Random(); List <Point3D>[] colorLists = new List <Point3D> [colors.Length]; for (int i = 0; i < colorLists.Length; i++) { colorLists[i] = new List <Point3D>(); } HashSet <Point3D> hash = new HashSet <Point3D>(); for (int i = 0; i < buf.Length; i += 4) { int n = rnd.Next(0, colors.Length); LAB lab = LAB.FromRGB(buf[i + 0], buf[i + 1], buf[i + 2]); Point3D point = new Point3D(lab.L, lab.A, lab.B); if (hash.Add(point)) { colorLists[n].Add(point); } } for (int i = 0; i < colors.Length; i++) { colors[i] = colorLists[i]; } }
/// <summary>Colorクラスを作成する</summary> /// <param name="c">色</param> /// <returns>RGBの色<see cref="Color"/></returns> public static Color ToRGB(LAB c) { double y = (c.L + 16) / 116; double x = c.A / 500 + y; double z = y - c.B / 200; double Xn = 98.072; double Yn = 100.000; double Zn = 118.225; double[] xyz = new double[3]; xyz[0] = Math.Pow(x, 3) * Xn; xyz[1] = Math.Pow(y, 3) * Yn; xyz[2] = Math.Pow(z, 3) * Zn; xyz[0] /= 100; xyz[1] /= 100; xyz[2] /= 100; double[] srgb = new double[3]; srgb[0] = 3.5064 * xyz[0] - 1.7400 * xyz[1] - 0.5441 * xyz[2]; srgb[1] = -1.0690 * xyz[0] + 1.9777 * xyz[1] + 0.0352 * xyz[2]; srgb[2] = 0.0563 * xyz[0] - 0.1970 * xyz[1] + 1.0511 * xyz[2]; double[] rgb = new double[3]; rgb[0] = Math.Pow(srgb[0], 1 / 2.2); rgb[1] = Math.Pow(srgb[1], 1 / 2.2); rgb[2] = Math.Pow(srgb[2], 1 / 2.2); byte r = (byte)(rgb[0] * 255); byte g = (byte)(rgb[1] * 255); byte b = (byte)(rgb[2] * 255); return(Color.FromArgb(r, g, b)); }
/// <summary>カラー配列の置き換え</summary> /// <param name="buf">カラー配列</param> private void Replace(byte[] buf) { for (int i = 0; i < buf.Length; i += 4) { LAB lab = LAB.FromRGB(buf[i + 0], buf[i + 1], buf[i + 2]); Point3D c = new Point3D(lab.L, lab.A, lab.B); int min = 0; double minR = double.MaxValue; for (int k = 0; k < center.Length; k++) { double r = Range(c, center[k]); if (minR > r) { min = k; minR = r; } } Color color = LAB.ToRGB(LAB.FromLAB(center[min].X, center[min].Y, center[min].Z)); buf[i + 0] = color.R; buf[i + 1] = color.G; buf[i + 2] = color.B; } }