internal void AddPlayerColor(String player, Color color) { ColorHls hlsValue = HLSColor.RgbToHls(color); this.PlayerBrushes[player] = new List <Brush>(); this.PlayerBrushes[player].Add(new SolidColorBrush(HLSColor.HlsToRgb(hlsValue.H, Math.Min(1d, hlsValue.L * 1.1), hlsValue.S, hlsValue.A))); this.PlayerBrushes[player].Add(new SolidColorBrush(HLSColor.HlsToRgb(hlsValue.H, Math.Min(1d, hlsValue.L * 1.125), hlsValue.S * 0.95, hlsValue.A))); this.PlayerBrushes[player].Add(new SolidColorBrush(HLSColor.HlsToRgb(hlsValue.H, hlsValue.L * 0.25, hlsValue.S, hlsValue.A))); this.PlayerBrushes[player].ForEach((b) => b.Freeze()); }
static ColorHls RgbToHls(System.Windows.Media.Color rgbColor) { // Initialize result var hlsColor = new ColorHls(); // Convert RGB values to percentages double r = (double)rgbColor.R / 255; var g = (double)rgbColor.G / 255; var b = (double)rgbColor.B / 255; var a = (double)rgbColor.A / 255; // Find min and max RGB values var min = Math.Min(r, Math.Min(g, b)); var max = Math.Max(r, Math.Max(g, b)); var delta = max - min; /* If max and min are equal, that means we are dealing with * a shade of gray. So we set H and S to zero, and L to either * max or min (it doesn't matter which), and then we exit. */ //Special case: Gray if (max == min) { hlsColor.H = 0; hlsColor.S = 0; hlsColor.L = max; return(hlsColor); } /* If we get to this point, we know we don't have a shade of gray. */ // Set L hlsColor.L = (min + max) / 2; // Set S if (hlsColor.L < 0.5) { hlsColor.S = delta / (max + min); } else { hlsColor.S = delta / (2.0 - max - min); } // Set H if (r == max) { hlsColor.H = (g - b) / delta; } if (g == max) { hlsColor.H = 2.0 + (b - r) / delta; } if (b == max) { hlsColor.H = 4.0 + (r - g) / delta; } hlsColor.H *= 60; if (hlsColor.H < 0) { hlsColor.H += 360; } // Set A hlsColor.A = a; // Set return value return(hlsColor); }