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
//! Preset color filters.

use crate::channels::alter_channels;
use crate::colour_spaces;
use crate::colour_spaces::mix_with_colour;
use crate::effects::{adjust_contrast, duotone, inc_brightness};
use crate::monochrome;
use crate::{PhotonImage, Rgb};
use wasm_bindgen::prelude::*;

/// Solarization on the Blue channel.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::neue;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// neue(&mut img);
/// ```
#[wasm_bindgen]
pub fn neue(photon_image: &mut PhotonImage) {
    let end = photon_image.get_raw_pixels().len();

    for i in (0..end).step_by(4) {
        let b_val = photon_image.raw_pixels[i + 2];
        if 255_i32 - b_val as i32 > 0 {
            photon_image.raw_pixels[i + 2] = 255 - b_val;
        }
    }
}

/// Solarization on the Red and Green channels.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::lix;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// lix(&mut img);
/// ```
#[wasm_bindgen]
pub fn lix(photon_image: &mut PhotonImage) {
    let end = photon_image.get_raw_pixels().len();

    for i in (0..end).step_by(4) {
        let r_val = photon_image.raw_pixels[i];
        let g_val = photon_image.raw_pixels[i + 1];

        photon_image.raw_pixels[i] = 255 - r_val;
        photon_image.raw_pixels[i + 1] = 255 - g_val;
    }
}

/// Solarization on the Red and Blue channels.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::ryo;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// ryo(&mut img);
/// ```
#[wasm_bindgen]
pub fn ryo(photon_image: &mut PhotonImage) {
    let end = photon_image.get_raw_pixels().len();

    for i in (0..end).step_by(4) {
        let r_val = photon_image.raw_pixels[i];
        let b_val = photon_image.raw_pixels[i + 2];

        photon_image.raw_pixels[i] = 255 - r_val;
        photon_image.raw_pixels[i + 2] = 255 - b_val;
    }
}

/// Apply a filter to an image. Over 20 filters are available.
/// The filters are as follows:
/// * **oceanic**: Add an aquamarine-tinted hue to an image.
/// * **islands**: Aquamarine tint.
/// * **marine**: Add a green/blue mixed hue to an image.
/// * **seagreen**: Dark green hue, with tones of blue.
/// * **flagblue**: Royal blue tint
/// * **liquid**: Blue-inspired tint.
/// * **diamante**: Custom filter with a blue/turquoise tint.
/// * **radio**: Fallout-style radio effect.
/// * **twenties**: Slight-blue tinted historical effect.
/// * **rosetint**: Rose-tinted filter.
/// * **mauve**: Purple-infused filter.
/// * **bluechrome**: Blue monochrome effect.
/// * **vintage**: Vintage filter with a red tint.
/// * **perfume**: Increase the blue channel, with moderate increases in the Red and Green channels.
/// * **serenity**: Custom filter with an increase in the Blue channel's values.
/// # Arguments
/// * `img` - A PhotonImage.
/// * `filter_name` - The filter's name. Choose from the selection above, eg: "oceanic"
/// # Example
///
/// ```no_run
/// // For example, to add a filter called "vintage" to an image:
/// use photon_rs::filters::filter;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// filter(&mut img, "vintage");
/// ```
#[wasm_bindgen]
pub fn filter(img: &mut PhotonImage, filter_name: &str) {
    let oceanic_rgb = Rgb::new(0, 89, 173);
    let islands_rgb = Rgb::new(0, 24, 95);
    let marine_rgb = Rgb::new(0, 14, 119);
    let seagreen_rgb = Rgb::new(0, 68, 62);
    let flagblue_rgb = Rgb::new(0, 0, 131);
    let diamante_rgb = Rgb::new(30, 82, 87);
    let liquid_rgb = Rgb::new(0, 10, 75);
    let vintage_rgb = Rgb::new(120, 70, 13);
    let perfume_rgb = Rgb::new(80, 40, 120);
    let serenity_rgb = Rgb::new(10, 40, 90);

    match filter_name {
        // Match filter name to its corresponding function.
        "oceanic" => mix_with_colour(img, oceanic_rgb, 0.2),
        "islands" => mix_with_colour(img, islands_rgb, 0.2),
        "marine" => mix_with_colour(img, marine_rgb, 0.2),
        "seagreen" => mix_with_colour(img, seagreen_rgb, 0.2),
        "flagblue" => mix_with_colour(img, flagblue_rgb, 0.2),
        "diamante" => mix_with_colour(img, diamante_rgb, 0.1),
        "liquid" => mix_with_colour(img, liquid_rgb, 0.2),
        "radio" => monochrome::monochrome(img, 5, 40, 20),
        "twenties" => monochrome::monochrome(img, 18, 12, 20),
        "rosetint" => monochrome::monochrome(img, 80, 20, 31),
        "mauve" => monochrome::monochrome(img, 90, 40, 80),
        "bluechrome" => monochrome::monochrome(img, 20, 30, 60),
        "vintage" => mix_with_colour(img, vintage_rgb, 0.2),
        "perfume" => mix_with_colour(img, perfume_rgb, 0.2),
        "serenity" => mix_with_colour(img, serenity_rgb, 0.2),
        "golden" => golden(img),
        "pastel_pink" => pastel_pink(img),
        "cali" => cali(img),
        "dramatic" => dramatic(img),
        "firenze" => firenze(img),
        "obsidian" => obsidian(img),
        "lofi" => lofi(img),
        _ => monochrome::monochrome(img, 90, 40, 80),
    };
}

