Skip to content

Instantly share code, notes, and snippets.

@pmkroeker
Forked from rust-play/playground.rs
Last active June 3, 2020 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmkroeker/0ca42dd7ca5085265b07b833825f8525 to your computer and use it in GitHub Desktop.
Save pmkroeker/0ca42dd7ca5085265b07b833825f8525 to your computer and use it in GitHub Desktop.
// Convert number to hex string.
pub fn to_hex (value: i32) -> String {
let value = if value > 255 { 255 } else if value < 0 { 0 } else { value };
format!("{:02X}", value)
}
// Join all hex strings together from rgb input.
pub fn rgb (r: i32, g: i32, b: i32) -> String {
format!("{}{}{}", to_hex(r), to_hex(g), to_hex(b))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_be_black() {
assert_eq!(rgb(255, 255, 255), "FFFFFF".to_string());
}
#[test]
fn it_should_be_black_too() {
assert_eq!(rgb(255, 255, 300), "FFFFFF".to_string());
}
#[test]
fn it_should_be_white() {
assert_eq!(rgb(0, 0, 0), "000000".to_string());
}
#[test]
fn it_should_be_other() {
assert_eq!(rgb(148, 0, 211), "9400D3".to_string());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment