use embedded_graphics::pixelcolor::RgbColor; pub fn color_to_mw(color : &T) -> u32 { const RED_MW : u32 = 16 * 5; //< 16mA @ 5v = 80mW const GREEN_MW : u32 = 11 * 5; //< 11mA @ 5v = 55mW const BLUE_MW : u32 = 15 * 5; //< 15mA @ 5v = 75mW const DARK_MW : u32 = 1 * 5; //< 1mA @ 5v = 5mW let red = (color.r() as u32 * RED_MW).wrapping_shr(8); let green = (color.g() as u32 * GREEN_MW).wrapping_shr(8); let blue = (color.b() as u32 * BLUE_MW).wrapping_shr(8); return red + green + blue + DARK_MW; } pub fn brightness_for_mw(total_mw : u32, target : u8, max_power: u32) -> u8 { let target32 = target as u32; let requested_mw = (total_mw * target32) / 256; if requested_mw > max_power { return ((target32 * max_power) / requested_mw).try_into().unwrap(); } return target; }