private void canvasColor_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs <Color?> e) { if (!canvasColor.SelectedColor.HasValue) { return; } Color col = canvasColor.SelectedColor.Value; byte[] packet = LEDSetup.getMagicHeader(); for (int i = 0; i < LEDSetup.LED_C; i++) { LEDSetup.processColorSimple(i, packet, col.R, col.G, col.B); } LEDSetup.sendSerialData(packet); }
public void draw() { if (LEDSetup.OVERRIDE) { return; } if (device == null) { return; } byte[] serialData = LEDSetup.getMagicHeader(); //Bitmap screenBitmap = getScreenGDI(); Bitmap screenBitmap = getScreenDX(); if (screenBitmap == null) { return; } #if true for (int i = 0; i < LEDSetup.LED_C; i++) { int r = 0, g = 0, b = 0, c = 0; long re = 0, gr = 0, bl = 0; #if true var rect = new System.Drawing.Rectangle((int)LEDSetup.leds[i].Coords.X, (int)LEDSetup.leds[i].Coords.Y, (int)LEDSetup.leds[i].Coords.Width, (int)LEDSetup.leds[i].Coords.Height); byte[] dat; lock (screenBitmap) { var bmpData = screenBitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb); dat = new byte[bmpData.Stride * bmpData.Height]; System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, dat, 0, dat.Length); screenBitmap.UnlockBits(bmpData); } for (int d = 0; d < dat.Length; d += 4 * 60) { // Format is actually Bgr despite the name above being Rgb bl += dat[d] * dat[d]; gr += dat[d + 1] * dat[d + 1]; re += dat[d + 2] * dat[d + 2]; c++; } #else for (int x = (int)LEDSetup.leds[i].Coords.Left; x < (int)LEDSetup.leds[i].Coords.Right; x += 10) { for (int y = (int)LEDSetup.leds[i].Coords.Top; y < (int)LEDSetup.leds[i].Coords.Bottom; y += 10) { System.Drawing.Color col; lock (screenBitmap) { col = screenBitmap.GetPixel(x, y); } re += (long)(col.R * col.R); gr += (long)(col.G * col.G); bl += (long)(col.B * col.B); c++; } } #endif r = (int)(Math.Sqrt(re / c) * rc); g = (int)(Math.Sqrt(gr / c) * gc); b = (int)(Math.Sqrt(bl / c) * bc); if (r > 255) { r = 255; } if (g > 255) { g = 255; } if (b > 255) { b = 255; } LEDSetup.processColor(i, serialData, r, g, b); } #else Parallel.For(0, LEDSetup.LED_C, (i) => { int r = 0, g = 0, b = 0, c = 0; long re = 0, gr = 0, bl = 0; for (int x = (int)LEDSetup.leds[i].Coords.Left; x < (int)LEDSetup.leds[i].Coords.Right; x += 10) { for (int y = (int)LEDSetup.leds[i].Coords.Top; y < (int)LEDSetup.leds[i].Coords.Bottom; y += 10) { System.Drawing.Color col; lock (screenBitmap) { col = screenBitmap.GetPixel(x, y); } if (false) { re += (long)col.R; gr += (long)col.G; bl += (long)col.B; } else { re += (long)(col.R * col.R); gr += (long)(col.G * col.G); bl += (long)(col.B * col.B); } c++; } } if (false) { r = (int)(re / c); g = (int)(gr / c); b = (int)(bl / c); } else { r = (int)Math.Sqrt(re / c); g = (int)Math.Sqrt(gr / c); b = (int)Math.Sqrt(bl / c); } if (r > 255) { r = 255; } if (g > 255) { g = 255; } if (b > 255) { b = 255; } LEDSetup.processColor(i, serialData, r, g, b); }); #endif screenBitmap.Dispose(); LEDSetup.sendSerialData(serialData); }