Upload spectral-core/src/color.rs
Browse files- spectral-core/src/color.rs +29 -0
spectral-core/src/color.rs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/// Packed RGBA8 color.
|
| 2 |
+
pub type Rgb8 = u32;
|
| 3 |
+
|
| 4 |
+
/// Named ANSI colors.
|
| 5 |
+
pub const ANSI_COLORS: [Rgb8; 16] = [
|
| 6 |
+
0xFF_000000, 0xFF_CD3131, 0xFF_0DBC79, 0xFF_E5E510,
|
| 7 |
+
0xFF_2472C8, 0xFF_BC3FBC, 0xFF_11A8CD, 0xFF_E5E5E5,
|
| 8 |
+
0xFF_666666, 0xFF_F14C4C, 0xFF_23D18B, 0xFF_F5F543,
|
| 9 |
+
0xFF_3B8EEA, 0xFF_D670D6, 0xFF_29B8DB, 0xFF_E5E5E5,
|
| 10 |
+
];
|
| 11 |
+
|
| 12 |
+
/// Convert HSL to RGB8.
|
| 13 |
+
pub fn hsl(h: f32, s: f32, l: f32) -> Rgb8 {
|
| 14 |
+
let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
|
| 15 |
+
let x = c * (1.0 - ((h / 60.0) % 2.0 - 1.0).abs());
|
| 16 |
+
let m = l - c / 2.0;
|
| 17 |
+
let (r1, g1, b1) = match h as i32 / 60 {
|
| 18 |
+
0 => (c, x, 0.0),
|
| 19 |
+
1 => (x, c, 0.0),
|
| 20 |
+
2 => (0.0, c, x),
|
| 21 |
+
3 => (0.0, x, c),
|
| 22 |
+
4 => (x, 0.0, c),
|
| 23 |
+
_ => (c, 0.0, x),
|
| 24 |
+
};
|
| 25 |
+
let r = ((r1 + m) * 255.0).clamp(0.0, 255.0) as u32;
|
| 26 |
+
let g = ((g1 + m) * 255.0).clamp(0.0, 255.0) as u32;
|
| 27 |
+
let b = ((b1 + m) * 255.0).clamp(0.0, 255.0) as u32;
|
| 28 |
+
0xFF_000000 | (r << 16) | (g << 8) | b
|
| 29 |
+
}
|