-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
154 lines (135 loc) · 4.55 KB
/
main.rs
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
mod colors;
use std::env;
use colors::ComplementaryColors;
use colors::rgb::RGBColor;
fn main() {
let mut args: Vec<String> = env::args().collect();
args.remove(0);
if args.len() == 0 || args[0].eq("-h") || args[0].eq("--help") {
send_help();
return;
}
let mut mode = 0;
match args[0].as_ref() {
"-s" | "--shades" => mode = 1,
"-c" | "--complementary" => mode = 2,
_ => {}
}
if mode != 0 {
args.remove(0);
}
let mut colors: Vec<RGBColor> = Vec::new();
for arg in args {
if colors::is_rgb(&arg) {
match RGBColor::from_rgb(&arg) {
Ok(color) => {
colors.push(color);
}
Err(why) => {
if let colors::Error::ParseError(component) = why {
println!("Unable to parse {} component for '{}'", component, arg);
} else {
println!("Unknown error while parsing '{}'", arg);
}
}
}
} else if colors::is_hex(&arg) {
match RGBColor::from_hex(&arg) {
Ok(color) => {
colors.push(color);
}
Err(why) => {
if let colors::Error::ParseError(component) = why {
println!("Unable to parse {} component for '{}'", component, arg);
} else {
println!("Unknown error while parsing '{}'", arg);
}
}
}
} else {
println!("Unknown format '{}'", &arg);
}
}
match mode {
0 => color_info(colors),
1 => println!("Shades"),
2 => complementary_colors(colors),
_ => {}
}
}
fn send_help() {
println!(
" \
Colou-rs - A terminal color utility\n \
Usage\n \
color-rs [] - Display"
)
}
fn color_info(colors: Vec<RGBColor>) {
for color in colors {
let hsv = colors::hsv::HSVColor::from(color);
let hsl = colors::hsl::HSLColor::from(color);
let mut ansi = color.to_ansi_background();
ansi.foreground(if color.is_light() {
RGBColor::new(0, 0, 0)
} else {
RGBColor::new(255, 255, 255)
});
println!(
"{}\nHEX: {}\nRGB: {}\nHSV: {}\nHSL: {}",
ansi.paint("Colou-rs"),
color.to_hex(),
color.to_rgb(", "),
hsv.to_str(),
hsl.to_str(),
);
}
}
fn complementary_colors(colors: Vec<RGBColor>) {
for color in colors {
let mut ul = colors::ansi::ColorAnsi::new(None, None);
ul.underline(true);
let mut ulb = colors::ansi::ColorAnsi::new(None, None);
ulb.underline(true);
ulb.bold(true);
let triad = color.triad();
let tetradic = color.tetradic();
let anal = color.analogous();
let s_comp = color.split_complemetary();
println!(
" {}\
\n ╭───────────╮ ╭───────────╮\
\n │ {} │ │ {} │\
\n │ {} │ │ {} │\
\n │ {} │ │ {} │\
\n │ {} │ │ {} │\
\n ╰───────────╯ │ {} │\
\n ╭───────────╮ ╰───────────╯\
\n │ {} │\
\n │ {} │\
\n │ {} │\
\n │ {} │\
\n ╰───────────╯\
\n ╭───────────╮\
\n │ {} │\
\n │ {} │\
\n │ {} │\
\n │ {} │\
\n ╰───────────╯",
ulb.paint(format!("Complementary color for {}", color.to_hex())),
ul.paint("Triad"), ul.paint("Tetradic"),
triad[0].paint_hex(), tetradic[0].paint_hex(),
triad[1].paint_hex(), tetradic[1].paint_hex(),
triad[2].paint_hex(), tetradic[2].paint_hex(),
tetradic[3].paint_hex(),
ul.paint("Analogous"),
anal[0].paint_hex(),
anal[1].paint_hex(),
anal[2].paint_hex(),
ul.paint("Split"),
s_comp[0].paint_hex(),
s_comp[1].paint_hex(),
s_comp[2].paint_hex(),
);
}
}