Skip to content

Commit

Permalink
Try to find closest color in dxf palette
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Nov 3, 2013
1 parent f98ecf7 commit eee9a49
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 31 deletions.
306 changes: 278 additions & 28 deletions src/core/qgsdxfexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,274 @@
#include "qgsvectordataprovider.h"
#include "qgspoint.h"
#include "qgsrendererv2.h"
#include "qgssymbollayerv2.h"
#include "qgsvectorlayer.h"
#include <QIODevice>
#include <QTextStream>

//dxf color palette
double QgsDxfExport::mDxfColors[][3] =
{
{0, 0, 0}, // unused
{1, 0, 0}, // 1
{1, 1, 0},
{0, 1, 0},
{0, 1, 1},
{0, 0, 1},
{1, 0, 1},
{1, 1, 1}, // black or white
{0.5, 0.5, 0.5},
{0.75, 0.75, 0.75},
{1, 0, 0}, // 10
{1, 0.5, 0.5},
{0.65, 0, 0},
{0.65, 0.325, 0.325},
{0.5, 0, 0},
{0.5, 0.25, 0.25},
{0.3, 0, 0},
{0.3, 0.15, 0.15},
{0.15, 0, 0},
{0.15, 0.075, 0.075},
{1, 0.25, 0}, // 20
{1, 0.625, 0.5},
{0.65, 0.1625, 0},
{0.65, 0.4063, 0.325},
{0.5, 0.125, 0},
{0.5, 0.3125, 0.25},
{0.3, 0.075, 0},
{0.3, 0.1875, 0.15},
{0.15, 0.0375, 0},
{0.15, 0.0938, 0.075},
{1, 0.5, 0}, // 30
{1, 0.75, 0.5},
{0.65, 0.325, 0},
{0.65, 0.4875, 0.325},
{0.5, 0.25, 0},
{0.5, 0.375, 0.25},
{0.3, 0.15, 0},
{0.3, 0.225, 0.15},
{0.15, 0.075, 0},
{0.15, 0.1125, 0.075},
{1, 0.75, 0}, // 40
{1, 0.875, 0.5},
{0.65, 0.4875, 0},
{0.65, 0.5688, 0.325},
{0.5, 0.375, 0},
{0.5, 0.4375, 0.25},
{0.3, 0.225, 0},
{0.3, 0.2625, 0.15},
{0.15, 0.1125, 0},
{0.15, 0.1313, 0.075},
{1, 1, 0}, // 50
{1, 1, 0.5},
{0.65, 0.65, 0},
{0.65, 0.65, 0.325},
{0.5, 0.5, 0},
{0.5, 0.5, 0.25},
{0.3, 0.3, 0},
{0.3, 0.3, 0.15},
{0.15, 0.15, 0},
{0.15, 0.15, 0.075},
{0.75, 1, 0}, // 60
{0.875, 1, 0.5},
{0.4875, 0.65, 0},
{0.5688, 0.65, 0.325},
{0.375, 0.5, 0},
{0.4375, 0.5, 0.25},
{0.225, 0.3, 0},
{0.2625, 0.3, 0.15},
{0.1125, 0.15, 0},
{0.1313, 0.15, 0.075},
{0.5, 1, 0}, // 70
{0.75, 1, 0.5},
{0.325, 0.65, 0},
{0.4875, 0.65, 0.325},
{0.25, 0.5, 0},
{0.375, 0.5, 0.25},
{0.15, 0.3, 0},
{0.225, 0.3, 0.15},
{0.075, 0.15, 0},
{0.1125, 0.15, 0.075},
{0.25, 1, 0}, // 80
{0.625, 1, 0.5},
{0.1625, 0.65, 0},
{0.4063, 0.65, 0.325},
{0.125, 0.5, 0},
{0.3125, 0.5, 0.25},
{0.075, 0.3, 0},
{0.1875, 0.3, 0.15},
{0.0375, 0.15, 0},
{0.0938, 0.15, 0.075},
{0, 1, 0}, // 90
{0.5, 1, 0.5},
{0, 0.65, 0},
{0.325, 0.65, 0.325},
{0, 0.5, 0},
{0.25, 0.5, 0.25},
{0, 0.3, 0},
{0.15, 0.3, 0.15},
{0, 0.15, 0},
{0.075, 0.15, 0.075},
{0, 1, 0.25}, // 100
{0.5, 1, 0.625},
{0, 0.65, 0.1625},
{0.325, 0.65, 0.4063},
{0, 0.5, 0.125},
{0.25, 0.5, 0.3125},
{0, 0.3, 0.075},
{0.15, 0.3, 0.1875},
{0, 0.15, 0.0375},
{0.075, 0.15, 0.0938},
{0, 1, 0.5}, // 110
{0.5, 1, 0.75},
{0, 0.65, 0.325},
{0.325, 0.65, 0.4875},
{0, 0.5, 0.25},
{0.25, 0.5, 0.375},
{0, 0.3, 0.15},
{0.15, 0.3, 0.225},
{0, 0.15, 0.075},
{0.075, 0.15, 0.1125},
{0, 1, 0.75}, // 120
{0.5, 1, 0.875},
{0, 0.65, 0.4875},
{0.325, 0.65, 0.5688},
{0, 0.5, 0.375},
{0.25, 0.5, 0.4375},
{0, 0.3, 0.225},
{0.15, 0.3, 0.2625},
{0, 0.15, 0.1125},
{0.075, 0.15, 0.1313},
{0, 1, 1}, // 130
{0.5, 1, 1},
{0, 0.65, 0.65},
{0.325, 0.65, 0.65},
{0, 0.5, 0.5},
{0.25, 0.5, 0.5},
{0, 0.3, 0.3},
{0.15, 0.3, 0.3},
{0, 0.15, 0.15},
{0.075, 0.15, 0.15},
{0, 0.75, 1}, // 140
{0.5, 0.875, 1},
{0, 0.4875, 0.65},
{0.325, 0.5688, 0.65},
{0, 0.375, 0.5},
{0.25, 0.4375, 0.5},
{0, 0.225, 0.3},
{0.15, 0.2625, 0.3},
{0, 0.1125, 0.15},
{0.075, 0.1313, 0.15},
{0, 0.5, 1}, // 150
{0.5, 0.75, 1},
{0, 0.325, 0.65},
{0.325, 0.4875, 0.65},
{0, 0.25, 0.5},
{0.25, 0.375, 0.5},
{0, 0.15, 0.3},
{0.15, 0.225, 0.3},
{0, 0.075, 0.15},
{0.075, 0.1125, 0.15},
{0, 0.25, 1}, // 160
{0.5, 0.625, 1},
{0, 0.1625, 0.65},
{0.325, 0.4063, 0.65},
{0, 0.125, 0.5},
{0.25, 0.3125, 0.5},
{0, 0.075, 0.3},
{0.15, 0.1875, 0.3},
{0, 0.0375, 0.15},
{0.075, 0.0938, 0.15},
{0, 0, 1}, // 170
{0.5, 0.5, 1},
{0, 0, 0.65},
{0.325, 0.325, 0.65},
{0, 0, 0.5},
{0.25, 0.25, 0.5},
{0, 0, 0.3},
{0.15, 0.15, 0.3},
{0, 0, 0.15},
{0.075, 0.075, 0.15},
{0.25, 0, 1}, // 180
{0.625, 0.5, 1},
{0.1625, 0, 0.65},
{0.4063, 0.325, 0.65},
{0.125, 0, 0.5},
{0.3125, 0.25, 0.5},
{0.075, 0, 0.3},
{0.1875, 0.15, 0.3},
{0.0375, 0, 0.15},
{0.0938, 0.075, 0.15},
{0.5, 0, 1}, // 190
{0.75, 0.5, 1},
{0.325, 0, 0.65},
{0.4875, 0.325, 0.65},
{0.25, 0, 0.5},
{0.375, 0.25, 0.5},
{0.15, 0, 0.3},
{0.225, 0.15, 0.3},
{0.075, 0, 0.15},
{0.1125, 0.075, 0.15},
{0.75, 0, 1}, // 200
{0.875, 0.5, 1},
{0.4875, 0, 0.65},
{0.5688, 0.325, 0.65},
{0.375, 0, 0.5},
{0.4375, 0.25, 0.5},
{0.225, 0, 0.3},
{0.2625, 0.15, 0.3},
{0.1125, 0, 0.15},
{0.1313, 0.075, 0.15},
{1, 0, 1}, // 210
{1, 0.5, 1},
{0.65, 0, 0.65},
{0.65, 0.325, 0.65},
{0.5, 0, 0.5},
{0.5, 0.25, 0.5},
{0.3, 0, 0.3},
{0.3, 0.15, 0.3},
{0.15, 0, 0.15},
{0.15, 0.075, 0.15},
{1, 0, 0.75}, // 220
{1, 0.5, 0.875},
{0.65, 0, 0.4875},
{0.65, 0.325, 0.5688},
{0.5, 0, 0.375},
{0.5, 0.25, 0.4375},
{0.3, 0, 0.225},
{0.3, 0.15, 0.2625},
{0.15, 0, 0.1125},
{0.15, 0.075, 0.1313},
{1, 0, 0.5}, // 230
{1, 0.5, 0.75},
{0.65, 0, 0.325},
{0.65, 0.325, 0.4875},
{0.5, 0, 0.25},
{0.5, 0.25, 0.375},
{0.3, 0, 0.15},
{0.3, 0.15, 0.225},
{0.15, 0, 0.075},
{0.15, 0.075, 0.1125},
{1, 0, 0.25}, // 240
{1, 0.5, 0.625},
{0.65, 0, 0.1625},
{0.65, 0.325, 0.4063},
{0.5, 0, 0.125},
{0.5, 0.25, 0.3125},
{0.3, 0, 0.075},
{0.3, 0.15, 0.1875},
{0.15, 0, 0.0375},
{0.15, 0.075, 0.0938},
{0.33, 0.33, 0.33}, // 250
{0.464, 0.464, 0.464},
{0.598, 0.598, 0.598},
{0.732, 0.732, 0.732},
{0.866, 0.866, 0.866},
{1, 1, 1} // 255
};

