Example #1
0
        public static Contrast GetContrast(GColor backGround, GColor frontColor)
        {
            GColor back = new FormColor(Color.FromArgb(backGround.A, backGround.R, backGround.G, backGround.B));
            GColor front = new FormColor(Color.FromArgb(frontColor.A, frontColor.R, frontColor.G, frontColor.B));
#endif

            if (back.A == 255)
            {
                if (front.A < 255)
                    front = OverlayOn(front, back);
                double l1 = Luminance(back) + 0.05;
                double l2 = Luminance(front) + 0.05;
                double ratio = l2 > l1 ? l2 / l1 : l1 / l2;

                ratio = Math.Round(ratio, 1);
                return new Contrast()
                    {
                        Ratio = ratio,
                        Error = 0,
                        Min = ratio,
                        Max = ratio,
                        Closet = null,
                        Farthest = null
                    };
            }

            double onBlack = GetContrast(OverlayOn(back, Black), front).Ratio;
            double onWhite = GetContrast(OverlayOn(back, White), front).Ratio;

            double max = Math.Max(onBlack, onWhite);
#if NETFX_CORE || SILVERLIGHT
            Color closest = Closest(backGround, frontColor);
#else
            GColor closest = Closest(backGround, frontColor);
#endif
            double min = GetContrast(OverlayOn(back, closest), front).Ratio;

            return new Contrast()
                {
                    Ratio = Math.Round((max + min) / 2, 2),
                    Error = Math.Round((max - min) / 2, 2),
                    Min = min,
                    Max = max,
                    Closet = closest,
                    Farthest = onWhite == max ? White : Black
                };
        }