Created
January 19, 2019 05:31
-
-
Save archer884/deb02e9b2251406039e88760cb436489 to your computer and use it in GitHub Desktop.
Blend function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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