From 42ae30700aa3ae468658cd61f13640968bc9f2be Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Thu, 27 Aug 2020 11:37:00 +0200 Subject: [PATCH] Expose RGB values from Color --- lvgl/src/support.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lvgl/src/support.rs b/lvgl/src/support.rs index 600742e..bc9b0fb 100644 --- a/lvgl/src/support.rs +++ b/lvgl/src/support.rs @@ -27,6 +27,24 @@ impl Color { pub fn from_raw(raw: lvgl_sys::lv_color_t) -> Self { Self { raw } } + + pub fn r(&self) -> u8 { + unsafe { + lvgl_sys::_LV_COLOR_GET_R(self.raw) as u8 + } + } + + pub fn g(&self) -> u8 { + unsafe { + lvgl_sys::_LV_COLOR_GET_G(self.raw) as u8 + } + } + + pub fn b(&self) -> u8 { + unsafe { + lvgl_sys::_LV_COLOR_GET_B(self.raw) as u8 + } + } } impl From for Rgb888 { @@ -233,3 +251,25 @@ impl From for lvgl_sys::lv_anim_enable_t { } } } + + +#[cfg(test)] +mod test { + use super::*; + use lvgl_sys; + + #[test] + fn color_properties_accessible() { + let color = Color::from_rgb((206, 51, 255)); + + if lvgl_sys::LV_COLOR_DEPTH == 32 { + assert_eq!(color.r(), 206); + assert_eq!(color.g(), 51); + assert_eq!(color.b(), 255); + } else if lvgl_sys::LV_COLOR_DEPTH == 16 { + assert_eq!(color.r(), 25); + assert_eq!(color.g(), 12); + assert_eq!(color.b(), 31); + } + } +}