[Feature] Add perceived luminance helper #4659
Replies: 9 comments
-
Hello, 'MartinZikmund! Thanks for submitting a new feature request. I've automatically added a vote 👍 reaction to help get things started. Other community members can vote to help us prioritize this feature in the future! |
Beta Was this translation helpful? Give feedback.
-
If this would be a welcome addition, I will be happy to implement it. |
Beta Was this translation helpful? Give feedback.
-
I had a similar thought in a couple of the WinUI issues. Some kind of ContrastBrush. You provide it two colours, one for a Light contrast, one for a Dark contrast. The brush then looks at the pixel colour below/behind it and then chooses which ever of the two colours offers the better contrast ratio. This would work well with the Fluent Theme Editor which allows for overriding of the Theme colours, by default could use Black and White colours, and may be useful for High Contrast theme usage, As a brush, it could be applied in XAML, and would not need code-behind for each use of it. |
Beta Was this translation helpful? Give feedback.
-
@mdtauk I like that idea as well, is there an issue for this? In my specific scenario the C#-based solution is ok, as I only need to decide if I want to set |
Beta Was this translation helpful? Give feedback.
-
@MartinZikmund Thanks for considering to work on this feature. I like the idea and it seems like @mdtauk have a similar idea that seems to be closely aligned with it too. Let's see what devs think about this 🤔 |
Beta Was this translation helpful? Give feedback.
-
Thanks @MartinZikmund, I've assigned to you to drive this work. Seems like there's an opportunity here to have a couple of different helpers/solutions for various scenarios. :) |
Beta Was this translation helpful? Give feedback.
-
@mdtauk how would the contrast brush work for non-uniform backgrounds? |
Beta Was this translation helpful? Give feedback.
-
That is a good question. I'm not sure of any good way for the Xaml rendering to "read the pixel colour" let alone grab a range of pixels and perform the equivalent of an Average blur filter to find the average pixel colour |
Beta Was this translation helpful? Give feedback.
-
@MartinZikmund wanted to revive this discussion. I think we basically need to brainstorm or figure out the best possible helpers, but at minimum a C# helper function to start gets things going still, right? FYI @Sergio0694 in case you have any thoughts in this area. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the problem this feature would solve
For a given color, it would be useful to know the contrasting color for displaying text (white/black). This can also be useful to determine the
RequestedTheme
for a control based on a given background color.Describe the solution
A new helper method would be added to
ColorHelpers
class, which would calculate perceived luminance for a given color using the following formula (based on W3C):The result would be a number between 0 and 1, for 0.5 being the boundary between black/white foreground color.
Describe alternatives you've considered
The method could also be added as an extension method on the
Windows.UI.Color
type.Beta Was this translation helpful? Give feedback.
All reactions