minimize implementation
This commit is contained in:
parent
294b795005
commit
5ac7be697a
|
@ -238,97 +238,97 @@ namespace QskRgb
|
||||||
|
|
||||||
namespace QskRgb
|
namespace QskRgb
|
||||||
{
|
{
|
||||||
// Converts a HTML #12345678... color hex string to a 0x12345678... integer
|
// Converts a '#12345678' color hex string to a '0x12345678' unsigned integer
|
||||||
QSK_EXPORT constexpr QRgb fromHexString( const char* const str, const size_t len ) noexcept
|
QSK_EXPORT constexpr QRgb fromHexString( const char* const str, const size_t len ) noexcept
|
||||||
{
|
{
|
||||||
if ( len < 1 || str[ 0 ] != '#' )
|
if ( ( len != 9 && len != 7 ) || str[ 0 ] != '#' )
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
QRgb rgb = 0x00;
|
QRgb argb = 0xFF;
|
||||||
|
|
||||||
for ( size_t i = 1; i < len; ++i )
|
for ( size_t i = 1; i < len; ++i )
|
||||||
{
|
{
|
||||||
switch ( str[ i ] )
|
switch ( str[ i ] )
|
||||||
{
|
{
|
||||||
case '0':
|
case '0':
|
||||||
rgb = ( rgb << 4 ) | 0x0;
|
argb = ( argb << 4 ) | 0x0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '1':
|
case '1':
|
||||||
rgb = ( rgb << 4 ) | 0x1;
|
argb = ( argb << 4 ) | 0x1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '2':
|
case '2':
|
||||||
rgb = ( rgb << 4 ) | 0x2;
|
argb = ( argb << 4 ) | 0x2;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '3':
|
case '3':
|
||||||
rgb = ( rgb << 4 ) | 0x3;
|
argb = ( argb << 4 ) | 0x3;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '4':
|
case '4':
|
||||||
rgb = ( rgb << 4 ) | 0x4;
|
argb = ( argb << 4 ) | 0x4;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '5':
|
case '5':
|
||||||
rgb = ( rgb << 4 ) | 0x5;
|
argb = ( argb << 4 ) | 0x5;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '6':
|
case '6':
|
||||||
rgb = ( rgb << 4 ) | 0x6;
|
argb = ( argb << 4 ) | 0x6;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '7':
|
case '7':
|
||||||
rgb = ( rgb << 4 ) | 0x7;
|
argb = ( argb << 4 ) | 0x7;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '8':
|
case '8':
|
||||||
rgb = ( rgb << 4 ) | 0x8;
|
argb = ( argb << 4 ) | 0x8;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '9':
|
case '9':
|
||||||
rgb = ( rgb << 4 ) | 0x9;
|
argb = ( argb << 4 ) | 0x9;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'A':
|
case 'A':
|
||||||
case 'a':
|
case 'a':
|
||||||
rgb = ( rgb << 4 ) | 0xA;
|
argb = ( argb << 4 ) | 0xA;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'B':
|
case 'B':
|
||||||
case 'b':
|
case 'b':
|
||||||
rgb = ( rgb << 4 ) | 0xB;
|
argb = ( argb << 4 ) | 0xB;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'C':
|
case 'C':
|
||||||
case 'c':
|
case 'c':
|
||||||
rgb = ( rgb << 4 ) | 0xC;
|
argb = ( argb << 4 ) | 0xC;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'D':
|
case 'D':
|
||||||
case 'd':
|
case 'd':
|
||||||
rgb = ( rgb << 4 ) | 0xD;
|
argb = ( argb << 4 ) | 0xD;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'E':
|
case 'E':
|
||||||
case 'e':
|
case 'e':
|
||||||
rgb = ( rgb << 4 ) | 0xE;
|
argb = ( argb << 4 ) | 0xE;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'F':
|
case 'F':
|
||||||
case 'f':
|
case 'f':
|
||||||
rgb = ( rgb << 4 ) | 0xF;
|
argb = ( argb << 4 ) | 0xF;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
rgb = ( rgb << 4 ) | 0x0;
|
argb = ( argb << 4 ) | 0x0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return rgb;
|
return argb;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace literals
|
namespace literals
|
||||||
|
@ -366,7 +366,8 @@ namespace QskRgb
|
||||||
constexpr QRgb parse_hex_literal_x()
|
constexpr QRgb parse_hex_literal_x()
|
||||||
{
|
{
|
||||||
static_assert( C == 'x', "invalid hex prefix character" );
|
static_assert( C == 'x', "invalid hex prefix character" );
|
||||||
return parse_hex_literal_h< 0, Cs... >();
|
constexpr auto alpha = 0xFF;
|
||||||
|
return parse_hex_literal_h< alpha, Cs... >();
|
||||||
}
|
}
|
||||||
|
|
||||||
template< char C, char... Cs >
|
template< char C, char... Cs >
|
||||||
|
@ -382,26 +383,17 @@ namespace QskRgb
|
||||||
QSK_EXPORT constexpr QRgb operator""_rgba(
|
QSK_EXPORT constexpr QRgb operator""_rgba(
|
||||||
const char* const str, const size_t len ) noexcept
|
const char* const str, const size_t len ) noexcept
|
||||||
{
|
{
|
||||||
constexpr auto max = 9;
|
constexpr auto rrggbbaa = 9;
|
||||||
const auto argb = static_cast< int >( fromHexString( str, len ) );
|
const auto c = fromHexString( str, len );
|
||||||
const auto r = ( argb >> ( len == max ? 24 : 16 ) ) & 0xFF;
|
return len == rrggbbaa ? qRgba( qAlpha( c ), qRed( c ), qGreen( c ), qBlue( c ) )
|
||||||
const auto g = ( argb >> ( len == max ? 16 : 8 ) ) & 0xFF;
|
: qRgba( qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) );
|
||||||
const auto b = ( argb >> ( len == max ? 8 : 0 ) ) & 0xFF;
|
|
||||||
const auto a = ( len == max ? argb & 0xFF : 0xFF );
|
|
||||||
return qRgba( r, g, b, a );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts a hex string from '#[AA]RRGGBB' to '0xAARRGGBB' integer
|
// converts a hex string from '#[AA]RRGGBB' to '0xAARRGGBB' integer
|
||||||
QSK_EXPORT constexpr QRgb operator""_argb(
|
QSK_EXPORT constexpr QRgb operator""_argb(
|
||||||
const char* const str, const size_t len ) noexcept
|
const char* const str, const size_t len ) noexcept
|
||||||
{
|
{
|
||||||
constexpr auto max = 9;
|
return fromHexString( str, len );
|
||||||
const auto argb = static_cast< int >( fromHexString( str, len ) );
|
|
||||||
const auto r = ( argb >> 16 ) & 0xFF;
|
|
||||||
const auto g = ( argb >> 8 ) & 0xFF;
|
|
||||||
const auto b = ( argb >> 0 ) & 0xFF;
|
|
||||||
const auto a = ( len == max ? argb >> 24 : 0xFF ) & 0xFF;
|
|
||||||
return qRgba( r, g, b, a );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts a hex literal from '0xRRGGBB[AA]' to '0xAARRGGBB' integer
|
// converts a hex literal from '0xRRGGBB[AA]' to '0xAARRGGBB' integer
|
||||||
|
@ -412,23 +404,10 @@ namespace QskRgb
|
||||||
constexpr auto rrggbbaa = 10;
|
constexpr auto rrggbbaa = 10;
|
||||||
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == rrggbbaa,
|
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == rrggbbaa,
|
||||||
"invalid color literal length" );
|
"invalid color literal length" );
|
||||||
constexpr auto rgba = parse_hex_literal_0< Cs... >();
|
constexpr auto c = parse_hex_literal_0< Cs... >();
|
||||||
if constexpr ( sizeof...( Cs ) == rrggbb )
|
return sizeof...( Cs ) == rrggbbaa
|
||||||
{
|
? qRgba( qAlpha( c ), qRed( c ), qGreen( c ), qBlue( c ) )
|
||||||
const auto r = ( rgba >> ( 4 * 4 ) ) & 0xFF;
|
: qRgba( qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) );
|
||||||
const auto g = ( rgba >> ( 2 * 4 ) ) & 0xFF;
|
|
||||||
const auto b = ( rgba >> ( 0 * 4 ) ) & 0xFF;
|
|
||||||
const auto a = 0xFF;
|
|
||||||
return qRgba( r, g, b, a );
|
|
||||||
}
|
|
||||||
if constexpr ( sizeof...( Cs ) == rrggbbaa )
|
|
||||||
{
|
|
||||||
const auto r = ( rgba >> ( 6 * 4 ) ) & 0xFF;
|
|
||||||
const auto g = ( rgba >> ( 4 * 4 ) ) & 0xFF;
|
|
||||||
const auto b = ( rgba >> ( 2 * 4 ) ) & 0xFF;
|
|
||||||
const auto a = ( rgba >> ( 0 * 4 ) ) & 0xFF;
|
|
||||||
return qRgba( r, g, b, a );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts a hex literal from '0x[AA]RRGGBB' to '0xAARRGGBB' integer
|
// converts a hex literal from '0x[AA]RRGGBB' to '0xAARRGGBB' integer
|
||||||
|
@ -439,23 +418,7 @@ namespace QskRgb
|
||||||
constexpr auto aarrggbb = 10;
|
constexpr auto aarrggbb = 10;
|
||||||
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == aarrggbb,
|
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == aarrggbb,
|
||||||
"invalid color literal length" );
|
"invalid color literal length" );
|
||||||
constexpr auto rgba = parse_hex_literal_0< Cs... >();
|
return parse_hex_literal_0< Cs... >();
|
||||||
if constexpr ( sizeof...( Cs ) == rrggbb )
|
|
||||||
{
|
|
||||||
const auto r = ( rgba >> ( 4 * 4 ) ) & 0xFF;
|
|
||||||
const auto g = ( rgba >> ( 2 * 4 ) ) & 0xFF;
|
|
||||||
const auto b = ( rgba >> ( 0 * 4 ) ) & 0xFF;
|
|
||||||
const auto a = 0xFF;
|
|
||||||
return qRgba( r, g, b, a );
|
|
||||||
}
|
|
||||||
if constexpr ( sizeof...( Cs ) == aarrggbb )
|
|
||||||
{
|
|
||||||
const auto r = ( rgba >> ( 4 * 4 ) ) & 0xFF;
|
|
||||||
const auto g = ( rgba >> ( 2 * 4 ) ) & 0xFF;
|
|
||||||
const auto b = ( rgba >> ( 0 * 4 ) ) & 0xFF;
|
|
||||||
const auto a = ( rgba >> ( 6 * 4 ) ) & 0xFF;
|
|
||||||
return qRgba( r, g, b, a );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -465,26 +428,18 @@ namespace QskRgb
|
||||||
QSK_EXPORT constexpr QColor operator""_rgba(
|
QSK_EXPORT constexpr QColor operator""_rgba(
|
||||||
const char* const str, const size_t len ) noexcept
|
const char* const str, const size_t len ) noexcept
|
||||||
{
|
{
|
||||||
constexpr auto max = 9;
|
constexpr auto rrggbbaa = 9;
|
||||||
const auto argb = static_cast< int >( fromHexString( str, len ) );
|
const auto c = fromHexString( str, len );
|
||||||
const auto r = ( argb >> ( len == max ? 24 : 16 ) ) & 0xFF;
|
return len == rrggbbaa ? QColor{ qAlpha( c ), qRed( c ), qGreen( c ), qBlue( c ) }
|
||||||
const auto g = ( argb >> ( len == max ? 16 : 8 ) ) & 0xFF;
|
: QColor{ qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) };
|
||||||
const auto b = ( argb >> ( len == max ? 8 : 0 ) ) & 0xFF;
|
|
||||||
const auto a = ( len == max ? argb & 0xFF : 0xFF );
|
|
||||||
return { r, g, b, a };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts a hex string from '#[AA]RRGGBB' to a QColor
|
// converts a hex string from '#[AA]RRGGBB' to a QColor
|
||||||
QSK_EXPORT constexpr QColor operator""_argb(
|
QSK_EXPORT constexpr QColor operator""_argb(
|
||||||
const char* const str, const size_t len ) noexcept
|
const char* const str, const size_t len ) noexcept
|
||||||
{
|
{
|
||||||
constexpr auto max = 9;
|
const auto c = fromHexString( str, len );
|
||||||
const auto argb = static_cast< int >( fromHexString( str, len ) );
|
return QColor{ qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) };
|
||||||
const auto r = ( argb >> 16 ) & 0xFF;
|
|
||||||
const auto g = ( argb >> 8 ) & 0xFF;
|
|
||||||
const auto b = ( argb >> 0 ) & 0xFF;
|
|
||||||
const auto a = ( len == max ? argb >> 24 : 0xFF ) & 0xFF;
|
|
||||||
return { r, g, b, a };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts a hex literal from '0xRRGGBB[AA]' to a QColor
|
// converts a hex literal from '0xRRGGBB[AA]' to a QColor
|
||||||
|
@ -495,23 +450,10 @@ namespace QskRgb
|
||||||
constexpr auto rrggbbaa = 10;
|
constexpr auto rrggbbaa = 10;
|
||||||
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == rrggbbaa,
|
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == rrggbbaa,
|
||||||
"invalid color literal length" );
|
"invalid color literal length" );
|
||||||
constexpr auto rgba = parse_hex_literal_0< Cs... >();
|
constexpr auto c = parse_hex_literal_0< Cs... >();
|
||||||
if constexpr ( sizeof...( Cs ) == rrggbb )
|
return sizeof...( Cs ) == rrggbbaa
|
||||||
{
|
? QColor{ qAlpha( c ), qRed( c ), qGreen( c ), qBlue( c ) }
|
||||||
const auto r = ( rgba >> ( 4 * 4 ) ) & 0xFF;
|
: QColor{ qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) };
|
||||||
const auto g = ( rgba >> ( 2 * 4 ) ) & 0xFF;
|
|
||||||
const auto b = ( rgba >> ( 0 * 4 ) ) & 0xFF;
|
|
||||||
const auto a = 0xFF;
|
|
||||||
return { r, g, b, a };
|
|
||||||
}
|
|
||||||
if constexpr ( sizeof...( Cs ) == rrggbbaa )
|
|
||||||
{
|
|
||||||
const auto r = ( rgba >> ( 6 * 4 ) ) & 0xFF;
|
|
||||||
const auto g = ( rgba >> ( 4 * 4 ) ) & 0xFF;
|
|
||||||
const auto b = ( rgba >> ( 2 * 4 ) ) & 0xFF;
|
|
||||||
const auto a = ( rgba >> ( 0 * 4 ) ) & 0xFF;
|
|
||||||
return { r, g, b, a };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts a hex literal from '0x[AA]RRGGBB' to a QColor
|
// converts a hex literal from '0x[AA]RRGGBB' to a QColor
|
||||||
|
@ -522,23 +464,8 @@ namespace QskRgb
|
||||||
constexpr auto aarrggbb = 10;
|
constexpr auto aarrggbb = 10;
|
||||||
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == aarrggbb,
|
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == aarrggbb,
|
||||||
"invalid color literal length" );
|
"invalid color literal length" );
|
||||||
constexpr auto rgba = parse_hex_literal_0< Cs... >();
|
constexpr auto c = parse_hex_literal_0< Cs... >();
|
||||||
if constexpr ( sizeof...( Cs ) == rrggbb )
|
return QColor{ qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) };
|
||||||
{
|
|
||||||
const auto r = ( rgba >> ( 4 * 4 ) ) & 0xFF;
|
|
||||||
const auto g = ( rgba >> ( 2 * 4 ) ) & 0xFF;
|
|
||||||
const auto b = ( rgba >> ( 0 * 4 ) ) & 0xFF;
|
|
||||||
const auto a = 0xFF;
|
|
||||||
return { r, g, b, a };
|
|
||||||
}
|
|
||||||
if constexpr ( sizeof...( Cs ) == aarrggbb )
|
|
||||||
{
|
|
||||||
const auto r = ( rgba >> ( 4 * 4 ) ) & 0xFF;
|
|
||||||
const auto g = ( rgba >> ( 2 * 4 ) ) & 0xFF;
|
|
||||||
const auto b = ( rgba >> ( 0 * 4 ) ) & 0xFF;
|
|
||||||
const auto a = ( rgba >> ( 6 * 4 ) ) & 0xFF;
|
|
||||||
return { r, g, b, a };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,15 +35,15 @@ void QskRgbLiterals::parsing_data()
|
||||||
|
|
||||||
QTest::newRow( "" ) << "" << ( QRgb ) 0;
|
QTest::newRow( "" ) << "" << ( QRgb ) 0;
|
||||||
QTest::newRow( "#" ) << "#" << ( QRgb ) 0;
|
QTest::newRow( "#" ) << "#" << ( QRgb ) 0;
|
||||||
QTest::newRow( "#1" ) << "#1" << ( QRgb ) 0x1;
|
QTest::newRow( "#1" ) << "#1" << ( QRgb ) 0;
|
||||||
QTest::newRow( "#12" ) << "#12" << ( QRgb ) 0x12;
|
QTest::newRow( "#12" ) << "#12" << ( QRgb ) 0;
|
||||||
QTest::newRow( "#123" ) << "#123" << ( QRgb ) 0x123;
|
QTest::newRow( "#123" ) << "#123" << ( QRgb ) 0;
|
||||||
QTest::newRow( "#1234" ) << "#1234" << ( QRgb ) 0x1234;
|
QTest::newRow( "#1234" ) << "#1234" << ( QRgb ) 0;
|
||||||
QTest::newRow( "#12345" ) << "#12345" << ( QRgb ) 0x12345;
|
QTest::newRow( "#12345" ) << "#12345" << ( QRgb ) 0;
|
||||||
QTest::newRow( "#123456" ) << "#123456" << ( QRgb ) 0x123456;
|
QTest::newRow( "#123456" ) << "#123456" << ( QRgb ) 0xFF123456;
|
||||||
QTest::newRow( "#1234567" ) << "#1234567" << ( QRgb ) 0x1234567;
|
QTest::newRow( "#1234567" ) << "#1234567" << ( QRgb ) 0;
|
||||||
QTest::newRow( "#12345678" ) << "#12345678" << ( QRgb ) 0x12345678;
|
QTest::newRow( "#12345678" ) << "#12345678" << ( QRgb ) 0x12345678;
|
||||||
QTest::newRow( "#123456789" ) << "#123456789" << ( QRgb ) 0x23456789;
|
QTest::newRow( "#123456789" ) << "#123456789" << ( QRgb ) 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void QskRgbLiterals::parsing()
|
void QskRgbLiterals::parsing()
|
||||||
|
|
Loading…
Reference in New Issue