aseprite_loader/binary/
blend_mode.rs

1use strum::FromRepr;
2
3use super::{
4    errors::ParseResult,
5    scalars::{word, Word},
6};
7
8#[derive(Copy, Clone, Debug, Eq, PartialEq, FromRepr)]
9pub enum BlendMode {
10    Normal,
11    Multiply,
12    Screen,
13    Overlay,
14    Darken,
15    Lighten,
16    ColorDodge,
17    ColorBurn,
18    HardLight,
19    SoftLight,
20    Difference,
21    Exclusion,
22    Hue,
23    Saturation,
24    Color,
25    Luminosity,
26    Addition,
27    Subtract,
28    Divide,
29    Unknown(Word),
30}
31
32impl From<Word> for BlendMode {
33    fn from(word: Word) -> Self {
34        BlendMode::from_repr(word.into()).unwrap_or(BlendMode::Unknown(word))
35    }
36}
37
38pub fn parse_blend_mode(input: &[u8]) -> ParseResult<'_, BlendMode> {
39    let (input, blend_mode) = word(input)?;
40    Ok((input, blend_mode.into()))
41}
42
43#[test]
44fn test_parse_blend_mode() {
45    assert_eq!(
46        parse_blend_mode(b"\x07\x00").unwrap(),
47        (&b""[..], BlendMode::ColorBurn)
48    );
49    assert_eq!(
50        parse_blend_mode(b"\x12\x00").unwrap(),
51        (&b""[..], BlendMode::Divide)
52    );
53    assert_eq!(
54        parse_blend_mode(b"\x37\x13").unwrap(),
55        (&b""[..], BlendMode::Unknown(0x1337))
56    );
57}