artem 3.0.0

Convert images from multiple formats (jpg, png, webp, etc…) to ASCII art
Documentation
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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
275
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use image::Rgba;

use crate::{
    config::{self, Config},
    target,
};

/// Convert a pixel block to a char (as a String) from the given density string.
///
/// # Panics
///
/// Panics if either the given pixel block or the density is empty.
///
/// # Examples
///
/// ```compile_fail, compile will fail, this is an internal example
/// use image::Rgba;
/// use artem::config::TargetType;
///
/// //example pixels, use them from the directly if possible
/// let pixels = vec![
///     Rgba::<u8>::from([255, 255, 255, 255]),
///     Rgba::<u8>::from([0, 0, 0, 255]),
/// ];
///
/// assert_eq!(".", correlating_char(&pixels, "#k. ", false, TargetType::default()));
/// ```
///
/// To use color, use the `color` argument, if only the background should be colored, use the `on_background_color` arg instead.
///
/// The `invert` arg, inverts the mapping from pixel luminosity to density string.
pub fn correlating_char(block: &[Rgba<u8>], config: &Config) -> String {
    assert!(!block.is_empty());
    assert!(!config.characters.is_empty());

    let (red, green, blue) = average_color(block);

    //calculate luminosity from avg. pixel color
    let luminosity = luminosity(red, green, blue);

    //use chars length to support unicode chars
    let length = config.characters.chars().count();

    //swap to range for white to black values
    //convert from rgb values (0 - 255) to the density string index (0 - string length)
    let density_index = map_range(
        (0f32, 255f32),
        if config.invert {
            (0f32, length as f32)
        } else {
            (length as f32, 0f32)
        },
        luminosity,
    )
    .floor()
    .clamp(0f32, length as f32 - 1.0);

    //get correct char from map
    assert!((density_index as usize) < length);
    let density_char = config
        .characters
        .chars()
        .nth(density_index as usize)
        .expect("Failed to get char");

    //return the correctly formatted/colored string depending on the target
    match config.target {
        //if no color, use default case
        config::TargetType::Shell | config::TargetType::AnsiFile if config.color() => {
            target::ansi::colored_char(red, green, blue, density_char, config.background_color())
        }
        config::TargetType::HtmlFile => {
            if config.color() {
                target::html::colored_char(
                    red,
                    green,
                    blue,
                    density_char,
                    config.background_color(),
                )
            } else {
                density_char.to_string()
            }
        }
        //all other case, including a plain text file and shell without colors
        _ => density_char.to_string(),
    }
}

#[cfg(test)]
mod test_pixel_density {
    use std::env;

    use crate::ConfigBuilder;

    use super::*;

    #[test]
    fn invert_returns_first_instead_of_last_char() {
        let pixels = vec![
            Rgba::<u8>::from([255, 255, 255, 255]),
            Rgba::<u8>::from([255, 255, 255, 255]),
            Rgba::<u8>::from([0, 0, 0, 255]),
        ];
        let config = ConfigBuilder::new()
            .characters("# ".to_owned())
            .invert(true)
            .color(false)
            .build();
        assert_eq!(" ", correlating_char(&pixels, &config));
    }

    #[test]
    fn medium_density_char() {
        let pixels = vec![
            Rgba::<u8>::from([255, 255, 255, 255]),
            Rgba::<u8>::from([0, 0, 0, 255]),
        ];
        let config = ConfigBuilder::new()
            .characters("#k. ".to_owned())
            .color(false)
            .build();
        assert_eq!("k", correlating_char(&pixels, &config));
    }

    #[test]
    fn dark_density_char() {
        let pixels = vec![
            Rgba::<u8>::from([255, 255, 255, 255]),
            Rgba::<u8>::from([255, 255, 255, 255]),
            Rgba::<u8>::from([0, 0, 0, 255]),
        ];
        let config = ConfigBuilder::new()
            .characters("#k. ".to_owned())
            .color(false)
            .build();
        assert_eq!("#", correlating_char(&pixels, &config));
    }

    #[test]
    #[ignore = "Requires truecolor support"]
    fn colored_char() {
        //set needed env vars
        env::set_var("COLORTERM", "truecolor");
        //force color, this is not printed to the terminal anyways
        env::set_var("CLICOLOR_FORCE", "1");

        let pixels = vec![Rgba::<u8>::from([0, 0, 255, 255])];
        let config = ConfigBuilder::new().characters("#k. ".to_owned()).build();
        assert_eq!(
            "\u{1b}[38;2;0;0;255m \u{1b}[0m", //blue color
            correlating_char(&pixels, &config)
        );
    }

