Skip to content

Commit

Permalink
Support #RRGGBBAA style color codes in QgsSymbolLayerV2Utils color pa…
Browse files Browse the repository at this point in the history
…rsing
  • Loading branch information
nyalldawson committed Sep 10, 2014
1 parent ad8093e commit e7f3c82
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
Expand Down
20 changes: 20 additions & 0 deletions tests/src/core/testqgsstylev2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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 ) );
Expand Down

0 comments on commit e7f3c82

Please sign in to comment.