Skip to content
Permalink
Browse files

Support #RRGGBBAA style color codes in QgsSymbolLayerV2Utils color pa…

…rsing
  • Loading branch information
nyalldawson committed Sep 10, 2014
1 parent ad8093e commit e7f3c823a5eae4a2f75a99fb2fecf30593b3f141
Showing with 37 additions and 0 deletions.
  1. +17 −0 src/core/symbology-ng/qgssymbollayerv2utils.cpp
  2. +20 −0 tests/src/core/testqgsstylev2.cpp
@@ -3070,6 +3070,23 @@ QColor QgsSymbolLayerV2Utils::parseColorWithAlpha( const QString colorStr, bool
}
}

//color in hex format, with alpha
QRegExp hexColorAlphaRx( "^\\s*#?([0-9a-fA-F]{6})([0-9a-fA-F]{2})\\s*$" );
if ( hexColorAlphaRx.indexIn( colorStr ) != -1 )
{
QString hexColor = hexColorAlphaRx.cap( 1 );
parsedColor.setNamedColor( QString( "#" ) + hexColor );
bool alphaOk;
int alphaHex = hexColorAlphaRx.cap( 2 ).toInt( &alphaOk, 16 );

if ( parsedColor.isValid() && alphaOk )
{
parsedColor.setAlpha( alphaHex );
containsAlpha = true;
return parsedColor;
}
}

if ( !strictEval )
{
//color in hex format, without #
@@ -230,6 +230,16 @@ void TestStyleV2::testParseColor()
colorTests.insert( "00ff000", qMakePair( QColor(), false ) );
colorTests.insert( "fff", qMakePair( QColor( 255, 255, 255 ), false ) );
colorTests.insert( "fff0", qMakePair( QColor(), false ) );

// hex rrggbbaa colors
colorTests.insert( "#ff00ffaa", qMakePair( QColor( 255, 0, 255, 170 ), true ) );
colorTests.insert( "#99AA0099", qMakePair( QColor( 153, 170, 0, 153 ), true ) );
colorTests.insert( "#GG0000aa", qMakePair( QColor(), false ) );
colorTests.insert( "00000000", qMakePair( QColor( 0, 0, 0, 0 ), true ) );
colorTests.insert( "00ff0011", qMakePair( QColor( 0, 255, 0, 17 ), true ) );
colorTests.insert( "00gg0011", qMakePair( QColor( ), false ) );
colorTests.insert( "00ff00000", qMakePair( QColor(), false ) );

colorTests.insert( "0,0,0", qMakePair( QColor( 0, 0, 0 ), false ) );
colorTests.insert( "127,60,0", qMakePair( QColor( 127, 60, 0 ), false ) );
colorTests.insert( "255,255,255", qMakePair( QColor( 255, 255, 255 ), false ) );
@@ -301,6 +311,16 @@ void TestStyleV2::testParseColorList()
colorTests.insert( "00ff000", QColor() );
//colorTests.insert( "fff", QColor( 255, 255, 255 ) );
colorTests.insert( "fff0", QColor() );

// hex rrggbbaa colors
colorTests.insert( "#ff00ffaa", QColor( 255, 0, 255, 170 ) );
colorTests.insert( "#99AA0099", QColor( 153, 170, 0, 153 ) );
colorTests.insert( "#GG0000aa", QColor() );
colorTests.insert( "00000000", QColor( 0, 0, 0, 0 ) );
colorTests.insert( "00ff0011", QColor( 0, 255, 0, 17 ) );
colorTests.insert( "00gg0011", QColor( ) );
colorTests.insert( "00ff00000", QColor() );

colorTests.insert( "0,0,0", QColor( 0, 0, 0 ) );
colorTests.insert( "127,60,0", QColor( 127, 60, 0 ) );
colorTests.insert( "255,255,255", QColor( 255, 255, 255 ) );

0 comments on commit e7f3c82

Please sign in to comment.
You can’t perform that action at this time.