public static dynamic AmbientSampling() { try { //Prevents crash when user is on the secure desktop int numberOfSamples = 6; Bitmap[] screenSample = new Bitmap[numberOfSamples]; System.Drawing.Color[] screenPixels = new System.Drawing.Color[numberOfSamples]; for (int i = 0; i < screenSample.Length; i++) { screenSample[i] = new Bitmap(1, 1); Graphics screen = Graphics.FromImage(screenSample[i]); int width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; int height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; screen.CopyFromScreen(128 + (((width - 128) / (screenSample.Length - 1)) * i), 128 + (((height - 128) / (screenSample.Length - 1)) * i), 0, 0, screenSample[i].Size); screenPixels[i] = screenSample[i].GetPixel(0, 0); } //HSB values for colours do not directly correspond to the way hue handles light. //For instance, pink would show up as a vibrant red, while actual red would look extremely dark. float brightnessAverage = (screenPixels.Average(x => x.GetBrightness()) + screenPixels.Average(x => x.GetSaturation()) / 3) * 255; if (brightnessAverage > 255) { brightnessAverage = 255; } float saturationAverage = 2 - 2 * screenPixels.Average(x => x.GetBrightness()); if (saturationAverage > 1) { saturationAverage = 1; } saturationAverage = screenPixels.Average(x => x.GetSaturation()) * saturationAverage * 255; //Desaturation saturationAverage *= 0.5F; //Saturation in darkness float contrastBoost = (127 - (brightnessAverage * 10)); if (contrastBoost < 0) { contrastBoost = 0; } saturationAverage += contrastBoost; if (saturationAverage > 255) { saturationAverage = 255; } //Hue is an angle, and can't be averaged directly. double hueSin = 0; double hueCos = 0; foreach (System.Drawing.Color pixel in screenPixels) { hueSin += Math.Sin(pixel.GetHue() * Math.PI / 180); hueCos += Math.Cos(pixel.GetHue() * Math.PI / 180); } double hueAverage = Math.Atan2(hueSin, hueCos) * (180 / Math.PI); if (hueAverage < 0) { hueAverage = 360 + hueAverage; } hueAverage *= 182; dynamic ambient = new ExpandoObject(); ambient.bri = (int)brightnessAverage; ambient.hue = (int)hueAverage; ambient.sat = (int)saturationAverage; return(ambient); } catch (Exception) { dynamic ambient = new ExpandoObject(); ambient.bri = 128; ambient.hue = 1; ambient.sat = 1; return(ambient); } }
public static dynamic AmbientSampling() { try { //Prevents crash when user is on the secure desktop int numberOfSamples = 6; Bitmap[] screenSample = new Bitmap[numberOfSamples]; System.Drawing.Color[] screenPixels = new System.Drawing.Color[numberOfSamples]; for (int i = 0; i < screenSample.Length; i++) { screenSample[i] = new Bitmap(1, 1); Graphics screen = Graphics.FromImage(screenSample[i]); int width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; int height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; screen.CopyFromScreen(128 + (((width - 128) / (screenSample.Length - 1)) * i), 128 + (((height - 128) / (screenSample.Length - 1)) * i), 0, 0, screenSample[i].Size); screenPixels[i] = screenSample[i].GetPixel(0, 0); } //HSB values for colours do not directly correspond to the way hue handles light. //For instance, pink would show up as a vibrant red, while actual red would look extremely dark. float brightnessAverage = (screenPixels.Average(x => x.GetBrightness()) + screenPixels.Average(x => x.GetSaturation()) / 3) * 255; if (brightnessAverage > 255) { brightnessAverage = 255; } float saturationAverage = 2 - 2 * screenPixels.Average(x => x.GetBrightness()); if (saturationAverage > 1) { saturationAverage = 1; } saturationAverage = screenPixels.Average(x => x.GetSaturation()) * saturationAverage * 255; if (Properties.Settings.Default.ambientSatMode == 0) { //Desaturation saturationAverage *= 0.5F; //Saturation in darkness float contrastBoost = (127 - (brightnessAverage * 10)); if (contrastBoost < 0) { contrastBoost = 0; } saturationAverage += contrastBoost; } if (saturationAverage > 255) { saturationAverage = 255; } //Hue is an angle, and can't be averaged directly. double hueSin = 0; double hueCos = 0; foreach (System.Drawing.Color pixel in screenPixels) { hueSin += Math.Sin(pixel.GetHue() * Math.PI / 180); hueCos += Math.Cos(pixel.GetHue() * Math.PI / 180); } double hueAverage = Math.Atan2(hueSin, hueCos) * (180 / Math.PI); if (hueAverage < 0) { hueAverage = 360 + hueAverage; } hueAverage *= 182; dynamic ambient = new ExpandoObject(); ambient.bri = (int)brightnessAverage; ambient.hue = (int)hueAverage; ambient.sat = (int)saturationAverage; return ambient; } catch (Exception) { dynamic ambient = new ExpandoObject(); ambient.bri = 128; ambient.hue = 1; ambient.sat = 1; return ambient; } }