public static GradientBrush Convert(System.Windows.Media.Color color)
        {
            var scrgb = color.ToScRGBColor();

            var xyz = KnownColorSpaces.scRGB.ToXYZColor(scrgb);

            var lab = KnownColorSpaces.Lab.FromXYZColor(xyz) as LabColor;

            var l_base = lab.L;

            var gradientStops = new GradientStopCollection();

            var _lab = new LabColor(0xff, l_base * 1.07, lab.a, lab.b);
            var _c = _lab.ToWindowsMediaColor();

            gradientStops.Add(new GradientStop(_c, 0.5));


            _lab = new LabColor(0xff, l_base * .93, lab.a, lab.b);
            _c = _lab.ToWindowsMediaColor();

            gradientStops.Add(new GradientStop(_c, 1));


            var result = new LinearGradientBrush(gradientStops, 90);

            result.Freeze();

            return result;
        }
        /// <summary>
        /// Returns new color based on existing color with modified lightness component.
        /// </summary>
        /// <param name="color"></param>
        /// <param name="lightnessMultiplier">0.5 to reduce lightness by 50%, 1.5 to increase lightness by 50%</param>
        /// <returns></returns>
        public static System.Windows.Media.Color ChangeLighthness(this System.Windows.Media.Color color, double lightnessMultiplier)
        {
            var scRGB = color.ToScRGBColor();
            var xyz = KnownColorSpaces.scRGB.ToXYZColor(scRGB);
            var lab = (LabColor) KnownColorSpaces.Lab.FromXYZColor(xyz);

            var newColor = new LabColor(lab.Alpha, lab.L * lightnessMultiplier, lab.a, lab.b);

            return newColor.ToWindowsMediaColor();
        }
Пример #3
0
        static void RefreshForegruond(TextBlock tb)
        {
            var selectionHighlightBrush = GetSelectionHighlightBrush(tb) as SolidColorBrush;
            var hoverHighlightBrush = GetHoverHighlightBrush(tb) as SolidColorBrush;

            if (selectionHighlightBrush != null || hoverHighlightBrush != null)
            {
                var foreground_originalValue = GetForegroundOriginalValue(tb);
                var foreground_modifiedValue = GetForegroundModifiedValue(tb);

                var highlight_brush = selectionHighlightBrush;

                if (highlight_brush == null)
                    highlight_brush = hoverHighlightBrush;

                if (highlight_brush == null)
                {
                    // todo: restore original foreground

                    SetSelectionHighlightBrush(tb, null);
                    SetHoverHighlightBrush(tb, null);
                    SetForegroundModifiedValue(tb, null);
                    SetForegroundOriginalValue(tb, null);

                    return;
                }

                var highlight_color = highlight_brush.Color;

                var foreground_brush = tb.Foreground as SolidColorBrush;

                if (foreground_originalValue != null)
                {
                    foreground_brush = foreground_originalValue as SolidColorBrush;
                }

                if (foreground_brush == null)
                {
                    // todo: cleanup
                    // todo: restore original foreground
                    return;
                }

                var foreground_color = foreground_brush.Color;

                //foreground_color.ToContrastingLightnessColor();

                var highlightScRGB = highlight_color.ToScRGBColor();
                var foregroundScRGB = foreground_color.ToScRGBColor();

                var highlightXYZ = KnownColorSpaces.scRGB.ToXYZColor(highlightScRGB);
                var foregroundXYZ = KnownColorSpaces.scRGB.ToXYZColor(foregroundScRGB);

                var highlightLab = (LabColor)KnownColorSpaces.Lab.FromXYZColor(highlightXYZ);
                var foregroundLab = (LabColor)KnownColorSpaces.Lab.FromXYZColor(foregroundXYZ);

                var newForegroundLab = (LabColor)null;

                if (highlightLab.L > 50 && foregroundLab.L > 50)
                {
                    newForegroundLab = new LabColor(foregroundLab.Alpha, 5, foregroundLab.a, foregroundLab.b);
                }
                else if (highlightLab.L < 50 && foregroundLab.L < 50)
                {
                    newForegroundLab = new LabColor(foregroundLab.Alpha, 95, foregroundLab.a, foregroundLab.b);
                }

                if (newForegroundLab == null)
                    return;

                if (foreground_originalValue == null)
                {
                    var binding = BindingOperations.GetBinding(tb, TextBlock.ForegroundProperty);

                    if (binding != null)
                        SetForegroundOriginalValue(tb, binding);
                    else
                        SetForegroundOriginalValue(tb, tb.Foreground);
                }

                var fgBrush = new SolidColorBrush(newForegroundLab.ToWindowsMediaColor());
                fgBrush.Freeze();

                SetForegroundModifiedValue(tb, fgBrush);

                tb.Foreground = fgBrush;
            }
            else
            {
                var originalValue = GetForegroundOriginalValue(tb);

                if (originalValue == null)
                    return;

                if (originalValue is Binding)
                {
                    BindingOperations.SetBinding(tb, TextBlock.ForegroundProperty, originalValue as Binding);
                }
                else if (originalValue != null && object.Equals(GetForegroundModifiedValue(tb), tb.Foreground))
                {
                    tb.SetValue(TextBlock.ForegroundProperty, originalValue);
                }

                SetForegroundModifiedValue(tb, null);
                SetForegroundOriginalValue(tb, null);
            }

        }