1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use ici_files::image::IndexedImage;
use ici_files::prelude::AnimatedIndexedImage;
use ici_files::IciColor;
pub trait ChangeColors {
fn with_saturate(&self, amount: f32) -> Self;
fn saturate(&self) -> Self
where
Self: Sized,
{
self.with_saturate(-0.1)
}
fn desaturate(&self) -> Self
where
Self: Sized,
{
self.with_saturate(0.1)
}
fn with_brightness(&self, amount: f32) -> Self;
fn lighten(&self) -> Self
where
Self: Sized,
{
self.with_brightness(1.1)
}
fn darken(&self) -> Self
where
Self: Sized,
{
self.with_brightness(0.9)
}
}
impl ChangeColors for IciColor {
fn with_saturate(&self, amount: f32) -> Self {
let mut rgba = [
self.r as f32 / 255.0,
self.g as f32 / 255.0,
self.b as f32 / 255.0,
];
let lum = 0.2989 * rgba[0] + 0.5870 * rgba[1] + 0.1140 * rgba[2];
rgba[0] = rgba[0] + amount * (lum - rgba[0]);
rgba[1] = rgba[1] + amount * (lum - rgba[1]);
rgba[2] = rgba[2] + amount * (lum - rgba[2]);
IciColor::new(
(rgba[0] * 255.0) as u8,
(rgba[1] * 255.0) as u8,
(rgba[2] * 255.0) as u8,
self.a,
)
}
fn with_brightness(&self, amount: f32) -> Self {
IciColor::new(
((((self.r as f32) / 255.0) * amount).min(1.0).max(0.0) * 255.0) as u8,
((((self.g as f32) / 255.0) * amount).min(1.0).max(0.0) * 255.0) as u8,
((((self.b as f32) / 255.0) * amount).min(1.0).max(0.0) * 255.0) as u8,
self.a,
)
}
}
impl ChangeColors for Vec<IciColor> {
fn with_saturate(&self, amount: f32) -> Self {
let mut colors = vec![];
for color in self {
colors.push(color.with_saturate(amount));
}
colors
}
fn with_brightness(&self, amount: f32) -> Self {
let mut colors = vec![];
for color in self {
colors.push(color.with_brightness(amount));
}
colors
}
}
impl ChangeColors for IndexedImage {
fn with_saturate(&self, amount: f32) -> Self {
let mut image = self.clone();
image
.set_palette(&image.get_palette().to_vec().with_saturate(amount))
.expect("Color disappeared when changing saturation");
image
}
fn with_brightness(&self, amount: f32) -> Self {
let mut image = self.clone();
image
.set_palette(&image.get_palette().to_vec().with_brightness(amount))
.expect("Color disappeared when changing saturation");
image
}
}
impl ChangeColors for AnimatedIndexedImage {
fn with_saturate(&self, amount: f32) -> Self {
let mut image = self.clone();
image
.set_palette(&image.get_palette().to_vec().with_saturate(amount))
.expect("Color disappeared when changing saturation");
image
}
fn with_brightness(&self, amount: f32) -> Self {
let mut image = self.clone();
image
.set_palette(&image.get_palette().to_vec().with_brightness(amount))
.expect("Color disappeared when changing saturation");
image
}
}