コード例 #1
0
ファイル: Palette.cs プロジェクト: WClipboard/WClipboard
        private static string?GetResolvedTextColor(FrameworkElement frameworkElement, string?background)
        {
            if (background is null ||
                !TryGetColor(frameworkElement, LIGHT_TEXT_KEY, out var lightColor) ||
                !TryGetColor(frameworkElement, DARK_TEXT_KEY, out var darkColor) ||
                !TryGetColor(frameworkElement, background, out var backgroundColor) ||
                backgroundColor.A != 255)
            {
                return(null);
            }

            var lightContrast = ColorExtensions.ContrastWith(lightColor, backgroundColor);
            var darkContrast  = ColorExtensions.ContrastWith(darkColor, backgroundColor);

            return(lightContrast > darkContrast ? LIGHT_TEXT_KEY : DARK_TEXT_KEY);
        }
コード例 #2
0
        public override object Convert(object value, Type targetType, object?parameter, CultureInfo culture)
        {
            var color = value as Color? ??
                        (value as SolidColorBrush)?.Color ??
                        throw new ArgumentException($"{nameof(value)} must be of type {nameof(Color)} or {nameof(SolidColorBrush)}", nameof(value));

            if (color.A != 255)
            {
                throw new NotSupportedException($"{nameof(value)} must be nontransparant to use in a {nameof(ContrastTextColorConverter)}");
            }

            var lightColor = Application.Current.FindResource <Color>(LIGHT_TEXT_KEY);
            var darkColor  = Application.Current.FindResource <Color>(DARK_TEXT_KEY);

            var lightContrast = ColorExtensions.ContrastWith(lightColor, color);
            var darkContrast  = ColorExtensions.ContrastWith(darkColor, color);

            var contrastColor = lightContrast > darkContrast ? lightColor : darkColor;

            if (targetType.IsAssignableFrom(typeof(SolidColorBrush)))
            {
                return(new SolidColorBrush(contrastColor));
            }
            else if (targetType.IsAssignableFrom(typeof(Color)))
            {
                return(contrastColor);
            }
            else if (value is SolidColorBrush)
            {
                return(new SolidColorBrush(contrastColor));
            }
            else
            {
                return(contrastColor);
            }
        }