Skip to content

Commit e7f3c82

Browse files
committedSep 10, 2014
Support #RRGGBBAA style color codes in QgsSymbolLayerV2Utils color parsing
1 parent ad8093e commit e7f3c82

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

‎src/core/symbology-ng/qgssymbollayerv2utils.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3070,6 +3070,23 @@ QColor QgsSymbolLayerV2Utils::parseColorWithAlpha( const QString colorStr, bool
30703070
}
30713071
}
30723072

3073+
//color in hex format, with alpha
3074+
QRegExp hexColorAlphaRx( "^\\s*#?([0-9a-fA-F]{6})([0-9a-fA-F]{2})\\s*$" );
3075+
if ( hexColorAlphaRx.indexIn( colorStr ) != -1 )
3076+
{
3077+
QString hexColor = hexColorAlphaRx.cap( 1 );
3078+
parsedColor.setNamedColor( QString( "#" ) + hexColor );
3079+
bool alphaOk;
3080+
int alphaHex = hexColorAlphaRx.cap( 2 ).toInt( &alphaOk, 16 );
3081+
3082+
if ( parsedColor.isValid() && alphaOk )
3083+
{
3084+
parsedColor.setAlpha( alphaHex );
3085+
containsAlpha = true;
3086+
return parsedColor;
3087+
}
3088+
}
3089+
30733090
if ( !strictEval )
30743091
{
30753092
//color in hex format, without #

‎tests/src/core/testqgsstylev2.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ void TestStyleV2::testParseColor()
230230
colorTests.insert( "00ff000", qMakePair( QColor(), false ) );
231231
colorTests.insert( "fff", qMakePair( QColor( 255, 255, 255 ), false ) );
232232
colorTests.insert( "fff0", qMakePair( QColor(), false ) );
233+
234+
// hex rrggbbaa colors
235+
colorTests.insert( "#ff00ffaa", qMakePair( QColor( 255, 0, 255, 170 ), true ) );
236+
colorTests.insert( "#99AA0099", qMakePair( QColor( 153, 170, 0, 153 ), true ) );
237+
colorTests.insert( "#GG0000aa", qMakePair( QColor(), false ) );
238+
colorTests.insert( "00000000", qMakePair( QColor( 0, 0, 0, 0 ), true ) );
239+
colorTests.insert( "00ff0011", qMakePair( QColor( 0, 255, 0, 17 ), true ) );
240+
colorTests.insert( "00gg0011", qMakePair( QColor( ), false ) );
241+
colorTests.insert( "00ff00000", qMakePair( QColor(), false ) );
242+
233243
colorTests.insert( "0,0,0", qMakePair( QColor( 0, 0, 0 ), false ) );
234244
colorTests.insert( "127,60,0", qMakePair( QColor( 127, 60, 0 ), false ) );
235245
colorTests.insert( "255,255,255", qMakePair( QColor( 255, 255, 255 ), false ) );
@@ -301,6 +311,16 @@ void TestStyleV2::testParseColorList()
301311
colorTests.insert( "00ff000", QColor() );
302312
//colorTests.insert( "fff", QColor( 255, 255, 255 ) );
303313
colorTests.insert( "fff0", QColor() );
314+
315+
// hex rrggbbaa colors
316+
colorTests.insert( "#ff00ffaa", QColor( 255, 0, 255, 170 ) );
317+
colorTests.insert( "#99AA0099", QColor( 153, 170, 0, 153 ) );
318+
colorTests.insert( "#GG0000aa", QColor() );
319+
colorTests.insert( "00000000", QColor( 0, 0, 0, 0 ) );
320+
colorTests.insert( "00ff0011", QColor( 0, 255, 0, 17 ) );
321+
colorTests.insert( "00gg0011", QColor( ) );
322+
colorTests.insert( "00ff00000", QColor() );
323+
304324
colorTests.insert( "0,0,0", QColor( 0, 0, 0 ) );
305325
colorTests.insert( "127,60,0", QColor( 127, 60, 0 ) );
306326
colorTests.insert( "255,255,255", QColor( 255, 255, 255 ) );

0 commit comments

Comments
 (0)
Please sign in to comment.