optimize<Avatar>: use same colors for both light and dark theme

This commit is contained in:
leo 2021-04-07 15:34:00 +08:00
parent 794a6437fb
commit a71a877387

View file

@ -19,39 +19,14 @@ namespace SourceGit.Helpers {
public class Avatar : Image { public class Avatar : Image {
/// <summary> /// <summary>
/// Colors used in avatar for light theme /// Background colors for default
/// </summary> /// </summary>
public static Brush[] LightColors = new Brush[] { private static readonly Brush[] Backgrounds = new Brush[] {
Brushes.LightCoral, new LinearGradientBrush(Colors.Orange, Color.FromRgb(255, 213, 134), 90),
Brushes.LightGreen, new LinearGradientBrush(Colors.DodgerBlue, Colors.LightSkyBlue, 90),
Brushes.LightPink, new LinearGradientBrush(Colors.LimeGreen, Color.FromRgb(124, 241, 124), 90),
Brushes.LightSeaGreen, new LinearGradientBrush(Colors.Orchid, Color.FromRgb(248, 161, 245), 90),
Brushes.LightSteelBlue, new LinearGradientBrush(Colors.Tomato, Color.FromRgb(252, 165, 150), 90),
Brushes.Gray,
Brushes.SkyBlue,
Brushes.Plum,
Brushes.Gold,
Brushes.Khaki,
};
/// <summary>
/// Colors used in avatar for light theme
/// </summary>
public static Brush[] DarkColors = new Brush[] {
Brushes.DarkCyan,
Brushes.DarkGoldenrod,
Brushes.DarkGreen,
Brushes.DarkKhaki,
Brushes.DarkMagenta,
Brushes.DarkOliveGreen,
Brushes.DarkOrange,
Brushes.DarkOrchid,
Brushes.DarkSalmon,
Brushes.DarkSeaGreen,
Brushes.DarkSlateBlue,
Brushes.DarkSlateGray,
Brushes.DarkTurquoise,
Brushes.DarkViolet
}; };
/// <summary> /// <summary>
@ -108,27 +83,25 @@ namespace SourceGit.Helpers {
CultureInfo.CurrentCulture, CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, FlowDirection.LeftToRight,
new Typeface(new FontFamily("Consolas"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), new Typeface(new FontFamily("Consolas"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
Width * 0.75, Width * 0.65,
Brushes.White, Brushes.White,
VisualTreeHelper.GetDpi(this).PixelsPerDip); VisualTreeHelper.GetDpi(this).PixelsPerDip);
double offsetX = 0; double offsetX = 0;
if (HorizontalAlignment == HorizontalAlignment.Right) { if (HorizontalAlignment == HorizontalAlignment.Right) {
offsetX = -Width * 0.5; offsetX = -Width * 0.5;
} else if (HorizontalAlignment == HorizontalAlignment.Left) {
offsetX = Width * 0.5;
} }
var chars = placeholder.ToCharArray(); var chars = placeholder.ToCharArray();
var sum = 0; var sum = 0;
foreach (var ch in chars) sum += Math.Abs(ch); foreach (var ch in chars) sum += Math.Abs(ch);
Brush brush; var corner = Math.Max(2, Width / 16);
if (App.Setting.UI.UseLightTheme) {
brush = LightColors[sum % LightColors.Length];
} else {
brush = DarkColors[sum % DarkColors.Length];
}
dc.DrawRoundedRectangle(brush, null, new Rect(-Width * 0.5 + offsetX, -Height * 0.5, Width, Height), Width / 16, Height / 16); Brush brush = Backgrounds[sum % Backgrounds.Length];
dc.DrawRoundedRectangle(brush, null, new Rect(-Width * 0.5 + offsetX, -Height * 0.5, Width, Height), corner, corner);
dc.DrawText(formatted, new Point(formatted.Width * -0.5 + offsetX, formatted.Height * -0.5)); dc.DrawText(formatted, new Point(formatted.Width * -0.5 + offsetX, formatted.Height * -0.5));
} }
} }