BLOG-165 Adjust label text color based on background luminance for better contrast #298
Reference in New Issue
Block a user
Delete Branch "BLOG-165_support_dark_color_label_for_posts"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
This PR introduces support for dark background colors on post labels.
Specifically, the following changes have been made:
isDarkandcontrastingTextColorgetters inColorViewModel. The color contrast detection has been improved to use the standard sRGB relative luminance formula (per WCAG 2.x standard) with a threshold of0.179to determine if a background is dark, ensuring much more accurate contrast adjustments than a simple YIQ calculation.PostLabeldynamically to#ffffffif the background is dark, and inherits the default color if the background is light.PostLabelusing a lighter variant (lighten(0.4)) on dark labels, and a darker variant (darken(0.2)) on light labels to ensure visual clarity.Package Changes
No response
Screenshots
No response
Reference
Resolves #165
Checklist
BLOG-165 feat: adjust label text color based on background luminance for better contrastto BLOG-165 Adjust label text color based on background luminance for better contrast/improve
PR Code Suggestions ✨
Use WCAG formula for luminance
The YIQ formula is a decent approximation for perceived brightness, but for web
accessibility, the W3C recommends a more accurate formula for relative luminance.
Using the standard WCAG formula will ensure better contrast calculations across a
wider range of colors and align with modern accessibility guidelines.
frontend/src/lib/label/adapter/presenter/colorViewModel.ts [86-89]
Suggestion importance[1-10]: 8
__
Why: This is an excellent suggestion. The YIQ formula is a good approximation, but the WCAG formula for relative luminance is the standard for web accessibility. Using it ensures more accurate contrast calculations and better adherence to modern accessibility guidelines.
Use specific color for better contrast
Using
inheritfor the text color on light backgrounds relies on the parent elementhaving a dark text color. This assumption can fail in different contexts, such as a
dark-themed page, leading to unreadable text with low contrast. To ensure the
component is robust and accessible, it's better to return a specific dark color like
#000000.frontend/src/lib/label/adapter/presenter/colorViewModel.ts [91-93]
Suggestion importance[1-10]: 7
__
Why: The suggestion correctly points out that using
inheritcan lead to poor contrast if the parent element's color is not dark. Replacing it with a specific dark color like#000000makes the component more robust and improves accessibility.9a3dddaebb