Skip to content

Commit 9312276

Browse files
committed
Prepare color palette matching for dxf
1 parent dc62376 commit 9312276

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/core/qgsdxfexport.cpp

+38-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <QIODevice>
2424
#include <QTextStream>
2525

26-
QgsDxfExport::QgsDxfExport()
26+
QgsDxfExport::QgsDxfExport(): mSymbologyScaleDenominator( 1.0 ), mSymbologyExport( NoSymbology )
2727
{
2828
}
2929

@@ -351,10 +351,47 @@ double QgsDxfExport::scaleToMapUnits( double value, QgsSymbolV2::OutputUnit symb
351351

352352
int QgsDxfExport::colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer )
353353
{
354+
if ( !symbolLayer )
355+
{
356+
return 0;
357+
}
358+
359+
354360
return 5; //todo...
355361
}
356362

357363
double QgsDxfExport::widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer )
358364
{
359365
return 50; //todo...
360366
}
367+
368+
int QgsDxfExport::closestMatch( QRgb pixel, const QVector<QRgb>& palette )
369+
{
370+
int idx = 0;
371+
int current_distance = INT_MAX;
372+
for ( int i = 0; i < palette.size(); ++i )
373+
{
374+
int dist = pixel_distance( pixel, palette.at( i ) );
375+
if ( dist < current_distance )
376+
{
377+
current_distance = dist;
378+
idx = i;
379+
}
380+
}
381+
return idx;
382+
}
383+
384+
int QgsDxfExport::pixel_distance( QRgb p1, QRgb p2 )
385+
{
386+
int r1 = qRed( p1 );
387+
int g1 = qGreen( p1 );
388+
int b1 = qBlue( p1 );
389+
int a1 = qAlpha( p1 );
390+
391+
int r2 = qRed( p2 );
392+
int g2 = qGreen( p2 );
393+
int b2 = qBlue( p2 );
394+
int a2 = qAlpha( p2 );
395+
396+
return abs( r1 - r2 ) + abs( g1 - g2 ) + abs( b1 - b2 ) + abs( a1 - a2 );
397+
}

src/core/qgsdxfexport.h

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "qgsgeometry.h"
2222
#include "qgssymbolv2.h"
23+
#include <QColor>
2324
#include <QList>
2425

2526
class QgsMapLayer;
@@ -78,6 +79,11 @@ class QgsDxfExport
7879
//returns dxf palette index from symbol layer color
7980
int colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer );
8081
double widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer );
82+
83+
//functions for dxf palette
84+
static int closestMatch( QRgb pixel, const QVector<QRgb>& palette );
85+
static int pixel_distance( QRgb p1, QRgb p2 );
86+
8187
};
8288

8389
#endif // QGSDXFEXPORT_H

0 commit comments

Comments
 (0)