Пример #1
0
        /// <summary>
        /// 颜色修正
        /// </summary>
        /// <param name="color1">待修正色</param>
        /// <param name="color2">参照色</param>
        /// <returns>修正色</returns>
        public static HslColor Revise(HslColor color1, HslColor color2)
        {
            var newcolor = new HslColor(color1.ToRgb());

            while (IsTooBright(newcolor) || !IsTooMuchDiff(newcolor, color2) && !IsTooDark(newcolor) && newcolor.Lightness > 0)
            {
                newcolor = ReviseDarker(newcolor);
            }
            if (!IsTooDark(newcolor))
            {
                return(newcolor);
            }
            newcolor = ReviseBrighter(color1);
            while (IsTooDark(newcolor) || !IsTooMuchDiff(newcolor, color2) && !IsTooBright(newcolor) && newcolor.Lightness < 1)
            {
                newcolor = ReviseBrighter(newcolor);
            }
            if (!IsTooBright(newcolor))
            {
                return(newcolor);
            }
            if (IsTooBright(color1))
            {
                return(ReviseVeryBright(color1));
            }
            if (IsTooDark(color1))
            {
                return(ReviseVeryDark(color1));
            }
            return(color1);
        }
Пример #2
0
        /// <summary>
        /// 反色
        /// </summary>
        /// <param name="color">原色</param>
        /// <returns>反色</returns>
        public static HslColor Reverse(HslColor color)
        {
            Color rgb = color.ToRgb();

            return(new HslColor(Color.FromArgb(rgb.A, (byte)(255 - rgb.R), (byte)(255 - rgb.G), (byte)(255 - rgb.B))));
            //return new HSLColor(hsvColor.Alpha, hsvColor.Hue + 180, 1 - hsvColor.Saturation, 1 - hsvColor.Lightness);
        }
Пример #3
0
 /// <summary>
 /// 计算两种颜色的差异。0为无差异,1为差异最大值
 /// </summary>
 /// <param name="c1">颜色1</param>
 /// <param name="c2">颜色2</param>
 /// <returns>差异值</returns>
 public static double Difference(HslColor c1, HslColor c2)
 {
     return(Difference(c1.ToRgb(), c2.ToRgb()));
 }
Пример #4
0
        /// <summary>
        /// 从图片中获取指定区域的背景颜色
        /// </summary>
        /// <param name="pixels">The pixels.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <param name="top">The top.</param>
        /// <param name="bottom">The bottom.</param>
        /// <param name="forceDisableColorWeight">if set to <c>true</c> [force disable color weight].</param>
        /// <returns></returns>
        public static Color GetColorOfRegion(byte[] pixels, int width, int height, int left, int right, int top,
                                             int bottom, bool forceDisableColorWeight = false, bool removeFaceColor = true)
        {
            const int bytesPerPixel = 3;
            double    sr = 0, sg = 0, sb = 0;
            double    totalweight = 0;

            for (int i = top; i < bottom; i++)
            {
                for (int j = left; j < right; j++)
                {
                    byte   r = pixels[(i * width + j) * bytesPerPixel + 0];
                    byte   g = pixels[(i * width + j) * bytesPerPixel + 1];
                    byte   b = pixels[(i * width + j) * bytesPerPixel + 2];
                    double weight;
                    if (!forceDisableColorWeight && EnableColorWeight)
                    {
                        var color    = Color.FromRgb(r, g, b);
                        var hslColor = new HslColor(color);
                        weight = (1 - Math.Abs(1 - 2 * hslColor.Lightness)) * hslColor.Saturation;
                        if (weight < MinWeight)
                        {
                            weight = 0;
                        }
                        if (removeFaceColor)
                        {
                            var difference = Math.Abs(new HslColor(FaceColor).Hue - hslColor.Hue) / 360;
                            if (difference <= ZeroWeightFaceColorDifference)
                            {
                                weight = 0;
                            }
                            else
                            {
                                weight = weight * difference;
                            }
                        }
                    }
                    else
                    {
                        weight = 1;
                    }
                    totalweight += weight;
                    sr          += r * weight;
                    sg          += g * weight;
                    sb          += b * weight;
                }
            }

            if (totalweight <= 0)
            {
                if (removeFaceColor)
                {
                    //当去除人脸色彩后总权重为0时,禁用去除人脸色彩
                    return(GetColorOfRegion(pixels, width, height, left, right, top, bottom, false, false));
                }
                else
                {
                    //纯灰度图片不能使用权重
                    var newColor    = GetColorOfRegion(pixels, width, height, left, right, top, bottom, true);
                    var newHslColor = new HslColor(newColor);
                    newHslColor.Saturation = 0;
                    return(newHslColor.ToRgb());
                }
            }
            else
            {
                sr = sr / totalweight;
                sg = sg / totalweight;
                sb = sb / totalweight;
                return(Color.FromRgb((byte)sr, (byte)sg, (byte)sb));
            }
        }