atproto blogging
1//! Color utilities for editor UI.
2
3/// Convert RGBA u32 (packed as 0xRRGGBBAA) to CSS rgba() string.
4pub fn rgba_u32_to_css(color: u32) -> String {
5 let r = (color >> 24) & 0xFF;
6 let g = (color >> 16) & 0xFF;
7 let b = (color >> 8) & 0xFF;
8 let a = (color & 0xFF) as f32 / 255.0;
9 format!("rgba({}, {}, {}, {})", r, g, b, a)
10}
11
12/// Convert RGBA u32 to CSS rgba() string with a custom alpha value.
13///
14/// Useful for creating semi-transparent versions of a color (e.g., selection highlights).
15pub fn rgba_u32_to_css_alpha(color: u32, alpha: f32) -> String {
16 let r = (color >> 24) & 0xFF;
17 let g = (color >> 16) & 0xFF;
18 let b = (color >> 8) & 0xFF;
19 format!("rgba({}, {}, {}, {})", r, g, b, alpha)
20}
21
22#[cfg(test)]
23mod tests {
24 use super::*;
25
26 #[test]
27 fn test_rgba_to_css() {
28 // Fully opaque red
29 assert_eq!(rgba_u32_to_css(0xFF0000FF), "rgba(255, 0, 0, 1)");
30 // Semi-transparent green
31 assert_eq!(rgba_u32_to_css(0x00FF0080), "rgba(0, 255, 0, 0.5019608)");
32 // Fully transparent blue
33 assert_eq!(rgba_u32_to_css(0x0000FF00), "rgba(0, 0, 255, 0)");
34 }
35
36 #[test]
37 fn test_rgba_to_css_alpha() {
38 // Red with 25% alpha override
39 assert_eq!(rgba_u32_to_css_alpha(0xFF0000FF, 0.25), "rgba(255, 0, 0, 0.25)");
40 }
41}