/// <summary> /// Sets one DeviceKeys key with a specific color on the bitmap /// </summary> /// <param name="key">DeviceKey to be set</param> /// <param name="color">Color to be used</param> private void SetOneKey(Devices.DeviceKeys key, Color color) { Bitmaping keymaping = Effects.GetBitmappingFromDeviceKey(key); if (keymaping.Equals(new Bitmaping(0, 0, 0, 0)) && key == Devices.DeviceKeys.Peripheral) { peripheral = color; } else { if (keymaping.topleft_y < 0 || keymaping.bottomright_y > Effects.canvas_height || keymaping.topleft_x < 0 || keymaping.bottomright_x > Effects.canvas_width) { Global.logger.LogLine("Coudln't set key color " + key.ToString(), Logging_Level.Warning); return; } else { using (Graphics g = Graphics.FromImage(colormap)) { Rectangle keyarea = new Rectangle(keymaping.topleft_x, keymaping.topleft_y, keymaping.bottomright_x - keymaping.topleft_x, keymaping.bottomright_y - keymaping.topleft_y); g.FillRectangle(new SolidBrush(color), keyarea); needsRender = true; } } } }
/// <summary> /// Retrieves a color of the specified DeviceKeys key from the bitmap /// </summary> /// <param name="key">Key</param> /// <returns>Color of the Key</returns> public Color Get(Devices.DeviceKeys key) { try { Bitmaping keymaping = Effects.GetBitmappingFromDeviceKey(key); if (keymaping.Equals(new Bitmaping(0, 0, 0, 0)) && key == Devices.DeviceKeys.Peripheral) { return(peripheral); } else { if (keymaping.bottomright_x - keymaping.topleft_x == 0 || keymaping.bottomright_y - keymaping.topleft_y == 0) { return(Color.FromArgb(0, 0, 0)); } if (needsRender) { RenderLayer(); } long Red = 0; long Green = 0; long Blue = 0; long Alpha = 0; BitmapData srcData = colormap.LockBits( new Rectangle(keymaping.topleft_x, keymaping.topleft_y, keymaping.bottomright_x - keymaping.topleft_x, keymaping.bottomright_y - keymaping.topleft_y), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int stride = srcData.Stride; IntPtr Scan0 = srcData.Scan0; int width = keymaping.bottomright_x - keymaping.topleft_x; int height = keymaping.bottomright_y - keymaping.topleft_y; unsafe { byte *p = (byte *)(void *)Scan0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Blue += p[(y * stride) + x * 4]; Green += p[(y * stride) + x * 4 + 1]; Red += p[(y * stride) + x * 4 + 2]; Alpha += p[(y * stride) + x * 4 + 3]; } } } int Colorscount = width * height; colormap.UnlockBits(srcData); return(Color.FromArgb((int)(Alpha / Colorscount), (int)(Red / Colorscount), (int)(Green / Colorscount), (int)(Blue / Colorscount))); } } catch (Exception exc) { Global.logger.LogLine("EffectLayer.Get() Exception: " + exc, Logging_Level.Error); return(Color.FromArgb(0, 0, 0)); } }