예제 #1
0
 /// <summary>
 /// 将颜色调暗一些
 /// </summary>
 /// <param name="color">待修正色</param>
 /// <returns>修正色</returns>
 private static HslColor ReviseDarker(HslColor color)
 {
     return(ReviseDarker(color, ReviseParameter));
 }
예제 #2
0
 /// <summary>
 /// 将颜色调亮特定亮度
 /// </summary>
 /// <param name="color">待修正色</param>
 /// <param name="brigher">调整的亮度</param>
 /// <returns>修正色</returns>
 private static HslColor ReviseBrighter(HslColor color, double brigher)
 {
     return(new HslColor(color.Alpha, color.Hue, color.Saturation, color.Lightness + brigher));
     //return Color.FromRgb(ReviseByteBigger(hsvColor.R), ReviseByteBigger(hsvColor.G), ReviseByteBigger(hsvColor.B));
 }
예제 #3
0
 /// <summary>
 /// 将颜色调暗特定亮度
 /// </summary>
 /// <param name="color">待修正色</param>
 /// <param name="darker">调整的亮度</param>
 /// <returns>修正色</returns>
 private static HslColor ReviseDarker(HslColor color, double darker)
 {
     return(new HslColor(color.Alpha, color.Hue, color.Saturation, color.Lightness - darker));
 }
예제 #4
0
 /// <summary>
 /// 将颜色调整到能够接受的最高亮度
 /// </summary>
 /// <param name="color">待修正色</param>
 /// <returns>修正色</returns>
 private static HslColor ReviseVeryBright(HslColor color)
 {
     return(ReviseBrighter(color, TooBright - color.Lightness));
 }
예제 #5
0
 /// <summary>
 /// 将颜色调整到能够接受的最低亮度
 /// </summary>
 /// <param name="color">待修正色</param>
 /// <returns>修正色</returns>
 private static HslColor ReviseVeryDark(HslColor color)
 {
     return(ReviseDarker(color, color.Lightness - TooDark));
 }
예제 #6
0
 /// <summary>
 /// 颜色是否太暗
 /// </summary>
 /// <param name="color">颜色</param>
 /// <returns>Boolean值</returns>
 private static bool IsTooDark(HslColor color)
 {
     return(color.Lightness < TooDark);
 }
예제 #7
0
 /// <summary>
 /// 颜色是否太亮
 /// </summary>
 /// <param name="color">颜色</param>
 /// <returns>Boolean值</returns>
 private static bool IsTooBright(HslColor color)
 {
     return(color.Lightness > TooBright);
 }
예제 #8
0
 /// <summary>
 /// 颜色饱和度是否接近0
 /// </summary>
 /// <param name="color">颜色</param>
 /// <returns>Boolean值</returns>
 private static bool IsAlmostZeroSaturation(HslColor color)
 {
     return(color.Saturation < AlmostZeroSaturation);
 }
예제 #9
0
 /// <summary>
 /// 颜色饱和度是否太低
 /// </summary>
 /// <param name="color">颜色</param>
 /// <returns>Boolean值</returns>
 private static bool IsNotSaturateEnough(HslColor color)
 {
     return(color.Saturation < NotSaturateEnough);
 }
예제 #10
0
 /// <summary>
 /// 计算两种颜色的差异。0为无差异,1为差异最大值
 /// </summary>
 /// <param name="c1">颜色1</param>
 /// <param name="c2">颜色2</param>
 /// <returns>差异值</returns>
 private static double Difference(HslColor c1, HslColor c2)
 {
     return(Difference(c1.ToRgb(), c2.ToRgb()));
 }
예제 #11
0
 /// <summary>
 /// 两种颜色差异是否足够大
 /// </summary>
 /// <param name="c1">颜色1</param>
 /// <param name="c2">颜色2</param>
 /// <returns>Boolean值</returns>
 private static bool IsTooMuchDiff(HslColor c1, HslColor c2)
 {
     return(Difference(c1, c2) > CoverColorDiff);
     //return Math.Abs((c1.R - c2.R) * (c1.R - c2.R) + (c1.G - c2.G) * (c1.G - c2.G) + (c1.B - c2.B) * (c1.B - c2.B)) > CoverColorDiff;
 }
예제 #12
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>
        private 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));
            }
        }