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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
//! Image manipulation effects in HSL, HSLuv, LCh and HSV.

use crate::iter::ImageIterator;
use crate::{helpers, PhotonImage, Rgb};
use image::GenericImageView;
use image::Pixel as ImagePixel;
use palette::{FromColor, IntoColor};
use palette::{Hsla, Hsluva, Hsva, Hue, Lcha, Saturate, Shade, Srgba};
use wasm_bindgen::prelude::*;

/// Applies gamma correction to an image.
/// # Arguments
/// * `photon_image` - A PhotonImage that contains a view into the image.
/// * `red` - Gamma value for red channel.
/// * `green` - Gamma value for green channel.
/// * `blue` - Gamma value for blue channel.
/// # Example
///
/// ```no_run
/// // For example, to turn an image of type `PhotonImage` into a gamma corrected image:
/// use photon_rs::colour_spaces::gamma_correction;
/// use photon_rs::native::open_image;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// gamma_correction(&mut img, 2.2, 2.2, 2.2);
/// ```
///
#[wasm_bindgen]
pub fn gamma_correction(
    photon_image: &mut PhotonImage,
    red: f32,
    green: f32,
    blue: f32,
) {
    let buf = photon_image.raw_pixels.as_mut_slice();
    let buf_size = buf.len();

    // Initialize gamma arrays
    let mut gamma_r: Vec<u8> = vec![0; 256];
    let mut gamma_g: Vec<u8> = vec![0; 256];
    let mut gamma_b: Vec<u8> = vec![0; 256];

    let inv_red = 1.0 / red;
    let inv_green = 1.0 / green;
    let inv_blue = 1.0 / blue;

    // Set values within gamma arrays
    for i in 0..256 {
        let input = (i as f32) / 255.0;
        gamma_r[i] = (255.0 * input.powf(inv_red) + 0.5).clamp(0.0, 255.0) as u8;
        gamma_g[i] = (255.0 * input.powf(inv_green) + 0.5).clamp(0.0, 255.0) as u8;
        gamma_b[i] = (255.0 * input.powf(inv_blue) + 0.5).clamp(0.0, 255.0) as u8;
    }

    // Apply gamma correction
    for i in (0..buf_size).step_by(4) {
        let r = buf[i];
        let g = buf[i + 1];
        let b = buf[i + 2];

        buf[i] = gamma_r[r as usize];
        buf[i + 1] = gamma_g[g as usize];
        buf[i + 2] = gamma_b[b as usize];
    }
}

/// Image manipulation effects in the HSLuv colour space
///
/// Effects include:
/// * **saturate** - Saturation increase.
/// * **desaturate** - Desaturate the image.
/// * **shift_hue** - Hue rotation by a specified number of degrees.
/// * **darken** - Decrease the brightness.
/// * **lighten** - Increase the brightness.
///
/// # Arguments
/// * `photon_image` - A PhotonImage.
/// * `mode` - The effect desired to be applied. Choose from: `saturate`, `desaturate`, `shift_hue`, `darken`, `lighten`
/// * `amt` - A float value from 0 to 1 which represents the amount the effect should be increased by.
/// # Example
/// ```no_run
/// // For example to increase the saturation by 10%:
/// use photon_rs::colour_spaces::hsluv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// hsluv(&mut img, "saturate", 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn hsluv(mut photon_image: &mut PhotonImage, mode: &str, amt: f32) {
    let img = helpers::dyn_image_from_raw(photon_image);
    let (width, height) = img.dimensions();
    let mut img = img.to_rgba8();

    for (x, y) in ImageIterator::new(width, height) {
        let px_data = img.get_pixel(x, y).channels();
        let hsluv_color: Hsluva = Srgba::new(
            px_data[0] as f32 / 255.0,
            px_data[1] as f32 / 255.0,
            px_data[2] as f32 / 255.0,
            px_data[3] as f32 / 255.0,
        )
        .into_linear()
        .into_color();

        let new_color = match mode {
            // Match a single value
            "desaturate" => hsluv_color.desaturate(amt),
            "saturate" => hsluv_color.saturate(amt),
            "lighten" => hsluv_color.lighten(amt),
            "darken" => hsluv_color.darken(amt),
            "shift_hue" => hsluv_color.shift_hue(amt * 360.0),
            _ => hsluv_color.saturate(amt),
        };
        let final_color: Srgba =
            Srgba::from_linear(new_color.into_color()).into_format();

        let components = final_color.into_components();

        img.put_pixel(
            x,
            y,
            image::Rgba([
                (components.0 * 255.0) as u8,
                (components.1 * 255.0) as u8,
                (components.2 * 255.0) as u8,
                (components.3 * 255.0) as u8,
            ]),
        );
    }
    photon_image.raw_pixels = img.to_vec();
}

