diff --git a/common/src/lib.rs b/common/src/lib.rs index 1ba8b2c2..82377fc1 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -729,6 +729,19 @@ impl Flag { } } +/// A color represented in 8-bit sRGB plus alpha. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "serde", serde(deny_unknown_fields))] +#[repr(C)] +pub struct Color { + pub red: u8, + pub green: u8, + pub blue: u8, + pub alpha: u8, +} + // The following is based on the technique described here: // https://viruta.org/reducing-memory-consumption-in-librsvg-2.html @@ -740,7 +753,7 @@ enum PropertyValue { String(Box), F64(f64), Usize(usize), - Color(u32), + Color(Color), TextDecoration(TextDecoration), LengthSlice(Box<[u8]>), CoordSlice(Box<[f32]>), @@ -1337,14 +1350,14 @@ macro_rules! color_property_methods { ($($(#[$doc:meta])* ($id:ident, $getter:ident, $setter:ident, $clearer:ident)),+) => { $(property_methods! { $(#[$doc])* - ($id, $getter, get_color_property, Option, $setter, set_color_property, u32, $clearer) + ($id, $getter, get_color_property, Option, $setter, set_color_property, Color, $clearer) })* impl Node { option_properties_debug_method! { debug_color_properties, [$($getter,)*] } } $(#[cfg(test)] mod $getter { - use super::{Node, Role}; + use super::{Color, Node, Role}; #[test] fn getter_should_return_default_value() { @@ -1354,13 +1367,13 @@ macro_rules! color_property_methods { #[test] fn setter_should_update_the_property() { let mut node = Node::new(Role::Unknown); - node.$setter(1); - assert_eq!(node.$getter(), Some(1)); + node.$setter(Color { red: 255, green: 255, blue: 255, alpha: 255 }); + assert_eq!(node.$getter(), Some(Color { red: 255, green: 255, blue: 255, alpha: 255 })); } #[test] fn clearer_should_reset_the_property() { let mut node = Node::new(Role::Unknown); - node.$setter(1); + node.$setter(Color { red: 255, green: 255, blue: 255, alpha: 255 }); node.$clearer(); assert!(node.$getter().is_none()); } @@ -1676,7 +1689,7 @@ copy_type_getters! { (get_node_id_property, NodeId, NodeId), (get_f64_property, f64, F64), (get_usize_property, usize, Usize), - (get_color_property, u32, Color), + (get_color_property, Color, Color), (get_text_decoration_property, TextDecoration, TextDecoration), (get_bool_property, bool, Bool) } @@ -1694,7 +1707,7 @@ copy_type_setters! { (set_node_id_property, NodeId, NodeId), (set_f64_property, f64, F64), (set_usize_property, usize, Usize), - (set_color_property, u32, Color), + (set_color_property, Color, Color), (set_text_decoration_property, TextDecoration, TextDecoration), (set_bool_property, bool, Bool) } @@ -1828,11 +1841,11 @@ usize_property_methods! { } color_property_methods! { - /// For [`Role::ColorWell`], specifies the selected color in RGBA. + /// For [`Role::ColorWell`], specifies the selected color. (ColorValue, color_value, set_color_value, clear_color_value), - /// Background color in RGBA. + /// Background color. (BackgroundColor, background_color, set_background_color, clear_background_color), - /// Foreground color in RGBA. + /// Foreground color. (ForegroundColor, foreground_color, set_foreground_color, clear_foreground_color) } @@ -2516,7 +2529,7 @@ impl JsonSchema for Properties { SizeOfSet, PositionInSet }, - u32 { + Color { ColorValue, BackgroundColor, ForegroundColor diff --git a/consumer/src/text.rs b/consumer/src/text.rs index e1c6f70e..e4a96625 100644 --- a/consumer/src/text.rs +++ b/consumer/src/text.rs @@ -4,7 +4,7 @@ // the LICENSE-MIT file), at your option. use accesskit::{ - Node as NodeData, NodeId, Point, Rect, Role, TextAlign, TextDecoration, TextDirection, + Color, Node as NodeData, NodeId, Point, Rect, Role, TextAlign, TextDecoration, TextDirection, TextPosition as WeakPosition, TextSelection, VerticalOffset, }; use alloc::{string::String, vec::Vec}; @@ -1178,8 +1178,8 @@ inherited_properties! { (language, &'a str, set_language, "en", "fr"), (font_size, f64, set_font_size, 12.0, 24.0), (font_weight, f64, set_font_weight, 400.0, 700.0), - (background_color, u32, set_background_color, 0xffffff, 0xff), - (foreground_color, u32, set_foreground_color, 0x0, 0xff00), + (background_color, Color, set_background_color, accesskit::Color { red: 255, green: 255, blue: 255, alpha: 255 }, accesskit::Color { red: 255, green: 0, blue: 0, alpha: 255 }), + (foreground_color, Color, set_foreground_color, accesskit::Color { red: 0, green: 0, blue: 0, alpha: 255 }, accesskit::Color { red: 0, green: 0, blue: 255, alpha: 255 }), (overline, TextDecoration, set_overline, accesskit::TextDecoration::Solid, accesskit::TextDecoration::Dotted), (strikethrough, TextDecoration, set_strikethrough, accesskit::TextDecoration::Dotted, accesskit::TextDecoration::Dashed), (underline, TextDecoration, set_underline, accesskit::TextDecoration::Dashed, accesskit::TextDecoration::Double),