File: tst.rs

package info (click to toggle)
rust-colorsys 0.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 316 kB
  • sloc: makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,194 bytes parent folder | download
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
#[allow(unused_imports)]
use colorsys::{prelude::*, Hsl, Rgb};

#[test]
fn for_docs() {
  use colorsys::{Rgb, Hsl};

  let rbga_tuple = (57.3, 12.7, 53.0, 0.33);
  let rgba = Rgb::from(&rbga_tuple);
  let hsla: Hsl = rgba.as_ref().into();
// ~Hsl { h: 305.78, s: 63.71, l: 13.73, a: 0.33 }

  let rgb_arr: [u8; 3] = Rgb::from(&hsla).into();
// ~[57, 13, 53]

  let hsla_tuple: (f64,f64,f64,f64) = Hsl::from( Rgb::from(rgb_arr) ).into();
// ~Hsl { h: 305.78, s: 63.71, l: 13.73, a: 1 }

  let hex: String = rgba.to_hex_string();
// #390d35


// From/Into

  let rgb1 = Rgb::from_hex_str("37ea4c").unwrap();

  let rgb2 = Rgb::from(
    Into::<[f32; 4]>::into(Rgb::from(
      Into::<[u16; 3]>::into(
        Rgb::from(
          Into::<(i32,i32,i32)>::into(
            Rgb::from(
              Into::<[i64; 3]>::into(&rgb1)
            )
          )
        )
      )
    ))
  );

  assert_eq!(rgb1, rgb2);
  //
// Ratio
//
  use colorsys::{RgbRatio, ApproxEq};
  let blue = Rgb::from([34, 111, 235]);

  let ratio: [f32; 4] = blue.as_ratio().into();
// ~[0.133, 0.435, 0.922, 1.0]

  let converted: Rgb = RgbRatio::from(&ratio).into();
  assert!(blue.approx_eq_clarify(&converted, 0.0001));
}