/// Image manipulation effects in the LCh colour space
///
/// Effects include:
/// * **saturate** - Saturation increase.
/// * **desaturate** - Desaturate the image.
/// * **shift_hue** - Hue rotation by a specified number of degrees.
/// * **darken** - Decrease the brightness.
/// * **lighten** - Increase the brightness.
///
/// # Arguments
/// * `photon_image` - A PhotonImage.
/// * `mode` - The effect desired to be applied. Choose from: `saturate`, `desaturate`, `shift_hue`, `darken`, `lighten`
/// * `amt` - A float value from 0 to 1 which represents the amount the effect should be increased by.
/// # Example
/// ```no_run
/// // For example to increase the saturation by 10%:
/// use photon_rs::colour_spaces::lch;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// lch(&mut img, "saturate", 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn lch(mut photon_image: &mut PhotonImage, mode: &str, amt: f32) {
    let img = helpers::dyn_image_from_raw(photon_image);
    let (width, height) = img.dimensions();
    let mut img = img.to_rgba8();

    for (x, y) in ImageIterator::new(width, height) {
        let px_data = img.get_pixel(x, y).channels();
        let lch_colour: Lcha = Srgba::new(
            px_data[0] as f32 / 255.0,
            px_data[1] as f32 / 255.0,
            px_data[2] as f32 / 255.0,
            px_data[3] as f32 / 255.0,
        )
        .into_linear()
        .into_color();

        let new_color = match mode {
            // Match a single value
            "desaturate" => lch_colour.desaturate(amt),
            "saturate" => lch_colour.saturate(amt),
            "lighten" => lch_colour.lighten(amt),
            "darken" => lch_colour.darken(amt),
            "shift_hue" => lch_colour.shift_hue(amt * 360.0),
            _ => lch_colour.saturate(amt),
        };
        let final_color: Srgba =
            Srgba::from_linear(new_color.into_color()).into_format();

        let components = final_color.into_components();

        img.put_pixel(
            x,
            y,
            image::Rgba([
                (components.0 * 255.0) as u8,
                (components.1 * 255.0) as u8,
                (components.2 * 255.0) as u8,
                (components.3 * 255.0) as u8,
            ]),
        );
    }
    photon_image.raw_pixels = img.to_vec();
}

/// Image manipulation effects in the HSL colour space.
///
/// Effects include:
/// * **saturate** - Saturation increase.
/// * **desaturate** - Desaturate the image.
/// * **shift_hue** - Hue rotation by a specified number of degrees.
/// * **darken** - Decrease the brightness.
/// * **lighten** - Increase the brightness.
///
/// # Arguments
/// * `photon_image` - A PhotonImage.
/// * `mode` - The effect desired to be applied. Choose from: `saturate`, `desaturate`, `shift_hue`, `darken`, `lighten`
/// * `amt` - A float value from 0 to 1 which represents the amount the effect should be increased by.
/// # Example
/// ```no_run
/// // For example to increase the saturation by 10%:
/// use photon_rs::colour_spaces::hsl;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// hsl(&mut img, "saturate", 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn hsl(mut photon_image: &mut PhotonImage, mode: &str, amt: f32) {
    // The function logic is kept separate from other colour spaces for now,
    // since other HSL-specific logic may be implemented here, which isn't available in other colour spaces
    let mut img = helpers::dyn_image_from_raw(photon_image).to_rgba8();
    for (x, y) in ImageIterator::with_dimension(&img.dimensions()) {
        let px_data = img.get_pixel(x, y).channels();

        let colour = Srgba::new(
            px_data[0] as f32 / 255.0,
            px_data[1] as f32 / 255.0,
            px_data[2] as f32 / 255.0,
            px_data[3] as f32 / 255.0,
        );

        let hsl_colour = Hsla::from_color(colour);

        let new_color = match mode {
            // Match a single value
            "desaturate" => hsl_colour.desaturate(amt),
            "saturate" => hsl_colour.saturate(amt),
            "lighten" => hsl_colour.lighten(amt),
            "darken" => hsl_colour.darken(amt),
            "shift_hue" => hsl_colour.shift_hue(amt * 360.0),
            _ => hsl_colour.saturate(amt),
        };
        let final_color = Srgba::from_color(new_color);

        let components = final_color.into_components();

        img.put_pixel(
            x,
            y,
            image::Rgba([
                (components.0 * 255.0) as u8,
                (components.1 * 255.0) as u8,
                (components.2 * 255.0) as u8,
                (components.3 * 255.0) as u8,
            ]),
        );
    }

    photon_image.raw_pixels = img.to_vec();
}

