Last active
September 2, 2025 14:26
-
-
Save diogotito/0149dc2dc120c4b06ff5a2628993a004 to your computer and use it in GitHub Desktop.
A CodeLab BitmapEffect that draws a pixel art target in Paint.NET
This file contains hidden or 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
| // Name: Procedural Target | |
| // Submenu: AAAAAAAA | |
| // Author: Diogo Marques | |
| // Title: Procedural Target | |
| // Version: 0.01 | |
| // Desc: Draws a pixel art target, procedurally. Recommended image size: 17x17 | |
| // Keywords: | |
| // URL: https://gist.github.com/diogotito/0149dc2dc120c4b06ff5a2628993a004 | |
| // Help: | |
| // For help writing a Bitmap plugin: https://boltbait.com/pdn/CodeLab/help/tutorial/bitmap/ | |
| static readonly ColorBgra32[] TARGET_COLORS = [ | |
| ColorBgra.GreenYellow, | |
| ColorBgra.OrangeRed, | |
| ColorBgra.DeepSkyBlue, | |
| //ColorBgra.DeepSkyBlue, | |
| ColorBgra.Black, | |
| ColorBgra.Black, | |
| ColorBgra.White, | |
| ColorBgra.White, | |
| ]; | |
| ColorBgra32 ComputePixel(double x, double y) | |
| { | |
| double distance = Math.Sqrt(x * x + y * y); | |
| int iDist = (int) Math.Round(distance); | |
| if (iDist >= TARGET_COLORS.Length) return ColorBgra.Transparent; | |
| return TARGET_COLORS[iDist]; | |
| } | |
| #region UICode | |
| #endregion | |
| protected override void OnRender(IBitmapEffectOutput output) | |
| { | |
| using IEffectInputBitmap<ColorBgra32> sourceBitmap = Environment.GetSourceBitmapBgra32(); | |
| using IBitmapLock<ColorBgra32> sourceLock = sourceBitmap.Lock(new RectInt32(0, 0, sourceBitmap.Size)); | |
| RegionPtr<ColorBgra32> sourceRegion = sourceLock.AsRegionPtr(); | |
| RectInt32 outputBounds = output.Bounds; | |
| using IBitmapLock<ColorBgra32> outputLock = output.LockBgra32(); | |
| RegionPtr<ColorBgra32> outputSubRegion = outputLock.AsRegionPtr(); | |
| var outputRegion = outputSubRegion.OffsetView(-outputBounds.Location); | |
| // Delete any of these lines you don't need | |
| int canvasCenterX = Environment.Document.Size.Width / 2; | |
| int canvasCenterY = Environment.Document.Size.Height / 2; | |
| // Loop through the output canvas tile | |
| for (int y = outputBounds.Top; y < outputBounds.Bottom; ++y) | |
| { | |
| if (IsCancelRequested) return; | |
| for (int x = outputBounds.Left; x < outputBounds.Right; ++x) | |
| { | |
| double px = 0.5 + (double) x; | |
| double py = 0.5 + (double) y; | |
| double cx = 0.5 + (double) canvasCenterX; | |
| double cy = 0.5 + (double) canvasCenterY; | |
| // Save your pixel to the output canvas | |
| outputRegion[x,y] = ComputePixel(cx - px, cy - py); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment