public IActionResult Post([FromBody] TransferInput input) { Image image = Image.CurrentImage; List <Color> NewPalette = ColorTransfer.FormatPalette(input.NewPalette); image.TransferPalette(NewPalette); return(Json(image.GetPixelArray())); }
public void OnTriggerStay2D(Collider2D other) { float dt = Time.deltaTime; ColorTransfer transferOther = other.GetComponent <ColorTransfer> (); if (transferOther == null) { return; } ColorModel colorModelOther = transferOther.GetColorModel(); float ColorAmt = _colorModel.GetColorAmount(); Vector3 RGBThis = _colorModel.GetRGB(); Vector3 RGBOther = colorModelOther.GetRGB(); float densityThis = _colorModel.GetDensity(); float densityOther = colorModelOther.GetDensity(); for (int i = 0; i < 3; i++) { float CThis = RGBThis [i]; float COther = RGBOther [i]; float deltaC = _DeltaFactor * (CThis - COther) + 1.0f; float CAbsorb = dt * _AbsorbPower * ColorAmt * densityThis / (deltaC * densityOther); CAbsorb = Mathf.Min(RGBOther [i], CAbsorb); RGBOther [i] -= CAbsorb; RGBThis [i] += CAbsorb; } _colorModel.SetRGB(RGBThis); colorModelOther.SetRGB(RGBOther); }
/// <summary> /// Applies the filter function <paramref name="f"/> to the values in the image. /// If the image is a gray scale image, the intensity is used for all three color channels of the input value /// and the intensity of the green output channel is written to the image. /// </summary> /// <param name="bmp">The bitmap to filter.</param> /// <param name="f">The transfer function.</param> /// <exception cref="ArgumentNullException">Is thrown, /// if <c>null</c> is given for <paramref name="bmp"/>, or <paramref name="f"/>.</exception> /// <exception cref="ArgumentException">Is thrown, /// if the pixel format of <paramref name="bmp"/> is not on of the following: /// <see cref="PixelFormat.Format8bppIndexed"/>, /// <see cref="PixelFormat.Format24bppRgb"/>, /// or <see cref="PixelFormat.Format32bppArgb"/>. /// </exception> public static void ApplyFilter(this Bitmap bmp, ColorTransfer f) { if (bmp == null) throw new ArgumentNullException("bmp"); if (f == null) throw new ArgumentNullException("f"); var w = bmp.Width; var h = bmp.Height; BitmapData bmpData; IntPtr line; switch (bmp.PixelFormat) { case PixelFormat.Format8bppIndexed: bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); line = bmpData.Scan0; for (var y = 0; y < h; y++) { for (var x = 0; x < w; x++) { var v = Marshal.ReadByte(line, x); var c = Color.FromArgb(v, v, v); c = f(x, y, c); Marshal.WriteByte(line, x, c.G); } line += bmpData.Stride; } bmp.UnlockBits(bmpData); break; case PixelFormat.Format24bppRgb: bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); line = bmpData.Scan0; for (var y = 0; y < h; y++) { for (var x = 0; x < w; x++) { var ofs = x * 3; var c = Color.FromArgb( Marshal.ReadByte(line, ofs + 2), Marshal.ReadByte(line, ofs + 1), Marshal.ReadByte(line, ofs + 0)); c = f(x, y, c); Marshal.WriteByte(line, ofs + 0, c.B); Marshal.WriteByte(line, ofs + 1, c.G); Marshal.WriteByte(line, ofs + 2, c.R); } line += bmpData.Stride; } bmp.UnlockBits(bmpData); break; case PixelFormat.Format32bppArgb: bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); line = bmpData.Scan0; for (var y = 0; y < h; y++) { for (var x = 0; x < w; x++) { var ofs = x * 4; var c = Color.FromArgb(Marshal.ReadInt32(line, ofs)); c = f(x, y, c); Marshal.WriteInt32(line, ofs, c.ToArgb()); } line += bmpData.Stride; } bmp.UnlockBits(bmpData); break; default: throw new ArgumentException("The pixel format of the given bitmap is not supported.", "bmp"); } }
/// <summary> /// Perform Palette-based Color Transfer /// </summary> /// <param name="newPalette"></param> public void TransferPalette(List <Color> newPalette) { this.Pixels = ColorTransfer.Transfer(this, newPalette); this.Edits.Add(new Image(this.Pixels)); }