Skip to content

Commit f75a235

Browse files
authored
Use boxed slice for lookup table to avoid stack overflow (#5212)
* Closes <#5210> * [x] I have followed the instructions in the PR template
1 parent 04ab5e7 commit f75a235

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

crates/ecolor/src/color32.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,17 @@ impl Color32 {
108108
// common-case optimization
109109
255 => Self::from_rgb(r, g, b),
110110
a => {
111-
static LOOKUP_TABLE: OnceLock<[u8; 256 * 256]> = OnceLock::new();
111+
static LOOKUP_TABLE: OnceLock<Box<[u8]>> = OnceLock::new();
112112
let lut = LOOKUP_TABLE.get_or_init(|| {
113113
use crate::{gamma_u8_from_linear_f32, linear_f32_from_gamma_u8};
114-
core::array::from_fn(|i| {
115-
let [value, alpha] = (i as u16).to_ne_bytes();
116-
let value_lin = linear_f32_from_gamma_u8(value);
117-
let alpha_lin = linear_f32_from_linear_u8(alpha);
118-
gamma_u8_from_linear_f32(value_lin * alpha_lin)
119-
})
114+
(0..=u16::MAX)
115+
.map(|i| {
116+
let [value, alpha] = i.to_ne_bytes();
117+
let value_lin = linear_f32_from_gamma_u8(value);
118+
let alpha_lin = linear_f32_from_linear_u8(alpha);
119+
gamma_u8_from_linear_f32(value_lin * alpha_lin)
120+
})
121+
.collect()
120122
});
121123

122124
let [r, g, b] =

0 commit comments

Comments
 (0)