Skip to content

Commit 975d535

Browse files
committed
refactor!: Use a proper Color struct for the color properties
1 parent dbf81c9 commit 975d535

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

common/src/lib.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,19 @@ impl Flag {
729729
}
730730
}
731731

732+
/// A color represented in 8-bit sRGB plus alpha.
733+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
734+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
735+
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
736+
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
737+
#[repr(C)]
738+
pub struct Color {
739+
pub r: u8,
740+
pub g: u8,
741+
pub b: u8,
742+
pub a: u8,
743+
}
744+
732745
// The following is based on the technique described here:
733746
// https://viruta.org/reducing-memory-consumption-in-librsvg-2.html
734747

@@ -740,7 +753,7 @@ enum PropertyValue {
740753
String(Box<str>),
741754
F64(f64),
742755
Usize(usize),
743-
Color(u32),
756+
Color(Color),
744757
TextDecoration(TextDecoration),
745758
LengthSlice(Box<[u8]>),
746759
CoordSlice(Box<[f32]>),
@@ -1337,14 +1350,14 @@ macro_rules! color_property_methods {
13371350
($($(#[$doc:meta])* ($id:ident, $getter:ident, $setter:ident, $clearer:ident)),+) => {
13381351
$(property_methods! {
13391352
$(#[$doc])*
1340-
($id, $getter, get_color_property, Option<u32>, $setter, set_color_property, u32, $clearer)
1353+
($id, $getter, get_color_property, Option<Color>, $setter, set_color_property, Color, $clearer)
13411354
})*
13421355
impl Node {
13431356
option_properties_debug_method! { debug_color_properties, [$($getter,)*] }
13441357
}
13451358
$(#[cfg(test)]
13461359
mod $getter {
1347-
use super::{Node, Role};
1360+
use super::{Color, Node, Role};
13481361

13491362
#[test]
13501363
fn getter_should_return_default_value() {
@@ -1354,13 +1367,13 @@ macro_rules! color_property_methods {
13541367
#[test]
13551368
fn setter_should_update_the_property() {
13561369
let mut node = Node::new(Role::Unknown);
1357-
node.$setter(1);
1358-
assert_eq!(node.$getter(), Some(1));
1370+
node.$setter(Color { r: 255, g: 255, b: 255, a: 255 });
1371+
assert_eq!(node.$getter(), Some(Color { r: 255, g: 255, b: 255, a: 255 }));
13591372
}
13601373
#[test]
13611374
fn clearer_should_reset_the_property() {
13621375
let mut node = Node::new(Role::Unknown);
1363-
node.$setter(1);
1376+
node.$setter(Color { r: 255, g: 255, b: 255, a: 255 });
13641377
node.$clearer();
13651378
assert!(node.$getter().is_none());
13661379
}
@@ -1676,7 +1689,7 @@ copy_type_getters! {
16761689
(get_node_id_property, NodeId, NodeId),
16771690
(get_f64_property, f64, F64),
16781691
(get_usize_property, usize, Usize),
1679-
(get_color_property, u32, Color),
1692+
(get_color_property, Color, Color),
16801693
(get_text_decoration_property, TextDecoration, TextDecoration),
16811694
(get_bool_property, bool, Bool)
16821695
}
@@ -1694,7 +1707,7 @@ copy_type_setters! {
16941707
(set_node_id_property, NodeId, NodeId),
16951708
(set_f64_property, f64, F64),
16961709
(set_usize_property, usize, Usize),
1697-
(set_color_property, u32, Color),
1710+
(set_color_property, Color, Color),
16981711
(set_text_decoration_property, TextDecoration, TextDecoration),
16991712
(set_bool_property, bool, Bool)
17001713
}
@@ -1828,11 +1841,11 @@ usize_property_methods! {
18281841
}
18291842

18301843
color_property_methods! {
1831-
/// For [`Role::ColorWell`], specifies the selected color in RGBA.
1844+
/// For [`Role::ColorWell`], specifies the selected color.
18321845
(ColorValue, color_value, set_color_value, clear_color_value),
1833-
/// Background color in RGBA.
1846+
/// Background color.
18341847
(BackgroundColor, background_color, set_background_color, clear_background_color),
1835-
/// Foreground color in RGBA.
1848+
/// Foreground color.
18361849
(ForegroundColor, foreground_color, set_foreground_color, clear_foreground_color)
18371850
}
18381851

@@ -2516,7 +2529,7 @@ impl JsonSchema for Properties {
25162529
SizeOfSet,
25172530
PositionInSet
25182531
},
2519-
u32 {
2532+
Color {
25202533
ColorValue,
25212534
BackgroundColor,
25222535
ForegroundColor

consumer/src/text.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// the LICENSE-MIT file), at your option.
55

66
use accesskit::{
7-
Node as NodeData, NodeId, Point, Rect, Role, TextAlign, TextDecoration, TextDirection,
7+
Color, Node as NodeData, NodeId, Point, Rect, Role, TextAlign, TextDecoration, TextDirection,
88
TextPosition as WeakPosition, TextSelection, VerticalOffset,
99
};
1010
use alloc::{string::String, vec::Vec};
@@ -1178,8 +1178,8 @@ inherited_properties! {
11781178
(language, &'a str, set_language, "en", "fr"),
11791179
(font_size, f64, set_font_size, 12.0, 24.0),
11801180
(font_weight, f64, set_font_weight, 400.0, 700.0),
1181-
(background_color, u32, set_background_color, 0xffffff, 0xff),
1182-
(foreground_color, u32, set_foreground_color, 0x0, 0xff00),
1181+
(background_color, Color, set_background_color, accesskit::Color { r: 255, g: 255, b: 255, a: 255 }, accesskit::Color { r: 255, g: 0, b: 0, a: 255 }),
1182+
(foreground_color, Color, set_foreground_color, accesskit::Color { r: 0, g: 0, b: 0, a: 255 }, accesskit::Color { r: 0, g: 0, b: 255, a: 255 }),
11831183
(overline, TextDecoration, set_overline, accesskit::TextDecoration::Solid, accesskit::TextDecoration::Dotted),
11841184
(strikethrough, TextDecoration, set_strikethrough, accesskit::TextDecoration::Dotted, accesskit::TextDecoration::Dashed),
11851185
(underline, TextDecoration, set_underline, accesskit::TextDecoration::Dashed, accesskit::TextDecoration::Double),

0 commit comments

Comments
 (0)