style: Unbox a bunch of color properties.

This builds on https://github.com/servo/rust-cssparser/pull/118.
This commit is contained in:
Emilio Cobos Álvarez 2017-02-12 15:17:45 +01:00
parent e394334739
commit 0c102e2350
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
37 changed files with 185 additions and 195 deletions

View file

@ -12,7 +12,7 @@ path = "lib.rs"
[dependencies]
azure = {git = "https://github.com/servo/rust-azure", features = ["plugins"]}
canvas_traits = {path = "../canvas_traits"}
cssparser = {version = "0.7", features = ["heap_size", "serde-serialization"]}
cssparser = {version = "0.8", features = ["heap_size", "serde-serialization"]}
euclid = "0.10.1"
gleam = "0.2.8"
ipc-channel = "0.5"

View file

@ -1016,9 +1016,9 @@ impl ToAzureStyle for RGBA {
type Target = Color;
fn to_azure_style(self) -> Color {
Color::rgba(self.red as AzFloat,
self.green as AzFloat,
self.blue as AzFloat,
self.alpha as AzFloat)
Color::rgba(self.red_f32() as AzFloat,
self.green_f32() as AzFloat,
self.blue_f32() as AzFloat,
self.alpha_f32() as AzFloat)
}
}

View file

@ -10,7 +10,7 @@ name = "canvas_traits"
path = "lib.rs"
[dependencies]
cssparser = {version = "0.7", features = ["heap_size", "serde-serialization"]}
cssparser = {version = "0.8", features = ["heap_size", "serde-serialization"]}
euclid = "0.10.1"
heapsize = "0.3.0"
heapsize_derive = "0.1"

View file

@ -14,7 +14,7 @@ app_units = "0.3"
atomic_refcell = "0.1"
bitflags = "0.7"
canvas_traits = {path = "../canvas_traits"}
cssparser = {version = "0.7", features = ["heap_size", "serde-serialization"]}
cssparser = {version = "0.8", features = ["heap_size", "serde-serialization"]}
euclid = "0.10.1"
fnv = "1.0"
gfx = {path = "../gfx"}

View file

