Skip to content

Commit

Permalink
feat(blend): blend support alpha value
Browse files Browse the repository at this point in the history
  • Loading branch information
JiatLn committed Mar 31, 2024
1 parent 6df7153 commit 18a1b12
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/color_calc/blend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,22 @@ pub enum BlendMode {
/// assert_eq!(blended_color.hex(), "#8ef6fa");
/// ```
pub fn blend(backdrop_color: &Color, source_color: &Color, mode: BlendMode) -> Color {
let backdrop_vec = backdrop_color.vec_of(ColorSpace::RGB);
let source_vec = source_color.vec_of(ColorSpace::RGB);

let zip_vec = backdrop_vec
.iter()
.zip(source_vec.iter())
.map(|(a, b)| (a / 255.0, b / 255.0));
let backdrop_vec = backdrop_color.vec_of(ColorSpace::RGBA);
let source_vec = source_color.vec_of(ColorSpace::RGBA);

let zip_vec =
backdrop_vec
.iter()
.zip(source_vec.iter())
.enumerate()
.map(|(index, (&a, &b))| {
// aplha
if index == 3 {
(a, b)
} else {
(a / 255.0, b / 255.0)
}
});

let v: Vec<_> = match mode {
BlendMode::Normal => zip_vec.map(|(a, b)| normal(a, b)).collect(),
Expand All @@ -133,8 +142,9 @@ pub fn blend(backdrop_color: &Color, source_color: &Color, mode: BlendMode) -> C
let r = v[0] * 255.0;
let g = v[1] * 255.0;
let b = v[2] * 255.0;
let a = v[3];

Color::new(r, g, b, 1.0)
Color::new(r, g, b, a)
}

#[cfg(test)]
Expand Down

0 comments on commit 18a1b12

Please sign in to comment.