1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/// Color conversion utilities
///
/// Provides functions for converting between hex color codes and RGB values
///
/// # Examples
///
/// ```ignore
/// use doe::color;
///
/// // Convert hex to RGB
/// let rgb = color::hex_to_rgb("#FF5733").unwrap();
/// assert_eq!(rgb, (255, 87, 51));
///
/// // Convert RGB to hex
/// let hex = color::rgb_to_hex((255, 87, 51));
/// assert_eq!(hex, "#FF5733");
/// ```
#[allow(warnings)]
pub mod color {
/// Converts a hex color string to RGB values
///
/// # Arguments
///
/// * `hex` - A string slice containing a hex color code (e.g. "#FF5733")
///
/// # Returns
///
/// Result containing a tuple of (red, green, blue) values or an error string
///
/// # Example
///
/// ```ignore
/// use doe::color;
///
/// match color::hex_to_rgb("#FF5733") {
/// Ok((r, g, b)) => println!("Red: {}, Green: {}, Blue: {}", r, g, b),
/// Err(e) => eprintln!("Error: {}", e),
/// }
/// ```
pub fn hex_to_rgb(hex: &str) -> Result<(u8, u8, u8), String> {
if hex.len() != 7 || !hex.starts_with('#') {
return Err("Invalid hex color format. Expected format: #RRGGBB".to_string());
}
let r = u8::from_str_radix(&hex[1..3], 16).map_err(|e| e.to_string())?;
let g = u8::from_str_radix(&hex[3..5], 16).map_err(|e| e.to_string())?;
let b = u8::from_str_radix(&hex[5..7], 16).map_err(|e| e.to_string())?;
Ok((r, g, b))
}
/// Converts RGB values to a hex color string
///
/// # Arguments
///
/// * `rgb` - A tuple of (red, green, blue) values (0-255)
///
/// # Returns
///
/// String containing the hex color code (e.g. "#FF5733")
///
/// # Example
///
/// ```ignore
/// use doe::color;
///
/// let hex = color::rgb_to_hex((255, 87, 51));
/// assert_eq!(hex, "#FF5733");
/// ```
pub fn rgb_to_hex(rgb: (u8, u8, u8)) -> String {
format!("#{:02X}{:02X}{:02X}", rgb.0, rgb.1, rgb.2)
}
}