fix: pack rgb seems to be wrong (#10)

* transmute for bitwise reinterpretation

* safe bit casting
This commit is contained in:
stelzo 2023-08-22 08:34:53 +02:00 committed by GitHub
parent cfe5b6a2f6
commit 3013f8e38b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 4 deletions

View File

@ -2,14 +2,16 @@ use crate::{ConversionError, MetaNames, PointConvertible, PointMeta};
#[inline]
fn pack_rgb(r: u8, g: u8, b: u8) -> f32 {
((r as u32) << 16) as f32 + ((g as u32) << 8) as f32 + (b as u32) as f32
let packed = ((r as u32) << 16) + ((g as u32) << 8) + (b as u32);
f32::from_bits(packed)
}
#[inline]
fn unpack_rgb(rgb: f32) -> [u8; 3] {
let r: u8 = ((rgb as u32) >> 16) as u8;
let g: u8 = ((rgb as u32) >> 8) as u8;
let b: u8 = (rgb as u32) as u8;
let packed: u32 = rgb.to_bits();
let r: u8 = (packed >> 16) as u8;
let g: u8 = (packed >> 8) as u8;
let b: u8 = packed as u8;
[r, g, b]
}