Skip to content

Commit

Permalink
[core] Implement a color encoding/decoding that can handle CMYK
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Dec 29, 2023
1 parent 709c6a3 commit 0cda8f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/core/symbology/qgssymbollayerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "qgssymbol.h"
#include "qgscolorramp.h"
#include "qgscolorrampimpl.h"
#include "qgscolorutils.h"
#include "qgsexpression.h"
#include "qgsexpressionnode.h"
#include "qgspainteffect.h"
Expand Down Expand Up @@ -63,7 +64,7 @@

QString QgsSymbolLayerUtils::encodeColor( const QColor &color )
{
return QStringLiteral( "%1,%2,%3,%4" ).arg( color.red() ).arg( color.green() ).arg( color.blue() ).arg( color.alpha() );
return QgsColorUtils::colorToString( color );
}

QColor QgsSymbolLayerUtils::decodeColor( const QString &str )
Expand All @@ -73,16 +74,16 @@ QColor QgsSymbolLayerUtils::decodeColor( const QString &str )
{
return QColor( str );
}
int red, green, blue, alpha;
red = lst[0].toInt();
green = lst[1].toInt();
blue = lst[2].toInt();
alpha = 255;
if ( lst.count() > 3 )
else if ( lst.count() < 5 )
{
alpha = lst[3].toInt();
int red, green, blue, alpha;
red = lst[0].toInt();
green = lst[1].toInt();
blue = lst[2].toInt();
alpha = lst.count() == 4 ? lst[3].toInt() : 255;
return QColor( red, green, blue, alpha );
}
return QColor( red, green, blue, alpha );
return QgsColorUtils::colorFromString( str );
}

QString QgsSymbolLayerUtils::encodeSldAlpha( int alpha )
Expand Down
33 changes: 33 additions & 0 deletions tests/src/python/test_qgssymbollayerutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,39 @@ def test_font_marker_load(self):
font_marker = QgsSymbolLayerUtils.loadSymbol(elem, QgsReadWriteContext())
self.assertEqual(font_marker.symbolLayers()[0].character(), "()")

def test_color_encoding_decoding(self):
"""
Test the encoding and decoding of QColor objects
"""

rgb = QColor(255, 0, 255)
encodedRgb = QgsSymbolLayerUtils.encodeColor(rgb)
self.assertEqual(encodedRgb, "255,0,255,255,rgb:1,0,1,1")
decodedRgb = QgsSymbolLayerUtils.decodeColor(encodedRgb)
self.assertEqual(decodedRgb, rgb)
self.assertEqual(decodedRgb.spec(), QColor.Rgb)

cmyk = QColor.fromCmykF(1, 0, 0, 0)
encodedCmyk = QgsSymbolLayerUtils.encodeColor(cmyk)
self.assertEqual(encodedCmyk, "0,255,255,255,cmyk:1,0,0,0,1")
decodedCmyk = QgsSymbolLayerUtils.decodeColor(encodedCmyk)
self.assertEqual(decodedCmyk, cmyk)
self.assertEqual(decodedCmyk.spec(), QColor.Cmyk)

hsv = QColor.fromHsvF(1, 0, 0)
encodedHsv = QgsSymbolLayerUtils.encodeColor(hsv)
self.assertEqual(encodedHsv, "0,0,0,255,hsv:1,0,0,1")
decodedHsv = QgsSymbolLayerUtils.decodeColor(encodedHsv)
self.assertEqual(decodedHsv, hsv)
self.assertEqual(decodedHsv.spec(), QColor.Hsv)

hsl = QColor.fromHslF(1, 0, 0)
encodedHsl = QgsSymbolLayerUtils.encodeColor(hsl)
self.assertEqual(encodedHsl, "0,0,0,255,hsl:0,0,0,1")
decodedHsl = QgsSymbolLayerUtils.decodeColor(encodedHsl)
self.assertEqual(decodedHsl, hsl)
self.assertEqual(decodedHsl.spec(), QColor.Hsl)


if __name__ == '__main__':
unittest.main()

0 comments on commit 0cda8f3

Please sign in to comment.