Created
April 8, 2014 21:56
-
-
Save vbfox/10199082 to your computer and use it in GitHub Desktop.
Generate a bitmap composed of random squares in different shades of blue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var bmp = new Bitmap(1920, 1200); | |
const int size = 20; | |
Random r = new Random(); | |
Color colorA = Color.FromArgb(0, 148, 255); | |
Color colorB = Color.FromArgb(145, 216, 255); | |
using(var g = Graphics.FromImage(bmp)) | |
{ | |
int y = 0; | |
while(y < bmp.Height) | |
{ | |
int x = 0; | |
while(x < bmp.Width) | |
{ | |
var color = ColorInterpolator.InterpolateBetween(colorA, colorB, r.NextDouble()); | |
g.FillRectangle(new SolidBrush(color), x, y, x + size, y + size); | |
x += size; | |
} | |
y += size; | |
} | |
} | |
bmp.Save(@"F:\Temp\twitter.png"); | |
using(var form = new Form()) | |
{ | |
var p = new PictureBox(); | |
p.Image = bmp; | |
p.Dock = DockStyle.Fill; | |
form.Controls.Add(p); | |
Application.Run(form); | |
} | |
} | |
// https://stackoverflow.com/questions/1236683/color-interpolation-between-3-colors-in-net | |
class ColorInterpolator { | |
delegate byte ComponentSelector(Color color); | |
static ComponentSelector _redSelector = color => color.R; | |
static ComponentSelector _greenSelector = color => color.G; | |
static ComponentSelector _blueSelector = color => color.B; | |
public static Color InterpolateBetween( | |
Color endPoint1, | |
Color endPoint2, | |
double lambda) { | |
if (lambda < 0 || lambda > 1) { | |
throw new ArgumentOutOfRangeException("lambda"); | |
} | |
Color color = Color.FromArgb( | |
InterpolateComponent(endPoint1, endPoint2, lambda, _redSelector), | |
InterpolateComponent(endPoint1, endPoint2, lambda, _greenSelector), | |
InterpolateComponent(endPoint1, endPoint2, lambda, _blueSelector) | |
); | |
return color; | |
} | |
static byte InterpolateComponent( | |
Color endPoint1, | |
Color endPoint2, | |
double lambda, | |
ComponentSelector selector) { | |
return (byte)(selector(endPoint1) | |
+ (selector(endPoint2) - selector(endPoint1)) * lambda); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment