Skip to content

Instantly share code, notes, and snippets.

@minitech
Last active September 11, 2015 08:02
Show Gist options
  • Save minitech/bf3e6e654cce695f3f85 to your computer and use it in GitHub Desktop.
Save minitech/bf3e6e654cce695f3f85 to your computer and use it in GitHub Desktop.
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
class ColorInterpolationApplication {
public static void Main() {
Application.Run(new ColorInterpolation());
}
}
class ColorInterpolation : Form {
public ColorInterpolation() : base() {
this.Paint += this.ColorInterpolation_Paint;
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
private static double Distance(int x, int y) {
return Math.Sqrt(x * x + y * y);
}
private void ColorInterpolation_Paint(object sender, PaintEventArgs e) {
int width = this.ClientSize.Width;
int height = this.ClientSize.Height;
using (var b = new Bitmap(width, height, PixelFormat.Format24bppRgb)) {
var d = b.LockBits(this.ClientRectangle, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
unsafe {
byte* p = (byte*)d.Scan0;
for (int y = 0; y < d.Height; y++) {
for (int x = 0; x < d.Width; x++) {
double rDist = Distance(x, y);
double gDist = Distance(width - x, y);
double bDist = Distance(x, height - y);
byte r = (byte)(255 - Math.Min(255, rDist / width * 255));
byte g = (byte)(255 - Math.Min(255, gDist / width * 255));
byte bl = (byte)(255 - Math.Min(255, bDist / height * 255));
*p++ = bl;
*p++ = g;
*p++ = r;
*p++ = 255;
}
}
}
b.UnlockBits(d);
e.Graphics.SetClip(this.ClientRectangle);
e.Graphics.DrawImageUnscaled(b, 0, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment