@@ -7,6 +7,10 @@ extern crate termion;
77extern crate image;
88extern crate pigmnts;
99extern crate reqwest;
10+ #[ macro_use]
11+ extern crate lazy_static;
12+
13+ pub mod utils;
1014
1115use clap:: { App , Arg } ;
1216use spinners:: { Spinner , Spinners } ;
@@ -17,7 +21,7 @@ use image::GenericImageView;
1721use pigmnts:: { Pixels , color:: { LAB , RGB , HSL } , weights, pigments_pixels} ;
1822
1923/// Creates a vector of strings with elements added conditonally
20- ///
24+ ///
2125/// # Example
2226/// ```
2327/// let myvec = conditional_vec![
@@ -40,11 +44,11 @@ macro_rules! conditional_vec {
4044}
4145
4246/// Creates a color palette from image
43- ///
47+ ///
4448/// Image is loaded from `image_path` and a palette of `count` colors are created
4549fn pigmnts ( image_path : & str , count : u8 ) -> Result < ( Vec < ( LAB , f32 ) > , u128 ) , Box < dyn std:: error:: Error > > {
4650 let mut img;
47-
51+
4852 if image_path. starts_with ( "http://" ) || image_path. starts_with ( "https://" ) {
4953 let mut res = reqwest:: blocking:: get ( image_path) ?;
5054 let mut buf: Vec < u8 > = vec ! [ ] ;
@@ -54,28 +58,27 @@ fn pigmnts(image_path: &str, count: u8) -> Result<(Vec<(LAB, f32)>, u128), Box<d
5458 else {
5559 img = image:: open ( image_path) ?;
5660 }
57-
61+
5862 img = img. resize ( 512 , 512 , image:: imageops:: FilterType :: CatmullRom ) ;
59-
63+
6064 // Start a timer
6165 let now = Instant :: now ( ) ;
62-
66+
6367 let pixels: Pixels = img
6468 . pixels ( )
6569 . map ( |( _, _, pix) | LAB :: from_rgb ( pix[ 0 ] , pix[ 1 ] , pix[ 2 ] ) )
6670 . collect ( ) ;
67-
71+
6872 let weightfn = weights:: resolve_mood ( & weights:: Mood :: Dominant ) ;
6973 let mut output = pigments_pixels ( & pixels, count, weightfn, None ) ;
70-
74+
7175 // Sort the output colors based on dominance
7276 output. sort_by ( |( _, a) , ( _, b) | b. partial_cmp ( a) . unwrap ( ) ) ;
73-
77+
7478 return Ok ( ( output, now. elapsed ( ) . as_millis ( ) ) ) ;
7579}
7680
7781fn main ( ) {
78-
7982 let matches = App :: new ( "Pigmnts" )
8083 . version ( env ! ( "CARGO_PKG_VERSION" ) )
8184 . author ( env ! ( "CARGO_PKG_AUTHORS" ) )
@@ -119,22 +122,27 @@ fn main() {
119122 . short ( "d" )
120123 . long ( "dominance" )
121124 . help ( "Enable dominance percentage of colors" ) )
125+ . arg ( Arg :: with_name ( "name" )
126+ . short ( "n" )
127+ . long ( "name" )
128+ . help ( "Enable nearest names of colors" ) )
122129 . get_matches ( ) ;
123-
130+
124131 let image_paths = matches. values_of ( "input" ) . unwrap ( ) ;
125132 let mut counts = values_t ! ( matches, "count" , u8 ) . unwrap_or ( Vec :: new ( ) ) ;
126133 let is_quiet = matches. is_present ( "quiet" ) ;
127134 let is_rgb = matches. is_present ( "rgb" ) ;
128135 let is_hsl = matches. is_present ( "hsl" ) ;
129136 let is_lab = matches. is_present ( "lab" ) ;
130137 let is_dom = matches. is_present ( "dominance" ) ;
138+ let is_name = matches. is_present ( "name" ) ;
131139 let mut is_hex = matches. is_present ( "hex" ) ;
132-
140+
133141 // Hex format is enabled when other formats are disabled
134142 if !is_rgb && !is_hsl & !is_lab {
135143 is_hex = true ;
136144 }
137-
145+
138146 // Fill the default count value (5) for every input file if not specified
139147 loop {
140148 let diff: i8 = image_paths. len ( ) as i8 - counts. len ( ) as i8 ;
@@ -144,40 +152,41 @@ fn main() {
144152 break ;
145153 }
146154 }
147-
155+
148156 // Enumerate through each image_path and generate palettes
149157 for ( i, image_path) in image_paths. enumerate ( ) {
150-
158+
151159 if is_quiet {
152160 // Quiet mode only shows the result separated by ':'
153-
161+
154162 let ( result, _) = pigmnts ( image_path, counts[ i] )
155163 . unwrap_or_else ( |err| {
156164 eprintln ! ( "Problem creating palette: {}" , err) ;
157165 process:: exit ( 1 ) ;
158166 } ) ;
159-
167+
160168 for ( color, dominance) in result. iter ( ) {
161169 let rgb = RGB :: from ( color) ;
162-
170+
163171 let record = conditional_vec ! [
164172 is_hex => rgb. hex( ) ,
165173 is_rgb => rgb,
166174 is_hsl => HSL :: from( color) ,
167175 is_lab => color,
168- is_dom => dominance * 100.0
176+ is_dom => dominance * 100.0 ,
177+ is_name => utils:: near_color_name( color)
169178 ] ;
170-
179+
171180 println ! ( "{}" , record. join( ":" ) ) ;
172181 }
173-
182+
174183 } else {
175-
184+
176185 print ! ( "{}{}Creating a palette of " , color:: Fg ( color:: White ) , style:: Bold ) ;
177186 print ! ( "{}{} " , color:: Fg ( color:: Blue ) , counts[ i] ) ;
178187 print ! ( "{}colors from " , color:: Fg ( color:: White ) ) ;
179188 println ! ( "{}{}{}" , color:: Fg ( color:: Blue ) , image_path, style:: Reset ) ;
180-
189+
181190 // Show the spinner in the terminal
182191 let sp = Spinner :: new ( Spinners :: Dots , String :: default ( ) ) ;
183192 let ( result, time) = pigmnts ( image_path, counts[ i] )
@@ -191,11 +200,11 @@ fn main() {
191200 ) ;
192201 process:: exit ( 1 ) ;
193202 } ) ;
194-
203+
195204 // Stop the spinner
196205 sp. stop ( ) ;
197206 println ! ( ) ;
198-
207+
199208 let mut table = Table :: new ( ) ;
200209 table. set_format (
201210 format:: FormatBuilder :: from ( * format:: consts:: FORMAT_CLEAN )
@@ -204,6 +213,7 @@ fn main() {
204213 ) ;
205214 let titles = conditional_vec ! [
206215 true => "" , // Title for color preview
216+ is_name => "Name" ,
207217 is_hex => "Hex" ,
208218 is_rgb => "RGB" ,
209219 is_hsl => "HSL" ,
@@ -218,10 +228,11 @@ fn main() {
218228 . collect ( )
219229 )
220230 ) ;
221-
231+
222232 for ( color, dominance) in result. iter ( ) {
223233 let rgb = RGB :: from ( color) ;
224234 let values = conditional_vec ! [
235+ is_name => utils:: near_color_name( color) ,
225236 is_hex => rgb. hex( ) ,
226237 is_rgb => rgb,
227238 is_hsl => HSL :: from( color) ,
@@ -232,16 +243,16 @@ fn main() {
232243 // Color preview is added
233244 format!( "{} {}" , color:: Bg ( color:: Rgb ( rgb. r, rgb. g, rgb. b) ) , style:: Reset )
234245 ] ;
235-
246+
236247 for value in values. iter ( ) {
237248 record. add_cell ( cell ! ( value) ) ;
238249 }
239-
250+
240251 table. add_row ( record) ;
241252 }
242253 table. printstd ( ) ;
243254 println ! ( ) ;
244-
255+
245256 println ! (
246257 "{}{}✓ Success!{} Took {}ms" ,
247258 color:: Fg ( color:: Green ) ,
@@ -251,5 +262,4 @@ fn main() {
251262 ) ;
252263 }
253264 }
254-
255265}
0 commit comments