    #[test]
    fn ansi_colored_char_shell() {
        //set no color support
        env::set_var("COLORTERM", "false");
        //force color, this is not printed to the terminal anyways
        env::set_var("CLICOLOR_FORCE", "1");
        //just some random color
        let pixels = vec![Rgba::<u8>::from([123, 42, 244, 255])];
        let config = ConfigBuilder::new().characters("#k. ".to_owned()).build();
        assert_eq!("\u{1b}[35m.\u{1b}[0m", correlating_char(&pixels, &config));
    }

    #[test]
    fn ansi_colored_char_ansi() {
        //set no color support
        env::set_var("COLORTERM", "false");
        //force color, this is not printed to the terminal anyways
        env::set_var("CLICOLOR_FORCE", "1");
        let pixels = vec![Rgba::<u8>::from([123, 42, 244, 255])];
        let config = ConfigBuilder::new()
            .characters("#k. ".to_owned())
            .target(config::TargetType::AnsiFile)
            .build();
        assert_eq!("\u{1b}[35m.\u{1b}[0m", correlating_char(&pixels, &config));
    }

    #[test]
    #[ignore = "Requires truecolor support"]
    fn colored_background_char_shell() {
        //set needed env vars
        env::set_var("COLORTERM", "truecolor");
        //force color, this is not printed to the terminal anyways
        env::set_var("CLICOLOR_FORCE", "1");

        let pixels = vec![Rgba::<u8>::from([0, 0, 255, 255])];
        let config = ConfigBuilder::new()
            .characters("#k. ".to_owned())
            .background_color(true)
            .build();
        assert_eq!(
            "\u{1b}[48;2;0;0;255m \u{1b}[0m",
            correlating_char(&pixels, &config)
        );
    }

    #[test]
    #[ignore = "Requires truecolor support"]
    fn colored_background_char_ansi() {
        //set needed env vars
        env::set_var("COLORTERM", "truecolor");
        //force color, this is not printed to the terminal anyways
        env::set_var("CLICOLOR_FORCE", "1");
        let pixels = vec![Rgba::<u8>::from([0, 0, 255, 255])];
        let config = ConfigBuilder::new()
            .characters("#k. ".to_owned())
            .target(config::TargetType::AnsiFile)
            .background_color(true)
            .build();
        assert_eq!(
            "\u{1b}[48;2;0;0;255m \u{1b}[0m",
            correlating_char(&pixels, &config)
        );
    }

    #[test]
    fn target_file_returns_non_colored_string() {
        //force color, this is not printed to the terminal anyways
        env::set_var("COLORTERM", "truecolor");
        env::set_var("CLICOLOR_FORCE", "1");

        let pixels = vec![Rgba::<u8>::from([0, 0, 255, 255])];
        let config = ConfigBuilder::new()
            .characters("#k. ".to_owned())
            .target(config::TargetType::File)
            .build();
        assert_eq!(" ", correlating_char(&pixels, &config));
    }

    #[test]
    fn white_has_no_tag() {
        //force color, this is not printed to the terminal anyways
        env::set_var("COLORTERM", "truecolor");
        env::set_var("CLICOLOR_FORCE", "1");

        let pixels = vec![Rgba::<u8>::from([0, 0, 255, 255])];
        let config = ConfigBuilder::new()
            .characters("#k. ".to_owned())
            .target(config::TargetType::HtmlFile)
            .build();
        assert_eq!(" ", correlating_char(&pixels, &config));
    }

    #[test]
    fn target_html_colored_string() {
        //force color, this is not printed to the terminal anyways
        env::set_var("COLORTERM", "truecolor");
        env::set_var("CLICOLOR_FORCE", "1");

        let pixels = vec![Rgba::<u8>::from([0, 0, 255, 255])];
        let config = ConfigBuilder::new()
            .characters("#k:.".to_owned())
            .target(config::TargetType::HtmlFile)
            .color(true)
            .build();
        assert_eq!(
            "<span style=\"color: #0000FF\">.</span>",
            correlating_char(&pixels, &config)
        );
    }

    #[test]
    fn target_html_background_string() {
        //force color, this is not printed to the terminal anyways
        env::set_var("COLORTERM", "truecolor");
        env::set_var("CLICOLOR_FORCE", "1");

        let pixels = vec![Rgba::<u8>::from([0, 0, 255, 255])];
        let config = ConfigBuilder::new()
            .characters("#k:. ".to_owned())
            .target(config::TargetType::HtmlFile)
            .background_color(true)
            .build();
        assert_eq!(
            "<span style=\"background-color: #0000FF\"> </span>",
            correlating_char(&pixels, &config)
        );
    }

