mabel_aseprite/
user_data.rs

1use crate::{reader::AseReader, Result};
2
3/// User-provided metadata which can be attached to various items.
4///
5/// Aseprite allows attaching user data to several entities, both via the GUI
6/// and via extensions. For an example see the discussion
7/// [How to associate data to each cel](https://community.aseprite.org/t/how-to-associate-data-to-each-cel-frame/6307).
8#[derive(Debug, Clone, PartialEq)]
9pub struct UserData {
10    /// User-provided string data.
11    pub text: Option<String>,
12    /// User-provided color.
13    pub color: Option<image::Rgba<u8>>,
14}
15
16pub(crate) fn parse_userdata_chunk(data: &[u8]) -> Result<UserData> {
17    let mut reader = AseReader::new(data);
18
19    let flags = reader.dword()?;
20    let text = if flags & 1 != 0 {
21        let s = reader.string()?;
22        Some(s)
23    } else {
24        None
25    };
26    let color = if flags & 2 != 0 {
27        let red = reader.byte()?;
28        let green = reader.byte()?;
29        let blue = reader.byte()?;
30        let alpha = reader.byte()?;
31        let rgba = image::Rgba([red, green, blue, alpha]);
32        Some(rgba)
33    } else {
34        None
35    };
36
37    Ok(UserData { text, color })
38}