当前位置: 首页>>代码示例>>C#>>正文


C# Color.Average方法代码示例

本文整理汇总了C#中System.Drawing.Color.Average方法的典型用法代码示例。如果您正苦于以下问题:C# Color.Average方法的具体用法?C# Color.Average怎么用?C# Color.Average使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Drawing.Color的用法示例。


在下文中一共展示了Color.Average方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AmbientSampling

        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;
            }
        }
开发者ID:Antexa,项目名称:Albedo,代码行数:71,代码来源:Platform.cs


注:本文中的System.Drawing.Color.Average方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。