    #[test]
    fn target_html_no_color() {
        //force color, this is not printed to the terminal anyways
        env::set_var("COLORTERM", "truecolor");
        env::set_var("CLICOLOR_FORCE", "1");

        let pixels = vec![Rgba::<u8>::from([0, 0, 255, 255])];
        let config = ConfigBuilder::new()
            .characters("#k. ".to_owned())
            .target(config::TargetType::HtmlFile)
            .color(false)
            .build();
        assert_eq!(" ", correlating_char(&pixels, &config));
    }
}

///Remap a value from one range to another.
///
/// If the value is outside of the specified range, it will still be
/// converted as if it was in the range. This means it could be much larger or smaller than expected.
/// This can be fixed by using the `clamp` function after the remapping.
fn map_range(from_range: (f32, f32), to_range: (f32, f32), value: f32) -> f32 {
    to_range.0 + (value - from_range.0) * (to_range.1 - to_range.0) / (from_range.1 - from_range.0)
}

#[cfg(test)]
mod test_map_range {
    use super::*;

    #[test]
    fn remap_values() {
        //remap 2 to 4
        assert_eq!(4f32, map_range((0f32, 10f32), (0f32, 20f32), 2f32));
    }

    #[test]
    fn remap_values_above_range() {
        //remap 21 to 42, since the value will be doubled
        assert_eq!(42f32, map_range((0f32, 10f32), (0f32, 20f32), 21f32));
    }

    #[test]
    fn remap_values_below_range() {
        //remap -1 to -2, since the value will be doubled
        assert_eq!(-2f32, map_range((0f32, 10f32), (0f32, 20f32), -1f32));
    }
}

/// Returns the average rbg color of multiple pixel.
///
/// If the input block is empty, all pixels are seen and calculated as if there were black.
///
/// # Examples
///
/// ```compile_fail, compile will fail, this is an internal example
/// let pixels: Vec<Rgba<u8>> = Vec::new();
/// assert_eq!((0, 0, 0, 0.0), get_pixel_color_luminosity(&pixels));
/// ```
///
/// The formula for calculating the rbg colors is based an a minutephysics video <https://www.youtube.com/watch?v=LKnqECcg6Gw>
fn average_color(block: &[Rgba<u8>]) -> (u8, u8, u8) {
    let sum = block
        .iter()
        .map(|pixel| {
            (
                pixel.0[0] as f32 * pixel.0[0] as f32,
                pixel.0[1] as f32 * pixel.0[1] as f32,
                pixel.0[2] as f32 * pixel.0[2] as f32,
            )
        })
        .fold((0f32, 0f32, 0f32), |acc, value| {
            (acc.0 + value.0, acc.1 + value.1, acc.2 + value.2)
        });
    (
        (sum.0 / block.len() as f32).sqrt() as u8,
        (sum.1 / block.len() as f32).sqrt() as u8,
        (sum.2 / block.len() as f32).sqrt() as u8,
    )
}

#[cfg(test)]
mod test_avg_color {
    use super::*;

    #[test]
    fn red_green() {
        let pixels = vec![
            Rgba::<u8>::from([255, 0, 0, 255]),
            Rgba::<u8>::from([0, 255, 0, 255]),
        ];

        assert_eq!((180, 180, 0), average_color(&pixels));
    }

    #[test]
    fn green_blue() {
        let pixels = vec![
            Rgba::<u8>::from([0, 255, 0, 255]),
            Rgba::<u8>::from([0, 0, 255, 255]),
        ];

        assert_eq!((0, 180, 180), average_color(&pixels));
    }

    #[test]
    fn empty_input() {
        let pixels: Vec<Rgba<u8>> = Vec::new();
        let (r, g, b) = average_color(&pixels);
        assert_eq!(0, r);
        assert_eq!(0, g);
        assert_eq!(0, b);
    }
}

/// Returns the luminosity of the given rgb colors as an float.
///
/// It converts the rgb values to floats, adds them with weightings and then returns them
/// as a float value.
///
/// # Examples
///
/// ```compile_fail, compile will fail, this is an internal example
/// use artem::pixel;
///
/// let luminosity = luminosity(154, 85, 54);
/// assert_eq!(97f32, luminosity);
/// ```
///
/// The formula/weighting for the colors comes from <http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/>
pub fn luminosity(red: u8, green: u8, blue: u8) -> f32 {
    (0.21 * red as f32) + (0.72 * green as f32) + (0.07 * blue as f32)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn luminosity_black_is_zero() {
        assert_eq!(0f32, luminosity(0, 0, 0))
    }

    #[test]
    fn luminosity_white_is_255() {
        assert_eq!(255.00002, luminosity(255, 255, 255))
    }

    #[test]
    fn luminosity_rust_color_is_255() {
        assert_eq!(97.32f32, luminosity(154, 85, 54))
    }
}