@@ -22,55 +22,45 @@ 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); !f.isEmpty ()) {
5048 using Conv::parse;
5149
52- if (f.getName () == " rgb" ) {
53- auto ps = f.getParams ();
54- if (ps.size () != 3 )
55- throw std::logic_error (" invalid color string" );
56- *this = RGBA (parse<uint32_t >(ps.at (0 )),
50+ auto &&ps = f.getParams ();
51+ if (f.getName () == " rgb" && ps.size () == 3 ) {
52+ return RGBA (parse<uint32_t >(ps.at (0 )),
5753 parse<uint32_t >(ps.at (1 )),
5854 parse<uint32_t >(ps.at (2 )));
5955 }
60- else if (f.getName () == " rgba" ) {
61- auto ps = f.getParams ();
62- if (ps.size () != 4 )
63- throw std::logic_error (" invalid color string" );
64- *this = RGBA (parse<uint32_t >(ps.at (0 )),
56+ if (f.getName () == " rgba" && ps.size () == 4 ) {
57+ return RGBA (parse<uint32_t >(ps.at (0 )),
6558 parse<uint32_t >(ps.at (1 )),
6659 parse<uint32_t >(ps.at (2 )),
6760 static_cast <uint8_t >(parse<double >(ps.at (3 )) * 255 ));
6861 }
69- else
70- throw std::logic_error (" invalid color string" );
7162 }
72- else
73- throw std::logic_error (" invalid color string" );
63+ throw std::logic_error (" invalid color string" );
7464}
7565
7666Color Color::RGB (uint32_t rgb)
0 commit comments