Repository navigation
Expand file tree
/
Copy pathcolor.rs
More file actions
194 lines (173 loc) · 6.05 KB
/
Copy pathcolor.rs
File metadata and controls
194 lines (173 loc) · 6.05 KB
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
use bevy::color::{Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Srgba, Xyza};
use bevy::prelude::Component;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ColorSpace {
Srgb = 0,
Linear = 1,
Hsl = 2,
Hsv = 3,
Hwb = 4,
Oklab = 5,
Oklch = 6,
Lab = 7,
Lch = 8,
Xyz = 9,
}
impl ColorSpace {
pub fn from_u8(v: u8) -> Option<Self> {
match v {
0 => Some(Self::Srgb),
1 => Some(Self::Linear),
2 => Some(Self::Hsl),
3 => Some(Self::Hsv),
4 => Some(Self::Hwb),
5 => Some(Self::Oklab),
6 => Some(Self::Oklch),
7 => Some(Self::Lab),
8 => Some(Self::Lch),
9 => Some(Self::Xyz),
_ => None,
}
}
pub fn parse(s: &str) -> Option<Self> {
use processing_core::constants as consts;
match () {
_ if s.eq_ignore_ascii_case(consts::SRGB) => Some(Self::Srgb),
_ if s.eq_ignore_ascii_case(consts::LINEAR) => Some(Self::Linear),
_ if s.eq_ignore_ascii_case(consts::HSL) => Some(Self::Hsl),
_ if s.eq_ignore_ascii_case(consts::HSV) => Some(Self::Hsv),
_ if s.eq_ignore_ascii_case(consts::HWB) => Some(Self::Hwb),
_ if s.eq_ignore_ascii_case(consts::OKLAB) => Some(Self::Oklab),
_ if s.eq_ignore_ascii_case(consts::OKLCH) => Some(Self::Oklch),
_ if s.eq_ignore_ascii_case(consts::LAB) => Some(Self::Lab),
_ if s.eq_ignore_ascii_case(consts::LCH) => Some(Self::Lch),
_ if s.eq_ignore_ascii_case(consts::XYZ) => Some(Self::Xyz),
_ => None,
}
}
pub fn default_maxes(&self) -> [f32; 4] {
match self {
Self::Srgb | Self::Linear | Self::Oklab | Self::Xyz => [1.0, 1.0, 1.0, 1.0],
Self::Hsl | Self::Hsv | Self::Hwb => [360.0, 1.0, 1.0, 1.0],
Self::Oklch => [1.0, 1.0, 360.0, 1.0],
Self::Lab => [100.0, 1.0, 1.0, 1.0],
Self::Lch => [100.0, 1.0, 360.0, 1.0],
}
}
pub fn color(self, c1: f32, c2: f32, c3: f32, alpha: f32) -> Color {
match self {
Self::Srgb => Color::Srgba(Srgba::new(c1, c2, c3, alpha)),
Self::Linear => Color::LinearRgba(LinearRgba::new(c1, c2, c3, alpha)),
Self::Hsl => Color::Hsla(Hsla::new(c1, c2, c3, alpha)),
Self::Hsv => Color::Hsva(Hsva::new(c1, c2, c3, alpha)),
Self::Hwb => Color::Hwba(Hwba::new(c1, c2, c3, alpha)),
Self::Oklab => Color::Oklaba(Oklaba::new(c1, c2, c3, alpha)),
Self::Oklch => Color::Oklcha(Oklcha::new(c1, c2, c3, alpha)),
Self::Lab => Color::Laba(Laba::new(c1, c2, c3, alpha)),
Self::Lch => Color::Lcha(Lcha::new(c1, c2, c3, alpha)),
Self::Xyz => Color::Xyza(Xyza::new(c1, c2, c3, alpha)),
}
}
pub fn gray(self, v: f32, alpha: f32) -> Color {
match self {
Self::Srgb | Self::Linear | Self::Xyz => self.color(v, v, v, alpha),
Self::Hsl | Self::Hsv | Self::Hwb => self.color(0.0, 0.0, v, alpha),
Self::Oklab | Self::Lab => self.color(v, 0.0, 0.0, alpha),
Self::Oklch | Self::Lch => self.color(v, 0.0, 0.0, alpha),
}
}
}
#[derive(Debug, Clone, Copy, Component)]
pub struct ColorMode {
pub space: ColorSpace,
pub max: [f32; 4],
}
impl Default for ColorMode {
fn default() -> Self {
Self::with_defaults(ColorSpace::Srgb)
}
}
impl ColorMode {
pub fn new(space: ColorSpace, max1: f32, max2: f32, max3: f32, max_alpha: f32) -> Self {
Self {
space,
max: [max1, max2, max3, max_alpha],
}
}
pub fn with_defaults(space: ColorSpace) -> Self {
Self {
space,
max: space.default_maxes(),
}
}
pub fn with_uniform_max(space: ColorSpace, max: f32) -> Self {
Self {
space,
max: [max, max, max, max],
}
}
/// Scale a raw float value for a given channel to the 0-1 normalized range.
pub fn scale(&self, value: f32, ch: usize) -> f32 {
let native = self.space.default_maxes();
value / self.max[ch] * native[ch]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_srgb_color() {
let c = ColorSpace::Srgb.color(1.0, 0.0, 0.0, 1.0);
let s: Srgba = c.into();
assert!((s.red - 1.0).abs() < 1e-6);
assert!(s.green.abs() < 1e-6);
}
#[test]
fn test_hsl_color() {
let c = ColorSpace::Hsl.color(180.0, 0.5, 0.5, 1.0);
let h: Hsla = c.into();
assert!((h.hue - 180.0).abs() < 0.5);
assert!((h.saturation - 0.5).abs() < 0.01);
assert!((h.lightness - 0.5).abs() < 0.01);
}
#[test]
fn test_hsv_red() {
let c = ColorSpace::Hsv.color(0.0, 1.0, 1.0, 1.0);
let s: Srgba = c.into();
assert!((s.red - 1.0).abs() < 0.01);
assert!(s.green < 0.01);
}
#[test]
fn test_gray_srgb() {
let c = ColorSpace::Srgb.gray(0.5, 1.0);
let s: Srgba = c.into();
assert!((s.red - 0.5).abs() < 0.01);
assert!((s.green - 0.5).abs() < 0.01);
assert!((s.blue - 0.5).abs() < 0.01);
}
#[test]
fn test_gray_hsl() {
let c = ColorSpace::Hsl.gray(0.5, 1.0);
let s: Srgba = c.into();
assert!((s.red - 0.5).abs() < 0.05);
assert!((s.green - 0.5).abs() < 0.05);
}
#[test]
fn test_scale_identity() {
let mode = ColorMode::default();
assert!((mode.scale(0.5, 0) - 0.5).abs() < 1e-6);
}
#[test]
fn test_scale_255() {
let mode = ColorMode::with_uniform_max(ColorSpace::Srgb, 255.0);
assert!((mode.scale(255.0, 0) - 1.0).abs() < 1e-4);
}
#[test]
fn test_scale_hsl_percent() {
let mode = ColorMode::new(ColorSpace::Hsl, 360.0, 100.0, 100.0, 1.0);
assert!((mode.scale(180.0, 0) - 180.0).abs() < 0.01);
assert!((mode.scale(50.0, 1) - 0.5).abs() < 1e-4);
assert!((mode.scale(50.0, 2) - 0.5).abs() < 1e-4);
}
}