/// Image manipulation in the HSV colour space.
///
/// Effects include:
/// * **saturate** - Saturation increase.
/// * **desaturate** - Desaturate the image.
/// * **shift_hue** - Hue rotation by a specified number of degrees.
/// * **darken** - Decrease the brightness.
/// * **lighten** - Increase the brightness.
///
/// # Arguments
/// * `photon_image` - A PhotonImage.
/// * `mode` - The effect desired to be applied. Choose from: `saturate`, `desaturate`, `shift_hue`, `darken`, `lighten`
/// * `amt` - A float value from 0 to 1 which represents the amount the effect should be increased by.
///
/// # Example
/// ```no_run
/// // For example to increase the saturation by 10%:
/// use photon_rs::colour_spaces::hsv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// hsv(&mut img, "saturate", 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn hsv(photon_image: &mut PhotonImage, mode: &str, amt: f32) {
    let img = helpers::dyn_image_from_raw(photon_image);
    let (width, height) = img.dimensions();
    let mut img = img.to_rgba8();

    for (x, y) in ImageIterator::new(width, height) {
        let px_data = img.get_pixel(x, y).channels();

        let color = Srgba::new(
            px_data[0] as f32 / 255.0,
            px_data[1] as f32 / 255.0,
            px_data[2] as f32 / 255.0,
            px_data[3] as f32 / 255.0,
        );

        let hsv_colour = Hsva::from_color(color);

        let new_color = match mode {
            // Match a single value
            "desaturate" => hsv_colour.desaturate(amt),
            "saturate" => hsv_colour.saturate(amt),
            "lighten" => hsv_colour.lighten(amt),
            "darken" => hsv_colour.darken(amt),
            "shift_hue" => hsv_colour.shift_hue(amt * 360.0),
            _ => hsv_colour.saturate(amt),
        };

        let srgba_new_color = Srgba::from_color(new_color);
        // let final_color: Srgba = Srgba::from_linear(srgba_new_color).into_format();

        let components = srgba_new_color.into_components();

        img.put_pixel(
            x,
            y,
            image::Rgba([
                (components.0 * 255.0) as u8,
                (components.1 * 255.0) as u8,
                (components.2 * 255.0) as u8,
                (components.3 * 255.0) as u8,
            ]),
        );
    }
    photon_image.raw_pixels = img.to_vec();
}

/// Shift hue by a specified number of degrees in the HSL colour space.
/// # Arguments
/// * `img` - A PhotonImage.
/// * `mode` - A float value from 0 to 1 which is the amount to shift the hue by, or hue rotate by.
///
/// # Example
/// ```no_run
/// // For example to hue rotate/shift the hue by 120 degrees in the HSL colour space:
/// use photon_rs::colour_spaces::hue_rotate_hsl;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// hue_rotate_hsl(&mut img, 120_f32);
/// ```
#[wasm_bindgen]
pub fn hue_rotate_hsl(img: &mut PhotonImage, degrees: f32) {
    hsl(img, "shift_hue", degrees);
}

/// Shift hue by a specified number of degrees in the HSV colour space.
/// # Arguments
/// * `img` - A PhotonImage.
/// * `mode` - A float value from 0 to 1 which is the amount to shift the hue by, or hue rotate by.
///
/// # Example
/// ```no_run
/// // For example to hue rotate/shift the hue by 120 degrees in the HSV colour space:
/// use photon_rs::colour_spaces::hue_rotate_hsv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// hue_rotate_hsv(&mut img, 120_f32);
/// ```
#[wasm_bindgen]
pub fn hue_rotate_hsv(img: &mut PhotonImage, degrees: f32) {
    hsv(img, "shift_hue", degrees);
}

/// Shift hue by a specified number of degrees in the LCh colour space.
/// # Arguments
/// * `img` - A PhotonImage.
/// * `mode` - A float value from 0 to 1 which is the amount to shift the hue by, or hue rotate by.
///
/// # Example
/// ```no_run
/// // For example to hue rotate/shift the hue by 120 degrees in the HSL colour space:
/// use photon_rs::colour_spaces::hue_rotate_lch;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// hue_rotate_lch(&mut img, 120_f32);
/// ```
#[wasm_bindgen]
pub fn hue_rotate_lch(img: &mut PhotonImage, degrees: f32) {
    lch(img, "shift_hue", degrees)
}

