Skip to main content

gecol_core/extract/
config.rs

1use std::hash::Hash;
2
3use serde::{Deserialize, Serialize};
4
5/// Holds all the extraction configuration.
6#[derive(Debug, Serialize, Deserialize)]
7pub struct ExtractionConfig {
8    // The width image to extract color from resizes to. If set to `None`, it
9    // keeps the original width.
10    #[serde(rename = "resize_width", default = "default_resize")]
11    pub res_w: Option<u32>,
12    // The height image to extract color from resizes to. If set to `None`, it
13    // keeps the original height.
14    #[serde(rename = "resize_height", default = "default_resize")]
15    pub res_h: Option<u32>,
16
17    // Saturation threshold for a pixel to be consider for the extraction.
18    // If the pixels saturation is lower than this value, it's skipped.
19    #[serde(rename = "saturation_threshold", default = "default_sat_thresh")]
20    pub sat_thresh: f32,
21    // Value threshold for a pixel to be consider for the extraction.
22    // If the pixels value is lower than this value, it's skipped.
23    #[serde(rename = "value_threshold", default = "default_val_thresh")]
24    pub val_thresh: f32,
25
26    // Threshold for the saliency to be used. If the average pixel saliency
27    // is lower than this value, saliency is not used.
28    #[serde(rename = "saliency_threshold", default)]
29    pub sal_thresh: f32,
30    // By how much the pixels saliency is multiplied by for its score.
31    #[serde(rename = "saliency_bonus", default = "default_sal_bonus")]
32    pub sal_bonus: f32,
33    // By how much the pixels warmth factor is multiplied by for its score.
34    #[serde(default = "default_warmth_bonus")]
35    pub warmth_bonus: f32,
36
37    // Number of cluster used in the k-means clustering of the pixels:
38    #[serde(default = "default_clusters")]
39    pub clusters: usize,
40
41    // By how much the final cluster's vibrancy (chroma) is multiplied.
42    #[serde(rename = "vibrancy_bonus", default = "default_vibr_bonus")]
43    pub vibr_bonus: f32,
44    // By how much the final cluster's dominance (pixel mass) is multiplied.
45    #[serde(rename = "dominance_bonus", default = "default_dom_bonus")]
46    pub dom_bonus: f32,
47}
48
49impl Default for ExtractionConfig {
50    fn default() -> Self {
51        Self {
52            res_w: default_resize(),
53            res_h: default_resize(),
54            sat_thresh: default_sat_thresh(),
55            val_thresh: default_val_thresh(),
56            sal_thresh: Default::default(),
57            sal_bonus: default_sal_bonus(),
58            warmth_bonus: default_warmth_bonus(),
59            vibr_bonus: default_vibr_bonus(),
60            dom_bonus: default_dom_bonus(),
61            clusters: default_clusters(),
62        }
63    }
64}
65
66impl Hash for ExtractionConfig {
67    /// Hashes the config state.
68    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
69        self.res_w.hash(state);
70        self.res_h.hash(state);
71        self.sat_thresh.to_bits().hash(state);
72        self.val_thresh.to_bits().hash(state);
73        self.sal_thresh.to_bits().hash(state);
74        self.sal_bonus.to_bits().hash(state);
75        self.warmth_bonus.to_bits().hash(state);
76        self.clusters.hash(state);
77        self.vibr_bonus.to_bits().hash(state);
78        self.dom_bonus.to_bits().hash(state);
79    }
80}
81
82fn default_resize() -> Option<u32> {
83    Some(255)
84}
85
86fn default_sat_thresh() -> f32 {
87    0.2
88}
89
90fn default_val_thresh() -> f32 {
91    0.15
92}
93
94fn default_sal_bonus() -> f32 {
95    1.5
96}
97
98fn default_warmth_bonus() -> f32 {
99    0.1
100}
101
102fn default_clusters() -> usize {
103    16
104}
105
106fn default_vibr_bonus() -> f32 {
107    2.5
108}
109
110fn default_dom_bonus() -> f32 {
111    1.
112}