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
use std::fmt::{Debug, Formatter};
#[derive(Clone)]
pub enum Color {
Rgba32(u32),
}
impl Debug for Color {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "#{:08x}", self.as_rgba_u32())
}
}
impl Color {
pub const fn rgb8(r: u8, g: u8, b: u8) -> Color {
Color::from_rgba32_u32(((r as u32) << 24) | ((g as u32) << 16) | ((b as u32) << 8) | 0xff)
}
pub const fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Color {
Color::from_rgba32_u32(
((r as u32) << 24) | ((g as u32) << 16) | ((b as u32) << 8) | (a as u32),
)
}
pub const fn from_rgba32_u32(rgba: u32) -> Color {
Color::Rgba32(rgba)
}
pub const fn grey8(grey: u8) -> Color {
Color::rgb8(grey, grey, grey)
}
pub fn grey(grey: impl Into<f64>) -> Color {
let grey = grey.into();
Color::rgb(grey, grey, grey)
}
pub fn rgba<F: Into<f64>>(r: F, g: F, b: F, a: F) -> Color {
let r = (r.into().max(0.0).min(1.0) * 255.0).round() as u32;
let g = (g.into().max(0.0).min(1.0) * 255.0).round() as u32;
let b = (b.into().max(0.0).min(1.0) * 255.0).round() as u32;
let a = (a.into().max(0.0).min(1.0) * 255.0).round() as u32;
Color::from_rgba32_u32((r << 24) | (g << 16) | (b << 8) | a)
}
pub fn rgb<F: Into<f64>>(r: F, g: F, b: F) -> Color {
let r = (r.into().max(0.0).min(1.0) * 255.0).round() as u32;
let g = (g.into().max(0.0).min(1.0) * 255.0).round() as u32;
let b = (b.into().max(0.0).min(1.0) * 255.0).round() as u32;
Color::from_rgba32_u32((r << 24) | (g << 16) | (b << 8) | 0xff)
}
#[allow(non_snake_case)]
#[allow(clippy::many_single_char_names)]
#[allow(clippy::unreadable_literal)]
pub fn hlc<F: Into<f64>>(h: F, l: F, c: F) -> Color {
fn f_inv(t: f64) -> f64 {
let d = 6. / 29.;
if t > d {
t.powi(3)
} else {
3. * d * d * (t - 4. / 29.)
}
}
let th = h.into() * (std::f64::consts::PI / 180.);
let c = c.into();
let a = c * th.cos();
let b = c * th.sin();
let L = l.into();
let ll = (L + 16.) * (1. / 116.);
let X = f_inv(ll + a * (1. / 500.));
let Y = f_inv(ll);
let Z = f_inv(ll - b * (1. / 200.));
let r_lin = 3.02172918 * X - 1.61692294 * Y - 0.40480625 * Z;
let g_lin = -0.94339358 * X + 1.91584267 * Y + 0.02755094 * Z;
let b_lin = 0.06945666 * X - 0.22903204 * Y + 1.15957526 * Z;
fn gamma(u: f64) -> f64 {
if u <= 0.0031308 {
12.92 * u
} else {
1.055 * u.powf(1. / 2.4) - 0.055
}
}
Color::rgb(gamma(r_lin), gamma(g_lin), gamma(b_lin))
}
pub fn hlca<F: Into<f64>>(h: F, l: F, c: F, a: impl Into<f64>) -> Color {
Color::hlc(h, c, l).with_alpha(a)
}
pub fn with_alpha(self, a: impl Into<f64>) -> Color {
let a = (a.into().max(0.0).min(1.0) * 255.0).round() as u32;
Color::from_rgba32_u32((self.as_rgba_u32() & !0xff) | a)
}
pub fn as_rgba_u32(&self) -> u32 {
match *self {
Color::Rgba32(rgba) => rgba,
}
}
#[doc(hidden)]
#[deprecated(since = "0.0.13", note = "renamed to Color::as_rgba8")]
pub fn as_rgba_u8(&self) -> (u8, u8, u8, u8) {
self.as_rgba8()
}
pub fn as_rgba8(&self) -> (u8, u8, u8, u8) {
let rgba = self.as_rgba_u32();
(
(rgba >> 24 & 255) as u8,
((rgba >> 16) & 255) as u8,
((rgba >> 8) & 255) as u8,
(rgba & 255) as u8,
)
}
pub fn as_rgba(&self) -> (f64, f64, f64, f64) {
let rgba = self.as_rgba_u32();
(
(rgba >> 24) as f64 / 255.0,
((rgba >> 16) & 255) as f64 / 255.0,
((rgba >> 8) & 255) as f64 / 255.0,
(rgba & 255) as f64 / 255.0,
)
}
pub const WHITE: Color = Color::rgb8(0xff, 0xff, 0xff);
pub const BLACK: Color = Color::rgb8(0, 0, 0);
}