/// Shift hue by a specified number of degrees in the HSLuv colour space.
/// # Arguments
/// * `img` - A PhotonImage.
/// * `mode` - A float value from 0 to 1 which is the amount to shift the hue by, or hue rotate by.
///
/// # Example
/// ```no_run
/// // For example to hue rotate/shift the hue by 120 degrees in the HSL colour space:
/// use photon_rs::colour_spaces::hue_rotate_hsluv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// hue_rotate_hsluv(&mut img, 120_f32);
/// ```
#[wasm_bindgen]
pub fn hue_rotate_hsluv(img: &mut PhotonImage, degrees: f32) {
    hsluv(img, "shift_hue", degrees)
}

/// Increase the image's saturation by converting each pixel's colour to the HSL colour space
/// and increasing the colour's saturation.
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to increase the saturation by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Increasing saturation by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to increase saturation by 10% in the HSL colour space:
/// use photon_rs::colour_spaces::saturate_hsl;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// saturate_hsl(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn saturate_hsl(img: &mut PhotonImage, level: f32) {
    hsl(img, "saturate", level)
}

/// Increase the image's saturation in the LCh colour space.
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to increase the saturation by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Increasing saturation by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to increase saturation by 40% in the Lch colour space:
/// use photon_rs::colour_spaces::saturate_lch;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// saturate_lch(&mut img, 0.4_f32);
/// ```
#[wasm_bindgen]
pub fn saturate_lch(img: &mut PhotonImage, level: f32) {
    lch(img, "saturate", level)
}

/// Increase the image's saturation in the HSLuv colour space.
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to increase the saturation by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Increasing saturation by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to increase saturation by 40% in the HSLuv colour space:
/// use photon_rs::colour_spaces::saturate_hsluv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// saturate_hsluv(&mut img, 0.4_f32);
/// ```
#[wasm_bindgen]
pub fn saturate_hsluv(img: &mut PhotonImage, level: f32) {
    hsluv(img, "saturate", level)
}

/// Increase the image's saturation in the HSV colour space.
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level by which to increase the saturation by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Increasing saturation by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to increase saturation by 30% in the HSV colour space:
/// use photon_rs::colour_spaces::saturate_hsv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// saturate_hsv(&mut img, 0.3_f32);
/// ```
#[wasm_bindgen]
pub fn saturate_hsv(img: &mut PhotonImage, level: f32) {
    hsv(img, "saturate", level)
}

/// Lighten an image by a specified amount in the LCh colour space.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to lighten the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Lightening by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to lighten an image by 10% in the LCh colour space:
/// use photon_rs::colour_spaces::lighten_lch;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// lighten_lch(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn lighten_lch(img: &mut PhotonImage, level: f32) {
    lch(img, "lighten", level)
}

/// Lighten an image by a specified amount in the HSLuv colour space.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to lighten the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Lightening by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to lighten an image by 10% in the HSLuv colour space:
/// use photon_rs::colour_spaces::lighten_hsluv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// lighten_hsluv(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn lighten_hsluv(img: &mut PhotonImage, level: f32) {
    hsluv(img, "lighten", level)
}

/// Lighten an image by a specified amount in the HSL colour space.
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to lighten the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Lightening by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to lighten an image by 10% in the HSL colour space:
/// use photon_rs::colour_spaces::lighten_hsl;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// lighten_hsl(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn lighten_hsl(img: &mut PhotonImage, level: f32) {
    hsl(img, "lighten", level)
}

/// Lighten an image by a specified amount in the HSV colour space.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to lighten the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Lightening by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to lighten an image by 10% in the HSV colour space:
/// use photon_rs::colour_spaces::lighten_hsv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// lighten_hsv(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn lighten_hsv(img: &mut PhotonImage, level: f32) {
    hsv(img, "lighten", level)
}

/// Darken the image by a specified amount in the LCh colour space.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to darken the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Darkening by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to darken an image by 10% in the LCh colour space:
/// use photon_rs::colour_spaces::darken_lch;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// darken_lch(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn darken_lch(img: &mut PhotonImage, level: f32) {
    lch(img, "darken", level)
}

/// Darken the image by a specified amount in the HSLuv colour space.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to darken the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Darkening by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to darken an image by 10% in the HSLuv colour space:
/// use photon_rs::colour_spaces::darken_hsluv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// darken_hsluv(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn darken_hsluv(img: &mut PhotonImage, level: f32) {
    hsluv(img, "darken", level)
}

/// Darken the image by a specified amount in the HSL colour space.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to darken the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Darkening by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to darken an image by 10% in the HSL colour space:
/// use photon_rs::colour_spaces::darken_hsl;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// darken_hsl(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn darken_hsl(img: &mut PhotonImage, level: f32) {
    hsl(img, "darken", level)
}

