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
//! Legacy comma-form `rgb()` / `rgba()` serializer.
//!
//! Mirrors culori 4.0.2's `serializeRgb`
//! (`node_modules/culori/src/formatter.js`). The input is first converted to
//! sRGB, then channels are clamped to 0..=255 (NaN → 0) and the output uses
//! the legacy comma-separated form:
//!
//! ```text
//! rgb(R, G, B) // alpha is undefined or exactly 1
//! rgba(R, G, B, A) // alpha < 1; A rounded to 2 decimals
//! ```
//!
//! Alpha is clamped to [0, 1] and rounded to two decimal places via the
//! same `Math.round(a*100)/100` rule culori uses.
use crateColor;
use crate;
/// Round a finite value to two decimals, matching culori's
/// `round(2)`: `Math.round(v*100)/100`. Trailing zeros are dropped on
/// stringification (Rust's default f64 format matches JS for the relevant
/// magnitudes).
/// Serialize a color as legacy `rgb(R, G, B)` or `rgba(R, G, B, A)`.
///
/// Channels are integers in 0..=255, clamped with rounding (NaN → 0). Alpha
/// is rendered with up to two decimals; if alpha is absent or exactly 1, the
/// `rgb(...)` form is used.
///
/// ```rust
/// use culors::{format_rgb, parse};
/// let c = parse("#f0f0f0f0").unwrap();
/// assert_eq!(format_rgb(&c), "rgba(240, 240, 240, 0.94)");
/// ```