hex to rgb function

fn hex_to_rgb(hex: &str) -> Result<(u8, u8, u8), String> {
    if hex.len() != 6 {
        return Err("Invalid hex color code".to_string());
    }

    let r = u8::from_str_radix(&hex[0..2], 16).map_err(|_| "Invalid hex color code".to_string())?;
    let g = u8::from_str_radix(&hex[2..4], 16).map_err(|_| "Invalid hex color code".to_string())?;
    let b = u8::from_str_radix(&hex[4..6], 16).map_err(|_| "Invalid hex color code".to_string())?;

    Ok((r, g, b))
}
This is a Rust function that converts a hexadecimal color code to RGB values. It takes a string `hex` as input and returns a `Result` containing the RGB values as a tuple `(u8, u8, u8)`. If the input is not a valid 6-character hex color code, it returns an `Err` with an error message.

Here's how the function works:
1. It first checks if the length of the input `hex` is equal to 6. If not, it returns an error indicating that the hex color code is invalid.
2. It then uses the `u8::from_str_radix` function to convert the first two characters of `hex` to an `u8` value representing the red component of the RGB color. If the conversion fails, it returns an error.
3. Similarly, it converts the next two characters to the green component and the last two characters to the blue component, returning errors if the conversions fail.
4. Finally, it returns the RGB values as a tuple `(r, g, b)` wrapped in a `Result`.

You can use this function like this:
```rust
fn main() {
    let hex_code = "FF0000";
    match hex_to_rgb(hex_code) {
        Ok((r, g, b)) => println!("RGB values: {}, {}, {}", r, g, b),
        Err(err) => println!("Error: {}", err),
    }
}

This example converts the hex color code "FF0000" (which represents red) to RGB values and prints them. If the conversion fails, it prints the error message.

I hope this helps! Let me know if you have any further questions. ```