mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
style: When resetting background color for high contrast, preserve alpha channel appropriately.
But discard it when backplating behind text, so that text is readable. This should be uncontroversial... Dealing with widgets is a bit harder so TBD. Differential Revision: https://phabricator.services.mozilla.com/D91779
This commit is contained in:
parent
1a5f48ba43
commit
379fb984f1
2 changed files with 40 additions and 15 deletions
|
@ -385,6 +385,8 @@ fn tweak_when_ignoring_colors(
|
|||
declaration: &mut Cow<PropertyDeclaration>,
|
||||
declarations_to_apply_unless_overriden: &mut DeclarationsToApplyUnlessOverriden,
|
||||
) {
|
||||
use crate::values::specified::Color;
|
||||
|
||||
if !longhand_id.ignored_when_document_colors_disabled() {
|
||||
return;
|
||||
}
|
||||
|
@ -401,21 +403,53 @@ fn tweak_when_ignoring_colors(
|
|||
return;
|
||||
}
|
||||
|
||||
fn alpha_channel(color: &Color) -> u8 {
|
||||
match *color {
|
||||
// Seems safe enough to assume that the default color and system
|
||||
// colors are opaque in HCM, though maybe we shouldn't asume the
|
||||
// later?
|
||||
#[cfg(feature = "gecko")]
|
||||
Color::InheritFromBodyQuirk | Color::System(..) => 255,
|
||||
// We don't have the actual color here, but since except for color:
|
||||
// transparent we force opaque text colors, it seems sane to do
|
||||
// this. You can technically fool this bit of code with:
|
||||
//
|
||||
// color: transparent; background-color: currentcolor;
|
||||
//
|
||||
// but this is best-effort, and that seems unlikely to happen in
|
||||
// practice.
|
||||
Color::CurrentColor => 255,
|
||||
// Complex colors are results of interpolation only and probably
|
||||
// shouldn't show up around here in HCM, but we've always treated
|
||||
// them as opaque effectively so keep doing it.
|
||||
Color::Complex { .. } => 255,
|
||||
Color::Numeric { ref parsed, .. } => parsed.alpha,
|
||||
}
|
||||
}
|
||||
|
||||
// A few special-cases ahead.
|
||||
match **declaration {
|
||||
// We honor color and background-color: transparent, and
|
||||
// "revert-or-initial" otherwise.
|
||||
PropertyDeclaration::BackgroundColor(ref color) => {
|
||||
if !color.is_transparent() {
|
||||
let color = builder.device.default_background_color();
|
||||
// For background-color, we revert or initial-with-preserved-alpha
|
||||
// otherwise, this is needed to preserve semi-transparent
|
||||
// backgrounds.
|
||||
//
|
||||
// FIXME(emilio, bug 1666059): We revert for alpha == 0, but maybe
|
||||
// should consider not doing that even if it causes some issues like
|
||||
// bug 1625036, or finding a performant way to preserve the original
|
||||
// widget background color's rgb channels but not alpha...
|
||||
let alpha = alpha_channel(color);
|
||||
if alpha != 0 {
|
||||
let mut color = builder.device.default_background_color();
|
||||
color.alpha = alpha;
|
||||
declarations_to_apply_unless_overriden.push(
|
||||
PropertyDeclaration::BackgroundColor(color.into())
|
||||
)
|
||||
}
|
||||
}
|
||||
PropertyDeclaration::Color(ref color) => {
|
||||
// otherwise.
|
||||
if color.0.is_transparent() {
|
||||
// We honor color: transparent, and "revert-or-initial" otherwise.
|
||||
if alpha_channel(&color.0) == 0 {
|
||||
return;
|
||||
}
|
||||
// If the inherited color would be transparent, but we would
|
||||
|
|
|
@ -529,15 +529,6 @@ impl Color {
|
|||
parse_hash_color(&serialization)
|
||||
.map_err(|()| location.new_custom_error(StyleParseErrorKind::UnspecifiedError))
|
||||
}
|
||||
|
||||
/// Returns true if the color is completely transparent, and false
|
||||
/// otherwise.
|
||||
pub fn is_transparent(&self) -> bool {
|
||||
match *self {
|
||||
Color::Numeric { ref parsed, .. } => parsed.alpha == 0,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "gecko")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue