use core::fmt;
use std::error::Error;
use crate::braille::Pixel;
pub const MIN_ZOOM: f64 = 0.001;
pub const MIN_DIFFERENCE: f64 = 1E-10;
pub(crate) fn round<T>(v: T) -> i32
where
T: Into<f64>,
{
v.into().round() as i32
}
pub(crate) fn mean(a: &[f64]) -> f64 {
let sum = a.iter().sum::<f64>();
sum / a.len() as f64
}
pub(crate) fn check_zoom(v: f64) {
if v <= MIN_ZOOM {
panic!("zoom too small!");
}
}
pub fn get_pos<T>(x: T, y: T) -> (i32, i32)
where
T: Into<f64>,
{
let (x, y) = (round(x), round(y));
let row = if y < 0 { (y + 1) / 4 - 1 } else { y / 4 };
let col = if x < 0 { (x - 1) / 2 } else { x / 2 };
(col, row)
}
#[allow(unused)]
pub(crate) fn make_braille(c: char) -> Option<Pixel> {
let c = c as u32;
if (0x2800..=0x28FF).contains(&c) {
unsafe { Some(Pixel::from_unchecked(c - 0x2800)) }
} else {
None
}
}
#[derive(Debug, Clone)]
pub struct RsilleErr {
msg: String,
}
impl RsilleErr {
pub fn new(msg: String) -> Self {
Self { msg }
}
pub fn to_rsille_err<E: Error>(e: E) -> RsilleErr {
RsilleErr::new(e.to_string())
}
}
impl fmt::Display for RsilleErr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg)
}
}