public static BasicColor IncreaseSaturation(BasicColor input, double factor) { if (factor < 0 || factor > 1) { throw new ArgumentException(); } if (input.IsGray()) { return(input); } byte inMin = input.GetMinRGB(), inMax = input.GetMaxRGB(), outMin, outMax; if (factor == 1) { outMin = 0; outMax = byte.MaxValue; } else { outMin = (byte)((double)inMin * (1 - factor)); outMax = (byte)((double)inMax * (1.0 - factor) + (double)byte.MaxValue * (factor)); } return(input.Copy().Map(inMin, inMax, outMin, outMax)); }
internal void UpdateColors(BasicColor screen, BasicColor output) { Canvas_AverageScreenColor.Background = new SolidColorBrush(Color.FromArgb(255, screen.R, screen.G, screen.B)); Canvas_AmbientLightColor.Background = new SolidColorBrush(Color.FromArgb(255, output.R, output.G, output.B)); Label_AverageScreenColorRGB.Content = screen.ToString(); Label_AmbientLightColorRGB.Content = output.ToString(); }
public static BasicColor SetMinBrightness(BasicColor input, double factor) { if (factor < 0 || factor > 1) { throw new ArgumentException(); } byte outMin = (byte)(byte.MaxValue * factor); return(input.Copy().Map(0, byte.MaxValue, outMin, byte.MaxValue)); }
private void updateColor(object obj) { while (true) { AmbientLightControl control = (AmbientLightControl)obj; BasicColor color = control.color; this.startTicks = DateTime.Now.Ticks; // measure time color.Set(ScreenColor.GetAverageScreenColor(ScreenColor.DeterminationMethod.Partly)); // read screen color double factor = saturation; transferFunctionFactor = TransferFunction(color.GetDelta()); if (preventFlickering) { factor *= transferFunctionFactor; } control.outputColor = ColorManipulation.IncreaseSaturation(color, factor).Multiply(brightness); try { SerialCommunication.SendColor(control.outputColor); // update hardware this.error = null; } catch (Exception e) { this.error = e; } this.executionTime = DateTime.Now.Ticks - this.startTicks; // stop time measurement this.updateUI(); // update ui switch (this.updateSpeed) { case UpdateSpeed.Low: Thread.Sleep(400); // keep Windows very responsive break; case UpdateSpeed.Normal: Thread.Sleep(100); break; case UpdateSpeed.Maximum: // continue as quick as possible break; default: Thread.Sleep(100); break; } } }
public static BasicColor GetAverageScreenColor(DeterminationMethod method) { Bitmap screenshot = GetScreenshot(); BasicColor avgColor = new BasicColor(); switch (method) { case DeterminationMethod.Interpolation: Bitmap bmp = new Bitmap(1, 1); using (Graphics g = Graphics.FromImage(bmp)) { // updated: the Interpolation mode needs to be set to // HighQualityBilinear or HighQualityBicubic or this method // doesn't work at all. With either setting, the results are // slightly different from the averaging method. g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(screenshot, new Rectangle(0, 0, 1, 1)); } Color avg = bmp.GetPixel(0, 0); bmp.Dispose(); avgColor = new BasicColor(avg.R, avg.G, avg.B); break; case DeterminationMethod.Partly: long sumR = 0, sumG = 0, sumB = 0, pxlCount = 0; int stepSize = 20; for (int x = 0; x < screenshot.Width; x += stepSize) { for (int y = 0; y < screenshot.Height; y += stepSize) { Color pixel = screenshot.GetPixel(x, y); sumR += pixel.R; sumG += pixel.G; sumB += pixel.B; pxlCount++; } } avgColor = new BasicColor((byte)(sumR / pxlCount), (byte)(sumG / pxlCount), (byte)(sumB / pxlCount)); break; } screenshot.Dispose(); return(avgColor); }
public static void SendColor(BasicColor color) { arduinoPort.Open(); arduinoPort.Write(color.GetByteArray(), 0, 3); arduinoPort.Close(); }
public void Set(BasicColor color) { this.R = color.R; this.G = color.G; this.B = color.B; }
public BasicColor(BasicColor copyFrom) { this.Set(copyFrom.R, copyFrom.G, copyFrom.B); }