tweaked colors

This commit is contained in:
JOLIMAITRE Matthieu 2022-04-04 16:02:12 +03:00
parent a7b292c1e7
commit 855cdbd235

View file

@ -1,8 +1,7 @@
use std::{ use std::{
collections::hash_map::DefaultHasher, collections::hash_map::DefaultHasher,
fmt::format,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
intrinsics::transmute, mem::transmute_copy,
}; };
use termion::color; use termion::color;
@ -95,22 +94,22 @@ impl Tile {
let hash = hasher.finish(); let hash = hasher.finish();
// SAFETY: // SAFETY:
// there are no logic that relies on the value of the outputted numbers, thus it is safe to create them without constructors // there are no logic that relies on the value of the outputted numbers, thus it is safe to create them without constructors
let [frac_a, frac_b]: [f64; 2] = unsafe { transmute::<_, [u32; 2]>(hash) } let [frac_a, frac_b]: [f64; 2] = unsafe { transmute_copy::<_, [u32; 2]>(&hash) }
.into_iter() .into_iter()
.map(|frac| (frac as f64) / (u32::MAX as f64)) .map(|frac| (frac as f64) / (u32::MAX as f64))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.try_into() .try_into()
.unwrap(); .unwrap();
let mut remaining = 255f64; let mut remaining = 300f64;
let r = Self::take_frac(&mut remaining, frac_a) as u8; let r = Self::take_fraction(&mut remaining, frac_a, 200.) as u8;
let g = Self::take_frac(&mut remaining, frac_b) as u8; let g = Self::take_fraction(&mut remaining, frac_b, 200.) as u8;
let b = remaining as u8; let b = remaining as u8;
color::Rgb(r, g, b) color::Rgb(r, g, b)
} }
fn take_frac(remainder: &mut f64, frac: f64) -> f64 { fn take_fraction(remainder: &mut f64, frac: f64, max: f64) -> f64 {
let result = *remainder * frac; let result = (*remainder * frac).min(max);
*remainder -= result; *remainder -= result;
result result
} }