protected RGB GetMostProminentRGB(int degrade, RGB rgbMatch) { weights.Clear(); int count = 0; foreach (Pixel pixel in pixels.Values) { int weight = pixel.weight * pixel.count; count++; if (pixel.DoesRgbMatch(rgbMatch)) { String key = (pixel.r >> degrade) + "," + (pixel.g >> degrade) + "," + (pixel.b >> degrade); if (weights.ContainsKey(key)) { weights[key] += weight; } else { weights.Add(key, weight); } } } RGB rgb = new RGB(); rgb.d = degrade; foreach (String key in weights.Keys) { if (count <= weights[key]) continue; String[] data = key.Split(','); rgb.count = count; rgb.r = int.Parse(data[0]); rgb.g = int.Parse(data[1]); rgb.b = int.Parse(data[2]); } return rgb; }
public bool DoesRgbMatch(RGB rgb) { if (null == rgb) return true; int r = this.r >> rgb.d; int g = this.g >> rgb.d; int b = this.b >> rgb.d; return rgb.r == r && rgb.g == g && rgb.b == b; }
private async Task ColorAsync(TimeSpan dueTime, TimeSpan interval, CancellationToken token) { if (interval.TotalMilliseconds == 0) return; Stopwatch ColorTH = new Stopwatch(); ColorTH.Start(); // Initial wait time before we begin the periodic loop. if (dueTime > TimeSpan.Zero) await Task.Delay(dueTime, token); DateTime LocalTimestamp = Timestamp; WSRColor color = new WSRColor(); // Repeat this loop until cancelled. while (!token.IsCancellationRequested) { // Skip already work with given data if (Timestamp == LocalTimestamp) { await Task.Delay(interval, token); continue; } // Timestamp data LocalTimestamp = Timestamp; ColorWatch.Again(); // Do Job try { CopyColorData = true; var rgb = color.GetMostProminentColor(ColorData); if (RGB == null || rgb.r > 50 && rgb.g > 50 && rgb.b > 50) { RGB = rgb; } if (WSRConfig.GetInstance().ColorTH.Milliseconds > 0 && ColorTH.Elapsed > WSRConfig.GetInstance().ColorTH) { WSRHttpManager.GetInstance().SendRequest("http://127.0.01:8080/sarah/hue?r=" + RGB.r + "&g=" + RGB.g + "&b=" + RGB.b); ColorTH.Restart(); } } catch (Exception ex) { WSRConfig.GetInstance().logError("COLOR", ex); } ColorWatch.Stop(); // Wait to repeat again. if (interval > TimeSpan.Zero) await Task.Delay(interval, token); } }