void GenerateBitmapWorker() { lock (syncWorker) workerAlive = true; for (;;) { float hue; lock (syncWorker) { if (!update) { workerAlive = false; break; } update = false; // Take a local copy of the hue to generate to avoid tearing hue = H; } lock (back) { unsafe { // Generate palette in HSV fixed(byte *cc = &back[0]) { var c = (int *)cc; for (var v = 0; v < 256; v++) { for (var s = 0; s < 256; s++) { *(c + (v * 256) + s) = HSLColor.FromHSV(hue, s / 255f, (255 - v) / 255f).RGB.ToArgb(); } } } } lock (front) { var swap = front; front = back; back = swap; } } } }
void GenerateBitmapWorker() { lock (syncWorker) workerAlive = true; for (;;) { float hue; lock (syncWorker) { if (!updateBack) { workerAlive = false; break; } updateBack = false; // Take a local copy of the hue to generate to avoid tearing hue = H; } var bitmapData = backBitmap.LockBits(backBitmap.Bounds(), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); unsafe { int *c = (int *)bitmapData.Scan0; // Generate palette in HSV for (var v = 0; v < 256; v++) { for (var s = 0; s < 256; s++) { *(c + (v * bitmapData.Stride >> 2) + s) = HSLColor.FromHSV(hue, s / 255f, (255 - v) / 255f).RGB.ToArgb(); } } } backBitmap.UnlockBits(bitmapData); lock (syncWorker) { var swap = swapBitmap; swapBitmap = backBitmap; backBitmap = swap; updateFront = true; } } }
public HSLColor RandomValidColor(MersenneTwister random, IEnumerable <Color> terrainColors, IEnumerable <Color> playerColors) { HSLColor color; Color forbidden; Action <string> ignoreError = _ => { }; do { var h = random.Next(255) / 255f; var s = float2.Lerp(HsvSaturationRange[0], HsvSaturationRange[1], random.NextFloat()); var v = float2.Lerp(HsvValueRange[0], HsvValueRange[1], random.NextFloat()); color = HSLColor.FromHSV(h, s, v); } while (!IsValid(color.RGB, out forbidden, terrainColors, playerColors, ignoreError)); return(color); }
public override void Initialize(WidgetArgs args) { base.Initialize(args); hueBitmap = new Bitmap(256, 256); hueSprite = new Sprite(new Sheet(new Size(256, 256)), new Rectangle(0, 0, 256, 1), TextureChannel.Alpha); var bitmapData = hueBitmap.LockBits(hueBitmap.Bounds(), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); unsafe { int *c = (int *)bitmapData.Scan0; for (var h = 0; h < 256; h++) { *(c + h) = HSLColor.FromHSV(h / 255f, 1, 1).RGB.ToArgb(); } } hueBitmap.UnlockBits(bitmapData); hueSprite.sheet.Texture.SetData(hueBitmap); }