/// Apply a lofi effect to an image.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::lofi;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// lofi(&mut img);
/// ```
#[wasm_bindgen]
pub fn lofi(img: &mut PhotonImage) {
    adjust_contrast(img, 30.0);
    colour_spaces::saturate_hsl(img, 0.2);
}

/// Apply a rose tint to an image.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::pastel_pink;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// pastel_pink(&mut img);
/// ```
#[wasm_bindgen]
pub fn pastel_pink(img: &mut PhotonImage) {
    alter_channels(img, 80, 12, 20);
    adjust_contrast(img, 30.0);
}

/// Apply a vintage, golden hue to an image.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::golden;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// golden(&mut img);
/// ```
#[wasm_bindgen]
pub fn golden(img: &mut PhotonImage) {
    let vignette_rgb = Rgb::new(235, 145, 50);
    mix_with_colour(img, vignette_rgb, 0.2);
    adjust_contrast(img, 30.0);
}

/// Increased contrast filter effect.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::cali;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// cali(&mut img);
/// ```
#[wasm_bindgen]
pub fn cali(img: &mut PhotonImage) {
    let cali_rgb = Rgb::new(255, 45, 75);
    colour_spaces::mix_with_colour(img, cali_rgb, 0.1);
    adjust_contrast(img, 50.0);
}

/// Greyscale effect with increased contrast.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::dramatic;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// dramatic(&mut img);
/// ```
#[wasm_bindgen]
pub fn dramatic(img: &mut PhotonImage) {
    monochrome::grayscale(img);
    adjust_contrast(img, 60.0);
}

/// Monochrome tint effect with increased contrast
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `rgb_color` - RGB color
/// # Example
///
/// ```no_run
/// use photon_rs::filters::monochrome_tint;
/// use photon_rs::native::open_image;
/// use photon_rs::Rgb;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// let rgb_color = Rgb::new(12, 12, 10);
/// monochrome_tint(&mut img, rgb_color);
/// ```
#[wasm_bindgen]
pub fn monochrome_tint(img: &mut PhotonImage, rgb_color: Rgb) {
    monochrome::grayscale(img);
    mix_with_colour(img, rgb_color, 0.4);
    adjust_contrast(img, 60.0);
}

/// Duotone effect with blue and purple tones.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::duotone_violette;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// duotone_violette(&mut img);
/// ```
#[wasm_bindgen]
pub fn duotone_violette(img: &mut PhotonImage) {
    let rgb_color = Rgb::new(16, 228, 248);
    let rgb_color2 = Rgb::new(116, 54, 221);
    duotone(img, rgb_color, rgb_color2);
}

/// Duotone effect with purple tones.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::duotone_horizon;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// duotone_horizon(&mut img);
/// ```
#[wasm_bindgen]
pub fn duotone_horizon(img: &mut PhotonImage) {
    let rgb_color = Rgb::new(169, 167, 132);
    let rgb_color2 = Rgb::new(150, 24, 149);
    duotone(img, rgb_color, rgb_color2);
}

/// A duotone filter with a user-specified color and a gray color
// to create a monochrome tint effect
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `rgb_color` - RGB color
/// # Example
///
/// ```no_run
/// use photon_rs::filters::duotone_tint;
/// use photon_rs::native::open_image;
/// use photon_rs::Rgb;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// let rgb_color = Rgb::new(12, 12, 10);
/// duotone_tint(&mut img, rgb_color);
/// ```
#[wasm_bindgen]
pub fn duotone_tint(img: &mut PhotonImage, rgb_color: Rgb) {
    let rgb_color2 = Rgb::new(68, 61, 76);
    duotone(img, rgb_color, rgb_color2);
}

/// Duotone effect with a lilac hue
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::duotone_lilac;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// duotone_lilac(&mut img);
/// ```
#[wasm_bindgen]
pub fn duotone_lilac(img: &mut PhotonImage) {
    let rgb_color = Rgb::new(45, 3, 3);
    let rgb_color2 = Rgb::new(163, 134, 224);
    duotone(img, rgb_color, rgb_color2);
}

/// A duotone ochre tint effect
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::duotone_ochre;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// duotone_ochre(&mut img);
/// ```
#[wasm_bindgen]
pub fn duotone_ochre(img: &mut PhotonImage) {
    let rgb_color = Rgb::new(25, 36, 88);
    let rgb_color2 = Rgb::new(236, 119, 0);
    duotone(img, rgb_color, rgb_color2);
}

/// Apply a red hue, with increased contrast and brightness.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::firenze;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// firenze(&mut img);
/// ```
#[wasm_bindgen]
pub fn firenze(img: &mut PhotonImage) {
    let cali_rgb = Rgb::new(255, 47, 78);
    colour_spaces::mix_with_colour(img, cali_rgb, 0.1);

    inc_brightness(img, 30);
    adjust_contrast(img, 50.0);
}

/// Apply a greyscale effect with increased contrast.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// # Example
///
/// ```no_run
/// use photon_rs::filters::obsidian;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// obsidian(&mut img);
/// ```
#[wasm_bindgen]
pub fn obsidian(img: &mut PhotonImage) {
    monochrome::grayscale(img);
    adjust_contrast(img, 25.0);
}