use color::{Color, Error};
use handles::map;
use handles::hex;
pub fn get_hex_alpha_value(color: &Color) -> f32 {
let hex_vec = hex::handle_hex_value( &color).unwrap();
let value: &[&str] = &hex_vec[&hex_vec.len() - 2..];
let a: usize = map::map_hex(&*value[1].to_uppercase());
let b: usize = map::map_hex(&*value[0].to_uppercase());
((a + b * 16) as f32 / 255.0 * 10000.0).round() / 10000.0
}
pub fn get_rgba_alpha_value(color: &str) -> Result<f32, Error> {
let vec_value: Vec<&str> = color.split(',').collect();
let result = match vec_value.len() {
3 => 1f32,
4 => {
let alpha: String = vec_value[3].replace(")", "");
match alpha.parse::<f32>() {
Ok(value) => value,
Err(_error) => return Err(Error::RgbAlphaFormat),
}
},
_ => return Err(Error::RgbAlphaFormat)
};
Ok(result)
}
pub fn handel_alpha_to_hexadecimal(alpha: f32) -> String {
if alpha == 1f32 || alpha > 1f32 {
return String::from("FF");
}
let to_decimal = alpha * 256f32;
let b = (to_decimal % 16f32) as usize;
let a = (to_decimal / 16f32 % 16f32) as usize;
map::map_rgb(&a).to_owned() + map::map_rgb(&b)
}
pub fn convert_rgb_value_to_number(value: &str) ->f32 {
let result;
if value.contains("%") {
let n = value.replace("%", "");
result = match n.parse::<f32>() {
Ok(value) => value / 100f32,
Err(_error) => 0f32,
};
} else {
result = match value.parse::<f32>() {
Ok(value) => value / 255f32,
Err(_error) => 0f32,
};
}
result
}
pub fn convert_alpha_value_to_number(value: &str) ->f32 {
let result;
if value.contains("%") {
let n = value.replace("%", "");
result = match n.parse::<f32>() {
Ok(value) => value / 100f32,
Err(_error) => 0f32,
};
} else {
result = match value.parse::<f32>() {
Ok(value) => value,
Err(_error) => 0f32,
};
}
result
}
pub fn convert_f32_to_percent(v: f32) -> String {
if v == 0f32 {
String::from("0")
} else if v > 100f32 {
format!("{:.2}", v)
} else {
let value = v * 100f32;
let mut s = value.to_string();
s.truncate(5);
s.push('%');
s
}
}
pub fn convert_f32_to_string(v: f32) -> String {
if v == 0f32 {
String::from("0")
} else {
format!("{:.2}", v)
}
}
pub fn round(value: f32, digit: u32) -> f32 {
let a = 10usize.pow(digit) as f32;
(value * a).round() / a
}
pub fn decimal_to_percent(value: f32) -> String {
if value < 1f32 {
format!("{}%", round(value, 2) * 100f32)
} else {
format!("{}", round(value, 2))
}
}