@ -2188,7 +2188,7 @@ pub trait ToGfxColor {
impl ToGfxColor for RGBA {
fn to_gfx_color(&self) -> ColorF {
ColorF::new(self.red, self.green, self.blue, self.alpha)
ColorF::new(self.red_f32(), self.green_f32(), self.blue_f32(), self.alpha_f32())
}
}

View file

@ -607,12 +607,7 @@ impl CollapsedBorder {
CollapsedBorder {
style: border_top_style::T::none,
width: Au(0),
color: Color::RGBA(RGBA {
red: 0.0,
green: 0.0,
blue: 0.0,
alpha: 0.0,
}),
color: Color::RGBA(RGBA::transparent()),
provenance: CollapsedBorderProvenance::FromTable,
}
}

View file

@ -11,7 +11,7 @@ path = "lib.rs"
[dependencies]
bitflags = "0.7"
cssparser = {version = "0.7", features = ["heap_size", "serde-serialization"]}
cssparser = {version = "0.8", features = ["heap_size", "serde-serialization"]}
heapsize = "0.3.0"
heapsize_derive = "0.1"
plugins = {path = "../plugins"}

View file

@ -34,7 +34,7 @@ byteorder = "1.0"
canvas_traits = {path = "../canvas_traits"}
caseless = "0.1.0"
cookie = {version = "0.2.5", features = ["serialize-rustc"]}
cssparser = {version = "0.7", features = ["heap_size", "serde-serialization"]}
cssparser = {version = "0.8", features = ["heap_size", "serde-serialization"]}
devtools_traits = {path = "../devtools_traits"}
encoding = "0.2"
euclid = "0.10.1"

View file

@ -57,7 +57,7 @@ impl CanvasGradientMethods for CanvasGradient {
let color = if parser.is_exhausted() {
match color {
Ok(CSSColor::RGBA(rgba)) => rgba,
Ok(CSSColor::CurrentColor) => RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0 },
Ok(CSSColor::CurrentColor) => RGBA::new(0, 0, 0, 255),
_ => return Err(Error::Syntax)
}
} else {

View file

@ -90,12 +90,7 @@ struct CanvasContextState {
impl CanvasContextState {
fn new() -> CanvasContextState {
let black = RGBA {
red: 0.0,
green: 0.0,
blue: 0.0,
alpha: 1.0,
};
let black = RGBA::new(0, 0, 0, 255);
CanvasContextState {
global_alpha: 1.0,
global_composition: CompositionOrBlending::default(),
@ -110,7 +105,7 @@ impl CanvasContextState {
shadow_offset_x: 0.0,
shadow_offset_y: 0.0,
shadow_blur: 0.0,
shadow_color: RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 }, // transparent black
shadow_color: RGBA::transparent(),
}
}
}
@ -490,12 +485,7 @@ impl CanvasRenderingContext2D {
style.GetPropertyValue(DOMString::from("display")) == "none";
if element_not_rendered {
Ok(RGBA {
red: 0.0,
green: 0.0,
blue: 0.0,
alpha: 1.0,
})
Ok(RGBA::new(0, 0, 0, 255))
} else {
self.parse_color(&style.GetPropertyValue(DOMString::from("color")))
}
@ -1349,11 +1339,11 @@ fn is_rect_valid(rect: Rect<f64>) -> bool {
fn serialize<W>(color: &RGBA, dest: &mut W) -> fmt::Result
where W: fmt::Write
{
let red = (color.red * 255.).round() as u8;
let green = (color.green * 255.).round() as u8;
let blue = (color.blue * 255.).round() as u8;
let red = color.red;
let green = color.green;
let blue = color.blue;
if color.alpha == 1f32 {
if color.alpha == 255 {
write!(dest,
"#{:x}{:x}{:x}{:x}{:x}{:x}",
red >> 4,
@ -1363,6 +1353,6 @@ fn serialize<W>(color: &RGBA, dest: &mut W) -> fmt::Result
blue >> 4,
blue & 0xF)
} else {
write!(dest, "rgba({}, {}, {}, {})", red, green, blue, color.alpha)
write!(dest, "rgba({}, {}, {}, {})", red, green, blue, color.alpha_f32())
}
}

View file

@ -407,7 +407,7 @@ impl LayoutElementHelpers for LayoutJS<Element> {
if let Some(color) = bgcolor {
hints.push(from_declaration(
PropertyDeclaration::BackgroundColor(DeclaredValue::Value(
Box::new(CSSColor { parsed: Color::RGBA(color), authored: None })))));
CSSColor { parsed: Color::RGBA(color), authored: None }))));
}
let background = if let Some(this) = self.downcast::<HTMLBodyElement>() {
@ -440,10 +440,10 @@ impl LayoutElementHelpers for LayoutJS<Element> {
if let Some(color) = color {
hints.push(from_declaration(
PropertyDeclaration::Color(DeclaredValue::Value(Box::new(CSSRGBA {
PropertyDeclaration::Color(DeclaredValue::Value(CSSRGBA {
parsed: color,
authored: None,
})))));
}))));
}
let font_family = if let Some(this) = self.downcast::<HTMLFontElement>() {

View file

@ -13,7 +13,7 @@ path = "lib.rs"
app_units = "0.3"
atomic_refcell = "0.1"
canvas_traits = {path = "../canvas_traits"}
cssparser = {version = "0.7", features = ["heap_size", "serde-serialization"]}
cssparser = {version = "0.8", features = ["heap_size", "serde-serialization"]}
euclid = "0.10.1"
gfx_traits = {path = "../gfx_traits"}
heapsize = "0.3.0"

View file

@ -18,5 +18,5 @@ path = "lib.rs"
[dependencies]
bitflags = "0.7"
matches = "0.1"
cssparser = ">=0.6, <0.8"
cssparser = ">=0.6, <0.9"
fnv = "1.0"

View file

@ -26,7 +26,7 @@ app_units = "0.3"
atomic_refcell = "0.1"
bitflags = "0.7"
cfg-if = "0.1.0"
cssparser = "0.7.3"
cssparser = "0.8"
encoding = "0.2"
euclid = "0.10.1"
fnv = "1.0"

View file

@ -394,12 +394,7 @@ pub fn parse_legacy_color(mut input: &str) -> Result<RGBA, ()> {
hex(input.as_bytes()[1] as char),
hex(input.as_bytes()[2] as char),
hex(input.as_bytes()[3] as char)) {
return Ok(RGBA {
red: (r as f32) * 17.0 / 255.0,
green: (g as f32) * 17.0 / 255.0,
blue: (b as f32) * 17.0 / 255.0,
alpha: 1.0,
})
return Ok(RGBA::new(r * 17, g * 17, b * 17, 255))
}
}
@ -466,12 +461,10 @@ pub fn parse_legacy_color(mut input: &str) -> Result<RGBA, ()> {
}
// Steps 15-20.
return Ok(RGBA {
red: hex_string(red).unwrap() as f32 / 255.0,
green: hex_string(green).unwrap() as f32 / 255.0,
blue: hex_string(blue).unwrap() as f32 / 255.0,
alpha: 1.0,
});
return Ok(RGBA::new(hex_string(red).unwrap(),
hex_string(green).unwrap(),
hex_string(blue).unwrap(),
255));
fn hex(ch: char) -> Result<u8, ()> {
match ch {

View file

@ -225,20 +225,18 @@ impl GeckoStyleCoordConvertible for None_ {
/// Convert a given RGBA value to `nscolor`.
pub fn convert_rgba_to_nscolor(rgba: &RGBA) -> u32 {
(((rgba.alpha * 255.0).round() as u32) << 24) |
(((rgba.blue * 255.0).round() as u32) << 16) |
(((rgba.green * 255.0).round() as u32) << 8) |
((rgba.red * 255.0).round() as u32)
((rgba.alpha as u32) << 24) |
((rgba.blue as u32) << 16) |
((rgba.green as u32) << 8) |
(rgba.red as u32)
}
/// Convert a given `nscolor` to a Servo RGBA value.
pub fn convert_nscolor_to_rgba(color: u32) -> RGBA {
RGBA {
red: ((color & 0xff) as f32) / 255.0,
green: (((color >> 8) & 0xff) as f32) / 255.0,
blue: (((color >> 16) & 0xff) as f32) / 255.0,
alpha: (((color >> 24) & 0xff) as f32) / 255.0,
}
RGBA::new((color & 0xff) as u8,
(color >> 8 & 0xff) as u8,
(color >> 16 & 0xff) as u8,
(color >> 24 & 0xff) as u8)
}
/// Round `width` down to the nearest device pixel, but any non-zero value that

View file

@ -510,18 +510,21 @@ impl Interpolate for RGBA {
val.max(0.).min(1.)
}
let alpha = clamp(try!(self.alpha.interpolate(&other.alpha, progress)));
let alpha = clamp(try!(self.alpha_f32().interpolate(&other.alpha_f32(), progress)));
if alpha == 0. {
Ok(RGBA { red: 0., green: 0., blue: 0., alpha: 0. })
Ok(RGBA::transparent())
} else {
Ok(RGBA { red: clamp(try!((self.red * self.alpha).interpolate(&(other.red * other.alpha), progress))
* 1. / alpha),
green: clamp(try!((self.green * self.alpha).interpolate(&(other.green * other.alpha), progress))
* 1. / alpha),
blue: clamp(try!((self.blue * self.alpha).interpolate(&(other.blue * other.alpha), progress))
* 1. / alpha),
alpha: alpha
})
// NB: We rely on RGBA::from_floats clamping already.
let red = try!((self.red_f32() * self.alpha_f32())
.interpolate(&(other.red_f32() * other.alpha_f32()), progress))
* 1. / alpha;
let green = try!((self.green_f32() * self.alpha_f32())
.interpolate(&(other.green_f32() * other.alpha_f32()), progress))
* 1. / alpha;
let blue = try!((self.blue_f32() * self.alpha_f32())
.interpolate(&(other.blue_f32() * other.alpha_f32()), progress))
* 1. / alpha;
Ok(RGBA::from_floats(red, green, blue, alpha))
}
}
}
@ -756,9 +759,7 @@ impl Interpolate for TextShadowList {
offset_x: Au(0),
offset_y: Au(0),
blur_radius: Au(0),
color: CSSParserColor::RGBA(RGBA {
red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0
})
color: CSSParserColor::RGBA(RGBA::transparent()),
};
let max_len = cmp::max(self.0.len(), other.0.len());
@ -793,9 +794,7 @@ impl Interpolate for BoxShadowList {
offset_y: Au(0),
spread_radius: Au(0),
blur_radius: Au(0),
color: CSSParserColor::RGBA(RGBA {
red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0
}),
color: CSSParserColor::RGBA(RGBA::transparent()),
inset: false,
};

View file

@ -7,9 +7,9 @@
<% data.new_style_struct("Background", inherited=False) %>
${helpers.predefined_type("background-color", "CSSColor",
"::cssparser::Color::RGBA(::cssparser::RGBA { red: 0., green: 0., blue: 0., alpha: 0. }) /* transparent */",
"::cssparser::Color::RGBA(::cssparser::RGBA::transparent())",
spec="https://drafts.csswg.org/css-backgrounds/#background-color",
animatable=True, complex_color=True, boxed=True)}
animatable=True, complex_color=True)}
<%helpers:vector_longhand name="background-image" animatable="False"
spec="https://drafts.csswg.org/css-backgrounds/#the-background-image"

View file

@ -20,7 +20,7 @@
"::cssparser::Color::CurrentColor",
alias=maybe_moz_logical_alias(product, side, "-moz-border-%s-color"),
spec=maybe_logical_spec(side, "color"),
animatable=True, logical = side[1], boxed=True)}
animatable=True, logical = side[1])}
% endfor
% for side in ALL_SIDES:

