Skip to content

Commit 922e882

Browse files
authored
refactor!: Use a proper Color struct for the color properties (#663)
1 parent 176c90c commit 922e882

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

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 red: u8,
740+
pub green: u8,
741+
pub blue: u8,
742+
pub alpha: 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

@@ -741,7 +754,7 @@ enum PropertyValue {
741754
F64(f64),
742755
F32(f32),
743756
Usize(usize),
744-
Color(u32),
757+
Color(Color),
745758
TextDecoration(TextDecoration),
746759
LengthSlice(Box<[u8]>),
747760
CoordSlice(Box<[f32]>),
@@ -1375,14 +1388,14 @@ macro_rules! color_property_methods {
13751388
($($(#[$doc:meta])* ($id:ident, $getter:ident, $setter:ident, $clearer:ident)),+) => {
13761389
$(property_methods! {
13771390
$(#[$doc])*
1378-
($id, $getter, get_color_property, Option<u32>, $setter, set_color_property, u32, $clearer)
1391+
($id, $getter, get_color_property, Option<Color>, $setter, set_color_property, Color, $clearer)
13791392
})*
13801393
impl Node {
13811394
option_properties_debug_method! { debug_color_properties, [$($getter,)*] }
13821395
}
13831396
$(#[cfg(test)]
13841397
mod $getter {
1385-
use super::{Node, Role};
1398+
use super::{Color, Node, Role};
13861399

13871400
#[test]
13881401
fn getter_should_return_default_value() {
@@ -1392,13 +1405,13 @@ macro_rules! color_property_methods {
13921405
#[test]
13931406
fn setter_should_update_the_property() {
13941407
let mut node = Node::new(Role::Unknown);
1395-
node.$setter(1);
1396-
assert_eq!(node.$getter(), Some(1));
1408+
node.$setter(Color { red: 255, green: 255, blue: 255, alpha: 255 });
1409+
assert_eq!(node.$getter(), Some(Color { red: 255, green: 255, blue: 255, alpha: 255 }));
13971410
}
13981411
#[test]
13991412
fn clearer_should_reset_the_property() {
14001413
let mut node = Node::new(Role::Unknown);
1401-
node.$setter(1);
1414+
node.$setter(Color { red: 255, green: 255, blue: 255, alpha: 255 });
14021415
node.$clearer();
14031416
assert!(node.$getter().is_none());
14041417
}
@@ -1715,7 +1728,7 @@ copy_type_getters! {
17151728
(get_f64_property, f64, F64),
17161729
(get_f32_property, f32, F32),
17171730
(get_usize_property, usize, Usize),
1718-
(get_color_property, u32, Color),
1731+
(get_color_property, Color, Color),
17191732
(get_text_decoration_property, TextDecoration, TextDecoration),
17201733
(get_bool_property, bool, Bool)
17211734
}
@@ -1734,7 +1747,7 @@ copy_type_setters! {
17341747
(set_f64_property, f64, F64),
17351748
(set_f32_property, f32, F32),
17361749
(set_usize_property, usize, Usize),
1737-
(set_color_property, u32, Color),
1750+
(set_color_property, Color, Color),
17381751
(set_text_decoration_property, TextDecoration, TextDecoration),
17391752
(set_bool_property, bool, Bool)
17401753
}
@@ -1871,11 +1884,11 @@ usize_property_methods! {
18711884
}
18721885

18731886
color_property_methods! {
1874-
/// For [`Role::ColorWell`], specifies the selected color in RGBA.
1887+
/// For [`Role::ColorWell`], specifies the selected color.
18751888
(ColorValue, color_value, set_color_value, clear_color_value),
1876-
/// Background color in RGBA.
1889+
/// Background color.
18771890
(BackgroundColor, background_color, set_background_color, clear_background_color),
1878-
/// Foreground color in RGBA.
1891+
/// Foreground color.
18791892
(ForegroundColor, foreground_color, set_foreground_color, clear_foreground_color)
18801893
}
18811894

@@ -2565,7 +2578,7 @@ impl JsonSchema for Properties {
25652578
SizeOfSet,
25662579
PositionInSet
25672580
},
2568-
u32 {
2581+
Color {
25692582
ColorValue,
25702583
BackgroundColor,
25712584
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, f32, set_font_size, 12.0, 24.0),
11801180
(font_weight, f32, 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 { red: 255, green: 255, blue: 255, alpha: 255 }, accesskit::Color { red: 255, green: 0, blue: 0, alpha: 255 }),
1182+
(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 }),
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)