Readable coloring for calendar events.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
xmlns:controls="using:Wino.Calendar.Controls"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:data="using:Wino.Calendar.ViewModels.Data"
|
||||
xmlns:helpers="using:Wino.Helpers"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:models="using:Wino.Core.Domain.Models.Calendar"
|
||||
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
|
||||
@@ -12,9 +13,12 @@
|
||||
|
||||
<!-- Default Calendar Item View Model Template -->
|
||||
<DataTemplate x:Key="CalendarItemViewModelItemTemplate" x:DataType="data:CalendarItemViewModel">
|
||||
<Grid Background="Red" CornerRadius="4">
|
||||
<Grid Background="{x:Bind helpers:XamlHelpers.GetSolidColorBrushFromHex(AssignedCalendar.BackgroundColorHex), Mode=OneWay}" CornerRadius="4">
|
||||
<TextBlock
|
||||
Margin="2,0"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
Foreground="{x:Bind helpers:XamlHelpers.GetReadableTextColor(AssignedCalendar.BackgroundColorHex), Mode=OneWay}"
|
||||
Text="{x:Bind CalendarItem.Title}"
|
||||
TextWrapping="Wrap" />
|
||||
</Grid>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Microsoft.Toolkit.Uwp.Helpers;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
@@ -42,6 +43,37 @@ namespace Wino.Helpers
|
||||
};
|
||||
}
|
||||
|
||||
public static SolidColorBrush GetReadableTextColor(string backgroundColor)
|
||||
{
|
||||
if (!backgroundColor.StartsWith("#")) throw new ArgumentException("Hex color must start with #.");
|
||||
|
||||
backgroundColor = backgroundColor.TrimStart('#');
|
||||
|
||||
if (backgroundColor.Length == 6)
|
||||
{
|
||||
var r = int.Parse(backgroundColor.Substring(0, 2), NumberStyles.HexNumber);
|
||||
var g = int.Parse(backgroundColor.Substring(2, 2), NumberStyles.HexNumber);
|
||||
var b = int.Parse(backgroundColor.Substring(4, 2), NumberStyles.HexNumber);
|
||||
|
||||
// Calculate relative luminance
|
||||
double luminance = (0.2126 * GetLinearValue(r)) +
|
||||
(0.7152 * GetLinearValue(g)) +
|
||||
(0.0722 * GetLinearValue(b));
|
||||
|
||||
return luminance > 0.5 ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Colors.White);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Hex color must be 6 characters long (e.g., #RRGGBB).");
|
||||
}
|
||||
}
|
||||
|
||||
private static double GetLinearValue(int colorComponent)
|
||||
{
|
||||
double sRGB = colorComponent / 255.0;
|
||||
return sRGB <= 0.03928 ? sRGB / 12.92 : Math.Pow((sRGB + 0.055) / 1.055, 2.4);
|
||||
}
|
||||
|
||||
public static Microsoft.UI.Xaml.Controls.NavigationViewDisplayMode NavigationViewDisplayModeConverter(SplitViewDisplayMode splitViewDisplayMode)
|
||||
{
|
||||
return splitViewDisplayMode switch
|
||||
|
||||
Reference in New Issue
Block a user