@@ -22,55 +22,51 @@ Color::operator std::string() const
2222 return res;
2323}
2424
25- Color::Color (const std::string &string)
25+ Color Color::fromString (const std::string &string)
2626{
27- if (string.empty ()) { * this = Transparent (); }
28- else if (string[0 ] == ' #' && string.size () == 9 ) {
29- red = Text::Character::hex (&string[1 ]) / 255.0 ;
30- green = Text::Character::hex (&string[3 ]) / 255.0 ;
31- blue = Text::Character::hex (&string[5 ]) / 255.0 ;
32- alpha = Text::Character::hex (&string[7 ]) / 255.0 ;
27+ if (string.empty ()) return Transparent ();
28+ if (string[0 ] == ' #' && string.size () == 9 ) {
29+ return { Text::Character::hex (&string[1 ]) / 255.0 ,
30+ Text::Character::hex (&string[3 ]) / 255.0 ,
31+ Text::Character::hex (&string[5 ]) / 255.0 ,
32+ Text::Character::hex (&string[7 ]) / 255.0 } ;
3333 }
34- else if (string[0 ] == ' #' && string.size () == 7 ) {
35- red = Text::Character::hex (&string[1 ]) / 255.0 ;
36- green = Text::Character::hex (&string[3 ]) / 255.0 ;
37- blue = Text::Character::hex (&string[5 ]) / 255.0 ;
38- alpha = 1.0 ;
34+ if (string[0 ] == ' #' && string.size () == 7 ) {
35+ return {Text::Character::hex (&string[1 ]) / 255.0 ,
36+ Text::Character::hex (&string[3 ]) / 255.0 ,
37+ Text::Character::hex (&string[5 ]) / 255.0 };
3938 }
40- else if (string[0 ] == ' #' && string.size () == 4 ) {
39+ if (string[0 ] == ' #' && string.size () == 4 ) {
4140 auto r = Text::Character::fromHex (string[1 ]);
4241 auto g = Text::Character::fromHex (string[2 ]);
4342 auto b = Text::Character::fromHex (string[3 ]);
44- red = ((r << 4U ) + r) / 255.0 ;
45- green = ((g << 4U ) + g) / 255.0 ;
46- blue = ((b << 4U ) + b) / 255.0 ;
47- alpha = 1.0 ;
43+ return {((r << 4U ) + r) / 255.0 ,
44+ ((g << 4U ) + g) / 255.0 ,
45+ ((b << 4U ) + b) / 255.0 };
4846 }
49- else if (const Text::FuncString f (string, false ); !f.isEmpty ()) {
47+ if (const Text::FuncString f (string, false ); !f.isEmpty ()) {
5048 using Conv::parse;
5149
5250 if (f.getName () == " rgb" ) {
5351 auto ps = f.getParams ();
5452 if (ps.size () != 3 )
5553 throw std::logic_error (" invalid color string" );
56- * this = RGBA (parse<uint32_t >(ps.at (0 )),
54+ return RGBA (parse<uint32_t >(ps.at (0 )),
5755 parse<uint32_t >(ps.at (1 )),
5856 parse<uint32_t >(ps.at (2 )));
5957 }
60- else if (f.getName () == " rgba" ) {
58+ if (f.getName () == " rgba" ) {
6159 auto ps = f.getParams ();
6260 if (ps.size () != 4 )
6361 throw std::logic_error (" invalid color string" );
64- * this = RGBA (parse<uint32_t >(ps.at (0 )),
62+ return RGBA (parse<uint32_t >(ps.at (0 )),
6563 parse<uint32_t >(ps.at (1 )),
6664 parse<uint32_t >(ps.at (2 )),
6765 static_cast <uint8_t >(parse<double >(ps.at (3 )) * 255 ));
6866 }
69- else
70- throw std::logic_error (" invalid color string" );
71- }
72- else
7367 throw std::logic_error (" invalid color string" );
68+ }
69+ throw std::logic_error (" invalid color string" );
7470}
7571
7672Color Color::RGB (uint32_t rgb)
0 commit comments