Newer
Older
use std::path::{Component, Path, PathBuf};
use image::{GenericImage, GenericImageView, Rgb, Rgba, RgbaImage, SubImage};
#[derive(Clone, Copy)]
pub struct SpriteData<'a, T: GenericImage> {
pub data: SubImage<&'a T>,
#[allow(dead_code)]
pub x: u32,
#[allow(dead_code)]
pub y: u32,
pub tx: u32,
pub ty: u32,
#[allow(dead_code)]
pub width: u32,
#[allow(dead_code)]
pub height: u32,
}
pub type OutputFormat = image::ImageBuffer<Rgba<u8>, Vec<u8>>;
#[allow(type_alias_bounds)]
pub type TypedOutputFormat<T: GenericImage> =
image::ImageBuffer<T::Pixel, Vec<<T::Pixel as image::Pixel>::Subpixel>>;
pub type RgbaOutputFormat = TypedOutputFormat<RgbaImage>;
pub fn new_image(new_width: u32, new_height: u32) -> OutputFormat {
let mut new_image = RgbaImage::new(new_width, new_height);
let (new_image_x, new_image_y, new_image_width, new_image_height) = new_image.bounds();
for x in new_image_x..new_image_width {
for y in new_image_y..new_image_height {
new_image.put_pixel(x, y, Rgba::from([0, 0, 0, 0]));
}
}
new_image
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct BasicRgba {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl BasicRgba {
pub fn hue(&self) -> f32 {
let red = self.r as f32 / u8::MAX as f32;
let green = self.g as f32 / u8::MAX as f32;
let blue = self.b as f32 / u8::MAX as f32;
let minimum = red.min(green).min(blue);
let maximum = red.max(green).max(blue);
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
let mut hue = if red >= green && red >= blue {
(green - blue) / (maximum - minimum)
} else if green >= red && green >= blue {
2.0 + (blue - red) / (maximum - minimum)
} else {
4.0 + (red - green) / (maximum - minimum)
};
hue *= 60.0;
while hue < 0.0 {
hue += 360.0;
}
hue
}
pub fn transparent() -> Self {
Self {
r: 0,
g: 0,
b: 0,
a: 0,
}
}
}
impl From<Rgba<u8>> for BasicRgba {
fn from(other: Rgba<u8>) -> Self {
Self {
r: other.0[0],
g: other.0[1],
b: other.0[2],
a: other.0[3],
}
}
}
impl From<&Rgba<u8>> for BasicRgba {
fn from(other: &Rgba<u8>) -> Self {
Self {
r: other.0[0],
g: other.0[1],
b: other.0[2],
a: other.0[3],
}
}
}
impl From<Rgb<u8>> for BasicRgba {
fn from(other: Rgb<u8>) -> Self {
Self {
r: other.0[0],
g: other.0[1],
b: other.0[2],
a: u8::MAX,
}
}
}
impl From<&Rgb<u8>> for BasicRgba {
fn from(other: &Rgb<u8>) -> Self {
Self {
r: other.0[0],
g: other.0[1],
b: other.0[2],
a: u8::MAX,
}
}
}
impl From<BasicRgba> for Rgba<u8> {
fn from(other: BasicRgba) -> Self {
Self::from([other.r, other.g, other.b, other.a])
}
}
impl From<&BasicRgba> for Rgba<u8> {
fn from(other: &BasicRgba) -> Self {
Self::from([other.r, other.g, other.b, other.a])
}
}
impl From<BasicRgba> for Rgb<u8> {
fn from(other: BasicRgba) -> Self {
Self::from([other.r, other.g, other.b])
}
}
impl From<&BasicRgba> for Rgb<u8> {
fn from(other: &BasicRgba) -> Self {
Self::from([other.r, other.g, other.b])
}
}
impl From<BasicRgba> for LabValue {
fn from(other: BasicRgba) -> Self {
let converted = lab::Lab::from_rgb(&[other.r, other.g, other.b]);
LabValue {
l: converted.l,
a: converted.a,
b: converted.b,
}
}
}
impl From<&BasicRgba> for LabValue {
fn from(other: &BasicRgba) -> Self {
let converted = lab::Lab::from_rgb(&[other.r, other.g, other.b]);
LabValue {
l: converted.l,
a: converted.a,
b: converted.b,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct BasicLab {
pub l: f32,
pub a: f32,
pub b: f32,
}
impl From<LabValue> for BasicLab {
fn from(other: LabValue) -> Self {
Self {
l: other.l,
a: other.a,
b: other.b,
}
}
}
impl From<BasicLab> for LabValue {
fn from(other: BasicLab) -> Self {
Self {
l: other.l,
a: other.a,
b: other.b,
}
}
}
impl From<BasicLab> for Lab {
fn from(other: BasicLab) -> Self {
Self {
l: other.l,
a: other.a,
b: other.b,
}
}
}
impl From<BasicLab> for BasicRgba {
fn from(other: BasicLab) -> Self {
let lab: Lab = other.into();
let vals = lab.to_rgb();
Self {
r: vals[0],
g: vals[1],
b: vals[2],
a: u8::MAX,
}
}
}
impl From<BasicRgba> for Vec3 {
fn from(other: BasicRgba) -> Self {
Vec3::new(other.r as f32, other.g as f32, other.b as f32)
}
}
impl From<BasicLab> for Vec3 {
fn from(other: BasicLab) -> Self {
Vec3::new(other.l, other.a, other.b)
}
}
impl UpperHex for BasicRgba {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!(
self.r, self.g, self.b, self.a
))
}
}
impl LowerHex for BasicRgba {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!(
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
pub struct HexStringValue(String);
pub trait HexString {
fn as_hex_string(&self) -> HexStringValue;
}
impl HexString for Rgba<u8> {
fn as_hex_string(&self) -> HexStringValue {
HexStringValue(format!(
"{:02X}{:02X}{:02X}{:02X}",
self.0[0], self.0[1], self.0[2], self.0[3]
))
}
}
impl<T: HexString + Clone> HexString for &T {
fn as_hex_string(&self) -> HexStringValue {
(*self).clone().as_hex_string()
}
}
impl UpperHex for HexStringValue {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0.to_uppercase())
}
}
impl LowerHex for HexStringValue {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0.to_lowercase())
}
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
pub fn normalize_path<T: AsRef<Path>>(path: T) -> PathBuf {
let path = path.as_ref();
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
PathBuf::from(c.as_os_str())
} else {
PathBuf::new()
};
for component in components {
match component {
Component::Prefix(..) => unreachable!(),
Component::RootDir => {
ret.push(component.as_os_str());
}
Component::CurDir => {}
Component::ParentDir => {
ret.pop();
}
Component::Normal(c) => {
ret.push(c);
}
}
}
ret
}