/// Darken the image's colours by a specified amount in the HSV colour space.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to darken the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Darkening by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to darken an image by 10% in the HSV colour space:
/// use photon_rs::colour_spaces::darken_hsv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// darken_hsv(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn darken_hsv(img: &mut PhotonImage, level: f32) {
    hsv(img, "darken", level)
}

/// Desaturate the image by a specified amount in the HSV colour space.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to desaturate the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Desaturating by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to desaturate an image by 10% in the HSV colour space:
/// use photon_rs::colour_spaces::desaturate_hsv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// desaturate_hsv(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn desaturate_hsv(img: &mut PhotonImage, level: f32) {
    hsv(img, "desaturate", level)
}

/// Desaturate the image by a specified amount in the HSL colour space.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to desaturate the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Desaturating by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to desaturate an image by 10% in the LCh colour space:
/// use photon_rs::colour_spaces::desaturate_hsl;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// desaturate_hsl(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn desaturate_hsl(img: &mut PhotonImage, level: f32) {
    hsl(img, "desaturate", level)
}

/// Desaturate the image by a specified amount in the LCh colour space.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to desaturate the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Desaturating by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to desaturate an image by 10% in the LCh colour space:
/// use photon_rs::colour_spaces::desaturate_lch;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// desaturate_lch(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn desaturate_lch(img: &mut PhotonImage, level: f32) {
    lch(img, "desaturate", level)
}

/// Desaturate the image by a specified amount in the HSLuv colour space.
///
/// # Arguments
/// * `img` - A PhotonImage.
/// * `level` - Float value from 0 to 1 representing the level to which to desaturate the image by.
/// The `level` must be from 0 to 1 in floating-point, `f32` format.
/// Desaturating by 80% would be represented by a `level` of 0.8
///
/// # Example
/// ```no_run
/// // For example to desaturate an image by 10% in the HSLuv colour space:
/// use photon_rs::colour_spaces::desaturate_hsluv;
/// use photon_rs::native::open_image;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// desaturate_hsluv(&mut img, 0.1_f32);
/// ```
#[wasm_bindgen]
pub fn desaturate_hsluv(img: &mut PhotonImage, level: f32) {
    hsluv(img, "desaturate", level)
}

/// Mix image with a single color, supporting passing `opacity`.
/// The algorithm comes from Jimp. See `function mix` and `function colorFn` at following link:
/// https://github.com/oliver-moran/jimp/blob/29679faa597228ff2f20d34c5758e4d2257065a3/packages/plugin-color/src/index.js
/// Specifically, result_value = (mix_color_value - origin_value) * opacity + origin_value =
/// mix_color_value * opacity + (1 - opacity) * origin_value for each
/// of RGB channel.
///
/// # Arguments
/// * `photon_image` - A PhotonImage that contains a view into the image.
/// * `mix_color` - the color to be mixed in, as an RGB value.
/// * `opacity` - the opacity of color when mixed to image. Float value from 0 to 1.
/// # Example
///
/// ```no_run
/// // For example, to mix an image with rgb (50, 255, 254) and opacity 0.4:
/// use photon_rs::Rgb;
/// use photon_rs::colour_spaces::mix_with_colour;
/// use photon_rs::native::open_image;
///
/// let mix_colour = Rgb::new(50_u8, 255_u8, 254_u8);
/// let mut img = open_image("img.jpg").expect("File should open");
/// mix_with_colour(&mut img, mix_colour, 0.4_f32);
/// ```
#[wasm_bindgen]
pub fn mix_with_colour(photon_image: &mut PhotonImage, mix_colour: Rgb, opacity: f32) {
    let img = helpers::dyn_image_from_raw(photon_image);
    let (width, height) = img.dimensions();
    let mut img = img.to_rgba8();

    // cache (mix_color_value * opacity) and (1 - opacity) so we dont need to calculate them each time during loop.
    let mix_red_offset = mix_colour.r as f32 * opacity;
    let mix_green_offset = mix_colour.g as f32 * opacity;
    let mix_blue_offset = mix_colour.b as f32 * opacity;
    let factor = 1.0 - opacity;

    for (x, y) in ImageIterator::new(width, height) {
        let px = img.get_pixel(x, y);
        let channels = px.channels();

        let r_value = mix_red_offset + (channels[0] as f32) * factor;
        let g_value = mix_green_offset + (channels[1] as f32) * factor;
        let b_value = mix_blue_offset + (channels[2] as f32) * factor;
        let alpha = channels[3];
        img.put_pixel(
            x,
            y,
            image::Rgba([r_value as u8, g_value as u8, b_value as u8, alpha]),
        );
    }
    photon_image.raw_pixels = img.to_vec();
}