-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f530180
commit 76c0472
Showing
23 changed files
with
542 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Globalization; | ||
using Avalonia.Data.Converters; | ||
using Avalonia.Media; | ||
|
||
namespace ChatAAC.Converters | ||
{ | ||
/// <summary> | ||
/// Returns a brush with a contrasting color (white or black) | ||
/// compared to the given background color. | ||
/// </summary> | ||
public class ContrastForegroundBrushConverter : IValueConverter | ||
{ | ||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
if (value is not SolidColorBrush backgroundBrush) return Brushes.Black; // fallback | ||
var color = backgroundBrush.Color; | ||
|
||
// Compute luminance (0..1) | ||
var luminance = ComputeRelativeLuminance(color); | ||
|
||
// If the background is dark, return White, else Black | ||
return luminance < 0.5 ? Brushes.White : Brushes.Black; | ||
} | ||
|
||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
// Usually one-way binding, so no need to convert back | ||
return null!; | ||
} | ||
|
||
private static double ComputeRelativeLuminance(Color color) | ||
{ | ||
// Standard formula for relative luminance (from W3C): | ||
// https://www.w3.org/TR/WCAG20/#relativeluminancedef | ||
// R, G, B are in [0..1]. | ||
var r = color.R / 255.0; | ||
var g = color.G / 255.0; | ||
var b = color.B / 255.0; | ||
|
||
// Apply gamma correction | ||
r = (r <= 0.03928) ? (r / 12.92) : Math.Pow((r + 0.055) / 1.055, 2.4); | ||
g = (g <= 0.03928) ? (g / 12.92) : Math.Pow((g + 0.055) / 1.055, 2.4); | ||
b = (b <= 0.03928) ? (b / 12.92) : Math.Pow((b + 0.055) / 1.055, 2.4); | ||
|
||
// 0.2126 R + 0.7152 G + 0.0722 B | ||
return 0.2126 * r + 0.7152 * g + 0.0722 * b; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Globalization; | ||
using Avalonia.Data.Converters; | ||
|
||
namespace ChatAAC.Converters; | ||
|
||
public class NotNullToBoolConverter : IValueConverter | ||
{ | ||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
return value != null; // If it’s not null, return true | ||
} | ||
|
||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
return null; // Typically not used | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.