1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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))
}
}