View file

@ -6,7 +6,7 @@
<% data.new_style_struct("Color", inherited=True) %>
<%helpers:raw_longhand name="color" need_clone="True" animatable="True" boxed="True"
<%helpers:raw_longhand name="color" need_clone="True" animatable="True"
spec="https://drafts.csswg.org/css-color/#color">
use cssparser::Color as CSSParserColor;
use cssparser::RGBA;
@ -36,18 +36,18 @@
}
#[inline]
pub fn get_initial_value() -> computed_value::T {
RGBA { red: 0., green: 0., blue: 0., alpha: 1. } /* black */
RGBA::new(0, 0, 0, 255) // black
}
pub fn parse_specified(context: &ParserContext, input: &mut Parser)
-> Result<DeclaredValue<Box<SpecifiedValue>>, ()> {
-> Result<DeclaredValue<SpecifiedValue>, ()> {
let value = try!(CSSColor::parse(context, input));
let rgba = match value.parsed {
CSSParserColor::RGBA(rgba) => rgba,
CSSParserColor::CurrentColor => return Ok(DeclaredValue::Inherit)
};
Ok(DeclaredValue::Value(Box::new(CSSRGBA {
Ok(DeclaredValue::Value(CSSRGBA {
parsed: rgba,
authored: value.authored,
})))
}))
}
</%helpers:raw_longhand>

View file

@ -147,7 +147,7 @@ ${helpers.single_keyword("column-fill", "auto balance", extra_prefixes="moz",
${helpers.predefined_type("column-rule-color", "CSSColor",
"::cssparser::Color::CurrentColor",
products="gecko", animatable=True, extra_prefixes="moz",
complex_color=True, need_clone=True, boxed=True,
complex_color=True, need_clone=True,
spec="https://drafts.csswg.org/css-multicol/#propdef-column-rule-color")}
// It's not implemented in servo or gecko yet.

View file

@ -429,7 +429,7 @@ ${helpers.single_keyword("text-align-last",
<%helpers:longhand name="-servo-text-decorations-in-effect"
derived_from="display text-decoration"
need_clone="True" products="servo"
animatable="False" boxed="True"
animatable="False"
spec="Nonstandard (Internal property used by Servo)">
use cssparser::RGBA;
use std::fmt;
@ -1025,9 +1025,8 @@ ${helpers.single_keyword("text-align-last",
${helpers.predefined_type("text-emphasis-color", "CSSColor",
"::cssparser::Color::CurrentColor",
products="gecko",animatable=True,
products="gecko", animatable=True,
complex_color=True, need_clone=True,
boxed=True,
spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-color")}
@ -1045,14 +1044,14 @@ ${helpers.predefined_type(
"-webkit-text-fill-color", "CSSColor",
"CSSParserColor::CurrentColor",
products="gecko", animatable=True,
complex_color=True, need_clone=True, boxed=True,
complex_color=True, need_clone=True,
spec="https://compat.spec.whatwg.org/#the-webkit-text-fill-color")}
${helpers.predefined_type(
"-webkit-text-stroke-color", "CSSColor",
"CSSParserColor::CurrentColor",
products="gecko", animatable=True,
complex_color=True, need_clone=True, boxed=True,
complex_color=True, need_clone=True,
spec="https://compat.spec.whatwg.org/#the-webkit-text-stroke-color")}
<%helpers:longhand products="gecko" name="-webkit-text-stroke-width" animatable="False"

View file

@ -11,7 +11,7 @@
// TODO(pcwalton): `invert`
${helpers.predefined_type("outline-color", "CSSColor", "::cssparser::Color::CurrentColor",
animatable=True, complex_color=True, need_clone=True, boxed=True,
animatable=True, complex_color=True, need_clone=True,
spec="https://drafts.csswg.org/css-ui/#propdef-outline-color")}
<%helpers:longhand name="outline-style" need_clone="True" animatable="False"

View file

@ -22,10 +22,9 @@ ${helpers.single_keyword("vector-effect", "none non-scaling-stroke",
${helpers.predefined_type(
"stop-color", "CSSColor",
"CSSParserColor::RGBA(RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0 })",
"CSSParserColor::RGBA(RGBA::new(0, 0, 0, 255))",
products="gecko",
animatable=False,
boxed=True,
spec="https://www.w3.org/TR/SVGTiny12/painting.html#StopColorProperty")}
${helpers.predefined_type("stop-opacity", "Opacity", "1.0",
@ -37,10 +36,9 @@ ${helpers.predefined_type("stop-opacity", "Opacity", "1.0",
${helpers.predefined_type(
"flood-color", "CSSColor",
"CSSParserColor::RGBA(RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0 })",
"CSSParserColor::RGBA(RGBA::new(0, 0, 0, 255))",
products="gecko",
animatable=False,
boxed=True,
spec="https://www.w3.org/TR/SVG/filters.html#FloodColorProperty")}
${helpers.predefined_type("flood-opacity", "Opacity",
@ -49,10 +47,9 @@ ${helpers.predefined_type("flood-opacity", "Opacity",
${helpers.predefined_type(
"lighting-color", "CSSColor",
"CSSParserColor::RGBA(RGBA { red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0 })",
"CSSParserColor::RGBA(RGBA::new(255, 255, 255, 255))",
products="gecko",
animatable=False,
boxed=True,
spec="https://www.w3.org/TR/SVG/filters.html#LightingColorProperty")}
// CSS Masking Module Level 1

View file

@ -28,7 +28,7 @@
pub enum Side {
Clip,
Ellipsis,
String(String),
String(Box<str>),
}
#[derive(PartialEq, Eq, Clone, Debug)]
@ -66,7 +66,7 @@
_ => Err(())
}
} else {
Ok(Side::String(try!(input.expect_string()).into_owned()))
Ok(Side::String(try!(input.expect_string()).into_owned().into_boxed_str()))
}
}
}
@ -210,9 +210,8 @@ ${helpers.single_keyword("text-decoration-style",
${helpers.predefined_type(
"text-decoration-color", "CSSColor",
"CSSParserColor::RGBA(RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0 })",
"CSSParserColor::RGBA(RGBA::new(0, 0, 0, 255))",
complex_color=True,
products="gecko",
animatable=True,
boxed=True,
spec="https://drafts.csswg.org/css-text-decor/#propdef-text-decoration-color")}

View file

@ -61,7 +61,7 @@ pub fn serialize_four_sides<W, I>(dest: &mut W,
fn serialize_directional_border<W, I,>(dest: &mut W,
width: &DeclaredValue<I>,
style: &DeclaredValue<BorderStyle>,
color: &DeclaredValue<Box<CSSColor>>)
color: &DeclaredValue<CSSColor>)
-> fmt::Result where W: fmt::Write, I: ToCss {
match *width {
DeclaredValue::Value(ref width) => {

View file

@ -43,14 +43,14 @@ no_viewport_percentage!(i32); // For PropertyDeclaration::Order
#[allow(missing_docs)]
pub struct CSSColor {
pub parsed: cssparser::Color,
pub authored: Option<String>,
pub authored: Option<Box<str>>,
}
impl Parse for CSSColor {
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
let start_position = input.position();
let authored = match input.next() {
Ok(Token::Ident(s)) => Some(s.into_owned()),
Ok(Token::Ident(s)) => Some(s.into_owned().into_boxed_str()),
_ => None,
};
input.reset(start_position);
@ -77,7 +77,7 @@ impl ToCss for CSSColor {
#[allow(missing_docs)]
pub struct CSSRGBA {
pub parsed: cssparser::RGBA,
pub authored: Option<String>,
pub authored: Option<Box<str>>,
}
no_viewport_percentage!(CSSRGBA);

View file

@ -15,7 +15,7 @@ servo = ["heapsize", "heapsize_derive", "serde", "serde_derive",
[dependencies]
app_units = "0.3"
cssparser = "0.7"
cssparser = "0.8"
euclid = "0.10.1"
heapsize = {version = "0.3.0", optional = true}
heapsize_derive = {version = "0.1", optional = true}