Skip to content

Instantly share code, notes, and snippets.

@archer884
Created January 19, 2019 05:31
Show Gist options
  • Save archer884/deb02e9b2251406039e88760cb436489 to your computer and use it in GitHub Desktop.
Save archer884/deb02e9b2251406039e88760cb436489 to your computer and use it in GitHub Desktop.
Blend function
fn blend(colors: impl IntoIterator<Item = Hex>) -> Option<Hex> {
let mut count = 0;
let mut a_sum = 0.0;
let mut b_sum = 0.0;
let mut c_sum = 0.0;
for color in colors {
let Hex(a, b, c) = color;
count += 1;
a_sum += f64::from(a);
b_sum += f64::from(b);
c_sum += f64::from(c);
}
match count {
0 => None,
_ => {
let count = f64::from(count);
Some(Hex(
(a_sum / count).round() as u8,
(b_sum / count).round() as u8,
(c_sum / count).round() as u8,
))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment