@@ -11,16 +11,15 @@ pub struct Rgb {
1111}
1212
1313impl Rgb {
14-
1514 // The simplest case is when R, G, and B are known; if the colour is passed as a hex integer,
1615 // then the following constructor can be used instead. This function is marked 'const' so it
1716 // can be used to initialize maps at compile time.
1817 pub const fn from_u32 ( h : u32 ) -> Self {
1918 // Ignore bits > 16
2019 Self {
2120 r : ( ( h >> 16 ) & 0xFF ) as u8 ,
22- g : ( ( h >> 8 ) & 0xFF ) as u8 ,
23- b : ( h & 0xFF ) as u8 ,
21+ g : ( ( h >> 8 ) & 0xFF ) as u8 ,
22+ b : ( h & 0xFF ) as u8 ,
2423 }
2524 }
2625
@@ -30,31 +29,30 @@ impl Rgb {
3029 let s = s. trim ( ) ;
3130
3231 // Strip common prefixes
33- let s = s. strip_prefix ( "0x" )
32+ let s = s
33+ . strip_prefix ( "0x" )
3434 . or_else ( || s. strip_prefix ( "#" ) )
3535 . unwrap_or ( s) ;
3636
3737 match s. len ( ) {
3838 6 => {
3939 // RRGGBB
40- let value = u32:: from_str_radix ( s, 16 )
41- . map_err ( |_| "invalid hex color" ) ?;
40+ let value = u32:: from_str_radix ( s, 16 ) . map_err ( |_| "invalid hex color" ) ?;
4241
4342 Ok ( Self {
4443 r : ( ( value >> 16 ) & 0xFF ) as u8 ,
45- g : ( ( value >> 8 ) & 0xFF ) as u8 ,
46- b : ( value & 0xFF ) as u8 ,
44+ g : ( ( value >> 8 ) & 0xFF ) as u8 ,
45+ b : ( value & 0xFF ) as u8 ,
4746 } )
4847 }
4948 8 => {
5049 // Assume AARRGGBB, ignore alpha
51- let value = u32:: from_str_radix ( s, 16 )
52- . map_err ( |_| "invalid hex color" ) ?;
50+ let value = u32:: from_str_radix ( s, 16 ) . map_err ( |_| "invalid hex color" ) ?;
5351
5452 Ok ( Self {
5553 r : ( ( value >> 16 ) & 0xFF ) as u8 ,
56- g : ( ( value >> 8 ) & 0xFF ) as u8 ,
57- b : ( value & 0xFF ) as u8 ,
54+ g : ( ( value >> 8 ) & 0xFF ) as u8 ,
55+ b : ( value & 0xFF ) as u8 ,
5856 } )
5957 }
6058 _ => Err ( "hex color must have 6 or 8 digits" ) ,
@@ -67,22 +65,46 @@ impl Rgb {
6765 }
6866}
6967
70- pub const RGB_RED : Rgb = Rgb { r : 255 , g : 0 , b : 0 } ;
71- pub const RGB_GRAY : Rgb = Rgb { r : 127 , g : 127 , b : 127 } ;
72- pub const RGB_LIGHT_GRAY : Rgb = Rgb { r : 224 , g : 224 , b : 224 } ;
73- pub const RGB_WHITE : Rgb = Rgb { r : 255 , g : 255 , b : 255 } ;
68+ pub const RGB_RED : Rgb = Rgb { r : 255 , g : 0 , b : 0 } ;
69+ pub const RGB_GRAY : Rgb = Rgb {
70+ r : 127 ,
71+ g : 127 ,
72+ b : 127 ,
73+ } ;
74+ pub const RGB_LIGHT_GRAY : Rgb = Rgb {
75+ r : 224 ,
76+ g : 224 ,
77+ b : 224 ,
78+ } ;
79+ pub const RGB_WHITE : Rgb = Rgb {
80+ r : 255 ,
81+ g : 255 ,
82+ b : 255 ,
83+ } ;
7484
7585pub const GAP_COLOR : Rgb = RGB_LIGHT_GRAY ;
7686
7787// In-house colors
78- pub const TERMAL_ORANGE : Rgb = Rgb { r : 255 , g : 165 , b : 0 } ;
79- pub const TERMAL_SALMON : Rgb = Rgb { r : 250 , g : 128 , b : 114 } ;
88+ pub const TERMAL_ORANGE : Rgb = Rgb {
89+ r : 255 ,
90+ g : 165 ,
91+ b : 0 ,
92+ } ;
93+ pub const TERMAL_SALMON : Rgb = Rgb {
94+ r : 250 ,
95+ g : 128 ,
96+ b : 114 ,
97+ } ;
8098
8199// ASCII 8-Color palette colors
82- pub const ASCII_8_COLOR_GREEN : Rgb = Rgb { r : 0 , g : 128 , b : 0 } ;
83- pub const ASCII_8_COLOR_MAGENTA : Rgb = Rgb { r : 128 , g : 0 , b : 128 } ;
84- pub const ASCII_8_COLOR_RED : Rgb = Rgb { r : 128 , g : 0 , b : 0 } ;
85- pub const ASCII_8_COLOR_BLUE : Rgb = Rgb { r : 0 , g : 0 , b : 128 } ;
100+ pub const ASCII_8_COLOR_GREEN : Rgb = Rgb { r : 0 , g : 128 , b : 0 } ;
101+ pub const ASCII_8_COLOR_MAGENTA : Rgb = Rgb {
102+ r : 128 ,
103+ g : 0 ,
104+ b : 128 ,
105+ } ;
106+ pub const ASCII_8_COLOR_RED : Rgb = Rgb { r : 128 , g : 0 , b : 0 } ;
107+ pub const ASCII_8_COLOR_BLUE : Rgb = Rgb { r : 0 , g : 0 , b : 128 } ;
86108
87109// Lesk aa colors (source?)
88110pub const LESK_ORANGE : Rgb = TERMAL_ORANGE ;
@@ -93,14 +115,46 @@ pub const LESK_RED: Rgb = ASCII_8_COLOR_RED;
93115
94116// ClustalX aa colors (source:
95117// https://www.cgl.ucsf.edu/chimera/1.2065/docs/ContributedSoftware/multalignviewer/colprot.par)
96- pub const CLUSTALX_RED : Rgb = Rgb { r : 229 , g : 51 , b : 25 } ;
97- pub const CLUSTALX_BLUE : Rgb = Rgb { r : 25 , g : 127 , b : 229 } ;
98- pub const CLUSTALX_GREEN : Rgb = Rgb { r : 25 , g : 204 , b : 25 } ;
99- pub const CLUSTALX_CYAN : Rgb = Rgb { r : 25 , g : 178 , b : 178 } ;
100- pub const CLUSTALX_PINK : Rgb = Rgb { r : 229 , g : 127 , b : 127 } ;
101- pub const CLUSTALX_MAGENTA : Rgb = Rgb { r : 204 , g : 76 , b : 204 } ;
102- pub const CLUSTALX_YELLOW : Rgb = Rgb { r : 204 , g : 204 , b : 0 } ;
103- pub const CLUSTALX_ORANGE : Rgb = Rgb { r : 229 , g : 153 , b : 76 } ;
118+ pub const CLUSTALX_RED : Rgb = Rgb {
119+ r : 229 ,
120+ g : 51 ,
121+ b : 25 ,
122+ } ;
123+ pub const CLUSTALX_BLUE : Rgb = Rgb {
124+ r : 25 ,
125+ g : 127 ,
126+ b : 229 ,
127+ } ;
128+ pub const CLUSTALX_GREEN : Rgb = Rgb {
129+ r : 25 ,
130+ g : 204 ,
131+ b : 25 ,
132+ } ;
133+ pub const CLUSTALX_CYAN : Rgb = Rgb {
134+ r : 25 ,
135+ g : 178 ,
136+ b : 178 ,
137+ } ;
138+ pub const CLUSTALX_PINK : Rgb = Rgb {
139+ r : 229 ,
140+ g : 127 ,
141+ b : 127 ,
142+ } ;
143+ pub const CLUSTALX_MAGENTA : Rgb = Rgb {
144+ r : 204 ,
145+ g : 76 ,
146+ b : 204 ,
147+ } ;
148+ pub const CLUSTALX_YELLOW : Rgb = Rgb {
149+ r : 204 ,
150+ g : 204 ,
151+ b : 0 ,
152+ } ;
153+ pub const CLUSTALX_ORANGE : Rgb = Rgb {
154+ r : 229 ,
155+ g : 153 ,
156+ b : 76 ,
157+ } ;
104158
105159// JalView Nucleotide Colors
106160
@@ -138,7 +192,6 @@ pub struct ResidueColorMap {
138192}
139193
140194impl ResidueColorMap {
141-
142195 pub fn with_default ( gap_color : Rgb , default : Rgb ) -> Self {
143196 let mut tbl = [ default; 256 ] ;
144197 tbl[ b'-' as usize ] = gap_color;
@@ -148,7 +201,7 @@ impl ResidueColorMap {
148201
149202 pub fn by_name ( name : ColorMapName ) -> Self {
150203 match name {
151- ColorMapName :: AALesk => Self :: aa_lesk ( ) ,
204+ ColorMapName :: AALesk => Self :: aa_lesk ( ) ,
152205 ColorMapName :: AAClustalX => Self :: aa_clustalx ( ) ,
153206 ColorMapName :: DNAJalView => Self :: dna_jalview ( ) ,
154207 ColorMapName :: Monochrome => Self :: monochrome ( ) ,
@@ -169,16 +222,29 @@ impl ResidueColorMap {
169222 self . set ( b. to_ascii_uppercase ( ) , color) ;
170223 }
171224
172-
173225 pub fn monochrome ( ) -> Self {
174226 Self :: with_default ( GAP_COLOR , RGB_WHITE )
175227 }
176228
177229 pub fn dna_basic ( ) -> Self {
178- let mut map = Self :: with_default ( GAP_COLOR , Rgb { r : 160 , g : 160 , b : 160 } ) ;
230+ let mut map = Self :: with_default (
231+ GAP_COLOR ,
232+ Rgb {
233+ r : 160 ,
234+ g : 160 ,
235+ b : 160 ,
236+ } ,
237+ ) ;
179238 map. set_pair ( b'A' , Rgb { r : 0 , g : 200 , b : 0 } ) ;
180239 map. set_pair ( b'T' , Rgb { r : 200 , g : 0 , b : 0 } ) ;
181- map. set_pair ( b'G' , Rgb { r : 255 , g : 165 , b : 0 } ) ;
240+ map. set_pair (
241+ b'G' ,
242+ Rgb {
243+ r : 255 ,
244+ g : 165 ,
245+ b : 0 ,
246+ } ,
247+ ) ;
182248 map. set_pair ( b'C' , Rgb { r : 0 , g : 0 , b : 200 } ) ;
183249 map
184250 }
@@ -256,14 +322,13 @@ impl ResidueColorMap {
256322 map. set_pair ( b'X' , RGB_WHITE ) ;
257323 map
258324 }
259-
260325}
261326
262327impl Display for ResidueColorMap {
263- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
328+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
264329 write ! ( f, "{{" ) ?;
265330 let residues = "ABCDEFGHIKLMNPQRSTUVWXY" ;
266- for c in residues. as_bytes ( ) . iter ( ) {
331+ for c in residues. as_bytes ( ) . iter ( ) {
267332 write ! ( f, "{}: {}, " , * c as char , self . rgb( * c) . to_hex( ) ) ?;
268333 }
269334 write ! ( f, "}}" ) ?;
@@ -274,14 +339,7 @@ impl Display for ResidueColorMap {
274339#[ cfg( test) ]
275340mod test {
276341
277- use super :: {
278- CLUSTALX_MAGENTA ,
279- ColorMapName ,
280- GAP_COLOR ,
281- RGB_RED ,
282- ResidueColorMap ,
283- Rgb ,
284- } ;
342+ use super :: { ColorMapName , ResidueColorMap , Rgb , CLUSTALX_MAGENTA , GAP_COLOR , RGB_RED } ;
285343
286344 #[ test]
287345 fn test_default_colormap ( ) {
@@ -292,8 +350,15 @@ mod test {
292350 #[ test]
293351 fn test_simple_colormap ( ) {
294352 let cmap = ResidueColorMap :: dna_basic ( ) ;
295- assert_eq ! ( Rgb { r: 160 , g: 160 , b: 160 } , cmap. rgb( b'%' ) ) ;
296- assert_eq ! ( Rgb { r: 0 , g: 200 , b: 0 } , cmap. rgb( b'A' ) ) ;
353+ assert_eq ! (
354+ Rgb {
355+ r: 160 ,
356+ g: 160 ,
357+ b: 160
358+ } ,
359+ cmap. rgb( b'%' )
360+ ) ;
361+ assert_eq ! ( Rgb { r: 0 , g: 200 , b: 0 } , cmap. rgb( b'A' ) ) ;
297362 }
298363
299364 #[ test]
@@ -321,5 +386,4 @@ mod test {
321386 assert_eq ! ( GAP_COLOR , cmap. rgb( b'-' ) ) ;
322387 assert_eq ! ( GAP_COLOR , cmap. rgb( b'.' ) ) ;
323388 }
324-
325389}
0 commit comments