QgsDxfExport::QgsDxfExport(): mSymbologyScaleDenominator( 1.0 ), mSymbologyExport( NoSymbology )
{
initColorPalette();
}

QgsDxfExport::~QgsDxfExport()
Expand Down Expand Up @@ -366,22 +627,22 @@ int QgsDxfExport::colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer )
return 0;
}


return 1; //todo...
QColor c = symbolLayer->color();
return closestColorMatch( c.rgba() );
}

double QgsDxfExport::widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer )
{
return 50; //todo...
}

int QgsDxfExport::closestMatch( QRgb pixel, const QVector<QRgb>& palette )
int QgsDxfExport::closestColorMatch( QRgb pixel )
{
int idx = 0;
int current_distance = INT_MAX;
for ( int i = 0; i < palette.size(); ++i )
for ( int i = 1; i < 256; ++i )
{
int dist = pixel_distance( pixel, palette.at( i ) );
int dist = color_distance( pixel, i );
if ( dist < current_distance )
{
current_distance = dist;
Expand All @@ -391,31 +652,20 @@ int QgsDxfExport::closestMatch( QRgb pixel, const QVector<QRgb>& palette )
return idx;
}

int QgsDxfExport::pixel_distance( QRgb p1, QRgb p2 )
int QgsDxfExport::color_distance( QRgb p1, int index )
{
int r1 = qRed( p1 );
int g1 = qGreen( p1 );
int b1 = qBlue( p1 );
int a1 = qAlpha( p1 );

int r2 = qRed( p2 );
int g2 = qGreen( p2 );
int b2 = qBlue( p2 );
int a2 = qAlpha( p2 );
if ( index > 255 || index < 0 )
{
return 0;
}

return abs( r1 - r2 ) + abs( g1 - g2 ) + abs( b1 - b2 ) + abs( a1 - a2 );
double redDiff = qRed( p1 ) - mDxfColors[index][0] * 255;
double greenDiff = qGreen( p1 ) - mDxfColors[index][1] * 255;
double blueDiff = qBlue( p1 ) - mDxfColors[index][2] * 255;
return ( redDiff * redDiff + greenDiff * greenDiff + blueDiff * blueDiff );
}

void QgsDxfExport::initColorPalette()
QRgb QgsDxfExport::createRgbEntry( qreal r, qreal g, qreal b )
{
mDxfColorPalette.resize( 256 );
mDxfColorPalette[1] = qRgb( 255, 0, 0 );
mDxfColorPalette[2] = qRgb( 255, 255, 0 );
mDxfColorPalette[3] = qRgb( 0, 255, 0 );
mDxfColorPalette[4] = qRgb( 0, 255, 255 );
mDxfColorPalette[5] = qRgb( 0, 0, 255 );
mDxfColorPalette[6] = qRgb( 255, 0, 255 );
mDxfColorPalette[7] = qRgb( 255, 255, 255 );
mDxfColorPalette[8] = qRgb( 255, 255, 255 );
mDxfColorPalette[9] = qRgb( 255, 255, 255 );
return QColor::fromRgbF( r, g, b ).rgb();
}
8 changes: 5 additions & 3 deletions src/core/qgsdxfexport.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ class QgsDxfExport
double widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer );

//functions for dxf palette
void initColorPalette();
static int closestMatch( QRgb pixel, const QVector<QRgb>& palette );
static int pixel_distance( QRgb p1, QRgb p2 );
static int closestColorMatch( QRgb pixel );
static int color_distance( QRgb p1, int index );
static QRgb createRgbEntry( qreal r, qreal g, qreal b );

static double mDxfColors[][3];

};

Expand Down

0 comments on commit eee9a49

Please sign in to comment.