1use serde::{Deserialize, Serialize};
2
3#[serde_with::serde_as]
11#[serde_with::skip_serializing_none]
12#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
13#[serde(default, rename_all = "camelCase")]
14#[non_exhaustive]
15pub struct Color {
16 #[serde_as(as = "crate::catalog::NotNull")]
18 pub primaries: Option<u8>,
19
20 #[serde_as(as = "crate::catalog::NotNull")]
22 pub transfer: Option<u8>,
23
24 #[serde_as(as = "crate::catalog::NotNull")]
26 pub matrix: Option<u8>,
27
28 #[serde_as(as = "crate::catalog::NotNull")]
30 pub range: Option<ColorRange>,
31
32 #[serde_as(as = "crate::catalog::NotNull")]
34 pub mastering: Option<Mastering>,
35
36 #[serde_as(as = "crate::catalog::NotNull")]
38 pub content_light: Option<ContentLight>,
39}
40
41#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
43#[serde(rename_all = "lowercase")]
44pub enum ColorRange {
45 Limited,
47 Full,
49}
50
51#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
53#[serde(rename_all = "camelCase")]
54pub struct Mastering {
55 pub red: Chromaticity,
57 pub green: Chromaticity,
59 pub blue: Chromaticity,
61 pub white: Chromaticity,
63 pub max_luminance: f64,
65 pub min_luminance: f64,
67}
68
69#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
71pub struct Chromaticity {
72 pub x: f64,
74 pub y: f64,
76}
77
78#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
80#[serde(rename_all = "camelCase")]
81pub struct ContentLight {
82 pub max_cll: u16,
84 pub max_fall: u16,
86}
87
88#[cfg(test)]
89mod test {
90 use super::*;
91
92 #[test]
93 fn hdr10_round_trips() {
94 let color = Color {
95 primaries: Some(9),
96 transfer: Some(16),
97 matrix: Some(9),
98 range: Some(ColorRange::Limited),
99 mastering: Some(Mastering {
100 red: Chromaticity { x: 0.708, y: 0.292 },
101 green: Chromaticity { x: 0.17, y: 0.797 },
102 blue: Chromaticity { x: 0.131, y: 0.046 },
103 white: Chromaticity { x: 0.3127, y: 0.329 },
104 max_luminance: 1000.0,
105 min_luminance: 0.005,
106 }),
107 content_light: Some(ContentLight {
108 max_cll: 1000,
109 max_fall: 400,
110 }),
111 };
112
113 let encoded = serde_json::to_value(&color).unwrap();
114 assert_eq!(
115 encoded,
116 serde_json::json!({
117 "primaries": 9,
118 "transfer": 16,
119 "matrix": 9,
120 "range": "limited",
121 "mastering": {
122 "red": { "x": 0.708, "y": 0.292 },
123 "green": { "x": 0.17, "y": 0.797 },
124 "blue": { "x": 0.131, "y": 0.046 },
125 "white": { "x": 0.3127, "y": 0.329 },
126 "maxLuminance": 1000.0,
127 "minLuminance": 0.005,
128 },
129 "contentLight": { "maxCll": 1000, "maxFall": 400 },
130 })
131 );
132 assert_eq!(serde_json::from_value::<Color>(encoded).unwrap(), color);
133 }
134
135 #[test]
136 fn unspecified_fields_are_omitted() {
137 let color = Color {
138 matrix: Some(1),
139 ..Default::default()
140 };
141 assert_eq!(
142 serde_json::to_value(&color).unwrap(),
143 serde_json::json!({ "matrix": 1 })
144 );
145 }
146}