Skip to content

Commit eee9a49

Browse files
committedNov 3, 2013
Try to find closest color in dxf palette
1 parent f98ecf7 commit eee9a49

File tree

2 files changed

+283
-31
lines changed

2 files changed

+283
-31
lines changed
 

‎src/core/qgsdxfexport.cpp

Lines changed: 278 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,274 @@
1919
#include "qgsvectordataprovider.h"
2020
#include "qgspoint.h"
2121
#include "qgsrendererv2.h"
22+
#include "qgssymbollayerv2.h"
2223
#include "qgsvectorlayer.h"
2324
#include <QIODevice>
2425
#include <QTextStream>
2526

27+
//dxf color palette
28+
double QgsDxfExport::mDxfColors[][3] =
29+
{
30+
{0, 0, 0}, // unused
31+
{1, 0, 0}, // 1
32+
{1, 1, 0},
33+
{0, 1, 0},
34+
{0, 1, 1},
35+
{0, 0, 1},
36+
{1, 0, 1},
37+
{1, 1, 1}, // black or white
38+
{0.5, 0.5, 0.5},
39+
{0.75, 0.75, 0.75},
40+
{1, 0, 0}, // 10
41+
{1, 0.5, 0.5},
42+
{0.65, 0, 0},
43+
{0.65, 0.325, 0.325},
44+
{0.5, 0, 0},
45+
{0.5, 0.25, 0.25},
46+
{0.3, 0, 0},
47+
{0.3, 0.15, 0.15},
48+
{0.15, 0, 0},
49+
{0.15, 0.075, 0.075},
50+
{1, 0.25, 0}, // 20
51+
{1, 0.625, 0.5},
52+
{0.65, 0.1625, 0},
53+
{0.65, 0.4063, 0.325},
54+
{0.5, 0.125, 0},
55+
{0.5, 0.3125, 0.25},
56+
{0.3, 0.075, 0},
57+
{0.3, 0.1875, 0.15},
58+
{0.15, 0.0375, 0},
59+
{0.15, 0.0938, 0.075},
60+
{1, 0.5, 0}, // 30
61+
{1, 0.75, 0.5},
62+
{0.65, 0.325, 0},
63+
{0.65, 0.4875, 0.325},
64+
{0.5, 0.25, 0},
65+
{0.5, 0.375, 0.25},
66+
{0.3, 0.15, 0},
67+
{0.3, 0.225, 0.15},
68+
{0.15, 0.075, 0},
69+
{0.15, 0.1125, 0.075},
70+
{1, 0.75, 0}, // 40
71+
{1, 0.875, 0.5},
72+
{0.65, 0.4875, 0},
73+
{0.65, 0.5688, 0.325},
74+
{0.5, 0.375, 0},
75+
{0.5, 0.4375, 0.25},
76+
{0.3, 0.225, 0},
77+
{0.3, 0.2625, 0.15},
78+
{0.15, 0.1125, 0},
79+
{0.15, 0.1313, 0.075},
80+
{1, 1, 0}, // 50
81+
{1, 1, 0.5},
82+
{0.65, 0.65, 0},
83+
{0.65, 0.65, 0.325},
84+
{0.5, 0.5, 0},
85+
{0.5, 0.5, 0.25},
86+
{0.3, 0.3, 0},
87+
{0.3, 0.3, 0.15},
88+
{0.15, 0.15, 0},
89+
{0.15, 0.15, 0.075},
90+
{0.75, 1, 0}, // 60
91+
{0.875, 1, 0.5},
92+
{0.4875, 0.65, 0},
93+
{0.5688, 0.65, 0.325},
94+
{0.375, 0.5, 0},
95+
{0.4375, 0.5, 0.25},
96+
{0.225, 0.3, 0},
97+
{0.2625, 0.3, 0.15},
98+
{0.1125, 0.15, 0},
99+
{0.1313, 0.15, 0.075},
100+
{0.5, 1, 0}, // 70
101+
{0.75, 1, 0.5},
102+
{0.325, 0.65, 0},
103+
{0.4875, 0.65, 0.325},
104+
{0.25, 0.5, 0},
105+
{0.375, 0.5, 0.25},
106+
{0.15, 0.3, 0},
107+
{0.225, 0.3, 0.15},
108+
{0.075, 0.15, 0},
109+
{0.1125, 0.15, 0.075},
110+
{0.25, 1, 0}, // 80
111+
{0.625, 1, 0.5},
112+
{0.1625, 0.65, 0},
113+
{0.4063, 0.65, 0.325},
114+
{0.125, 0.5, 0},
115+
{0.3125, 0.5, 0.25},
116+
{0.075, 0.3, 0},
117+
{0.1875, 0.3, 0.15},
118+
{0.0375, 0.15, 0},
119+
{0.0938, 0.15, 0.075},
120+
{0, 1, 0}, // 90
121+
{0.5, 1, 0.5},
122+
{0, 0.65, 0},
123+
{0.325, 0.65, 0.325},
124+
{0, 0.5, 0},
125+
{0.25, 0.5, 0.25},
126+
{0, 0.3, 0},
127+
{0.15, 0.3, 0.15},
128+
{0, 0.15, 0},
129+
{0.075, 0.15, 0.075},
130+
{0, 1, 0.25}, // 100
131+
{0.5, 1, 0.625},
132+
{0, 0.65, 0.1625},
133+
{0.325, 0.65, 0.4063},
134+
{0, 0.5, 0.125},
135+
{0.25, 0.5, 0.3125},
136+
{0, 0.3, 0.075},
137+
{0.15, 0.3, 0.1875},
138+
{0, 0.15, 0.0375},
139+
{0.075, 0.15, 0.0938},
140+
{0, 1, 0.5}, // 110
141+
{0.5, 1, 0.75},
142+
{0, 0.65, 0.325},
143+
{0.325, 0.65, 0.4875},
144+
{0, 0.5, 0.25},
145+
{0.25, 0.5, 0.375},
146+
{0, 0.3, 0.15},
147+
{0.15, 0.3, 0.225},
148+
{0, 0.15, 0.075},
149+
{0.075, 0.15, 0.1125},
150+
{0, 1, 0.75}, // 120
151+
{0.5, 1, 0.875},
152+
{0, 0.65, 0.4875},
153+
{0.325, 0.65, 0.5688},
154+
{0, 0.5, 0.375},
155+
{0.25, 0.5, 0.4375},
156+
{0, 0.3, 0.225},
157+
{0.15, 0.3, 0.2625},
158+
{0, 0.15, 0.1125},
159+
{0.075, 0.15, 0.1313},
160+
{0, 1, 1}, // 130
161+
{0.5, 1, 1},
162+
{0, 0.65, 0.65},
163+
{0.325, 0.65, 0.65},
164+
{0, 0.5, 0.5},
165+
{0.25, 0.5, 0.5},
166+
{0, 0.3, 0.3},
167+
{0.15, 0.3, 0.3},
168+
{0, 0.15, 0.15},
169+
{0.075, 0.15, 0.15},
170+
{0, 0.75, 1}, // 140
171+
{0.5, 0.875, 1},
172+
{0, 0.4875, 0.65},
173+
{0.325, 0.5688, 0.65},
174+
{0, 0.375, 0.5},
175+
{0.25, 0.4375, 0.5},
176+
{0, 0.225, 0.3},
177+
{0.15, 0.2625, 0.3},
178+
{0, 0.1125, 0.15},
179+
{0.075, 0.1313, 0.15},
180+
{0, 0.5, 1}, // 150
181+
{0.5, 0.75, 1},
182+
{0, 0.325, 0.65},
183+
{0.325, 0.4875, 0.65},
184+
{0, 0.25, 0.5},
185+
{0.25, 0.375, 0.5},
186+
{0, 0.15, 0.3},
187+
{0.15, 0.225, 0.3},
188+
{0, 0.075, 0.15},
189+
{0.075, 0.1125, 0.15},
190+
{0, 0.25, 1}, // 160
191+
{0.5, 0.625, 1},
192+
{0, 0.1625, 0.65},
193+
{0.325, 0.4063, 0.65},
194+
{0, 0.125, 0.5},
195+
{0.25, 0.3125, 0.5},
196+
{0, 0.075, 0.3},
197+
{0.15, 0.1875, 0.3},
198+
{0, 0.0375, 0.15},
199+
{0.075, 0.0938, 0.15},
200+
{0, 0, 1}, // 170
201+
{0.5, 0.5, 1},
202+
{0, 0, 0.65},
203+
{0.325, 0.325, 0.65},
204+
{0, 0, 0.5},
205+
{0.25, 0.25, 0.5},
206+
{0, 0, 0.3},
207+
{0.15, 0.15, 0.3},
208+
{0, 0, 0.15},
209+
{0.075, 0.075, 0.15},
210+
{0.25, 0, 1}, // 180
211+
{0.625, 0.5, 1},
212+
{0.1625, 0, 0.65},
213+
{0.4063, 0.325, 0.65},
214+
{0.125, 0, 0.5},
215+
{0.3125, 0.25, 0.5},
216+
{0.075, 0, 0.3},
217+
{0.1875, 0.15, 0.3},
218+
{0.0375, 0, 0.15},
219+
{0.0938, 0.075, 0.15},
220+
{0.5, 0, 1}, // 190
221+
{0.75, 0.5, 1},
222+
{0.325, 0, 0.65},
223+
{0.4875, 0.325, 0.65},
224+
{0.25, 0, 0.5},
225+
{0.375, 0.25, 0.5},
226+
{0.15, 0, 0.3},
227+
{0.225, 0.15, 0.3},
228+
{0.075, 0, 0.15},
229+
{0.1125, 0.075, 0.15},
230+
{0.75, 0, 1}, // 200
231+
{0.875, 0.5, 1},
232+
{0.4875, 0, 0.65},
233+
{0.5688, 0.325, 0.65},
234+
{0.375, 0, 0.5},
235+
{0.4375, 0.25, 0.5},
236+
{0.225, 0, 0.3},
237+
{0.2625, 0.15, 0.3},
238+
{0.1125, 0, 0.15},
239+
{0.1313, 0.075, 0.15},
240+
{1, 0, 1}, // 210
241+
{1, 0.5, 1},
242+
{0.65, 0, 0.65},
243+
{0.65, 0.325, 0.65},
244+
{0.5, 0, 0.5},
245+
{0.5, 0.25, 0.5},
246+
{0.3, 0, 0.3},
247+
{0.3, 0.15, 0.3},
248+
{0.15, 0, 0.15},
249+
{0.15, 0.075, 0.15},
250+
{1, 0, 0.75}, // 220
251+
{1, 0.5, 0.875},
252+
{0.65, 0, 0.4875},
253+
{0.65, 0.325, 0.5688},
254+
{0.5, 0, 0.375},
255+
{0.5, 0.25, 0.4375},
256+
{0.3, 0, 0.225},
257+
{0.3, 0.15, 0.2625},
258+
{0.15, 0, 0.1125},
259+
{0.15, 0.075, 0.1313},
260+
{1, 0, 0.5}, // 230
261+
{1, 0.5, 0.75},
262+
{0.65, 0, 0.325},
263+
{0.65, 0.325, 0.4875},
264+
{0.5, 0, 0.25},
265+
{0.5, 0.25, 0.375},
266+
{0.3, 0, 0.15},
267+
{0.3, 0.15, 0.225},
268+
{0.15, 0, 0.075},
269+
{0.15, 0.075, 0.1125},
270+
{1, 0, 0.25}, // 240
271+
{1, 0.5, 0.625},
272+
{0.65, 0, 0.1625},
273+
{0.65, 0.325, 0.4063},
274+
{0.5, 0, 0.125},
275+
{0.5, 0.25, 0.3125},
276+
{0.3, 0, 0.075},
277+
{0.3, 0.15, 0.1875},
278+
{0.15, 0, 0.0375},
279+
{0.15, 0.075, 0.0938},
280+
{0.33, 0.33, 0.33}, // 250
281+
{0.464, 0.464, 0.464},
282+
{0.598, 0.598, 0.598},
283+
{0.732, 0.732, 0.732},
284+
{0.866, 0.866, 0.866},
285+
{1, 1, 1} // 255
286+
};
287+
26288
QgsDxfExport::QgsDxfExport(): mSymbologyScaleDenominator( 1.0 ), mSymbologyExport( NoSymbology )
27289
{
28-
initColorPalette();
29290
}
30291

31292
QgsDxfExport::~QgsDxfExport()
@@ -366,22 +627,22 @@ int QgsDxfExport::colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer )
366627
return 0;
367628
}
368629

369-
370-
return 1; //todo...
630+
QColor c = symbolLayer->color();
631+
return closestColorMatch( c.rgba() );
371632
}
372633

373634
double QgsDxfExport::widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer )
374635
{
375636
return 50; //todo...
376637
}
377638

378-
int QgsDxfExport::closestMatch( QRgb pixel, const QVector<QRgb>& palette )
639+
int QgsDxfExport::closestColorMatch( QRgb pixel )
379640
{
380641
int idx = 0;
381642
int current_distance = INT_MAX;
382-
for ( int i = 0; i < palette.size(); ++i )
643+
for ( int i = 1; i < 256; ++i )
383644
{
384-
int dist = pixel_distance( pixel, palette.at( i ) );
645+
int dist = color_distance( pixel, i );
385646
if ( dist < current_distance )
386647
{
387648
current_distance = dist;
@@ -391,31 +652,20 @@ int QgsDxfExport::closestMatch( QRgb pixel, const QVector<QRgb>& palette )
391652
return idx;
392653
}
393654

394-
int QgsDxfExport::pixel_distance( QRgb p1, QRgb p2 )
655+
int QgsDxfExport::color_distance( QRgb p1, int index )
395656
{
396-
int r1 = qRed( p1 );
397-
int g1 = qGreen( p1 );
398-
int b1 = qBlue( p1 );
399-
int a1 = qAlpha( p1 );
400-
401-
int r2 = qRed( p2 );
402-
int g2 = qGreen( p2 );
403-
int b2 = qBlue( p2 );
404-
int a2 = qAlpha( p2 );
657+
if ( index > 255 || index < 0 )
658+
{
659+
return 0;
660+
}
405661

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

409-
void QgsDxfExport::initColorPalette()
668+
QRgb QgsDxfExport::createRgbEntry( qreal r, qreal g, qreal b )
410669
{
411-
mDxfColorPalette.resize( 256 );
412-
mDxfColorPalette[1] = qRgb( 255, 0, 0 );
413-
mDxfColorPalette[2] = qRgb( 255, 255, 0 );
414-
mDxfColorPalette[3] = qRgb( 0, 255, 0 );
415-
mDxfColorPalette[4] = qRgb( 0, 255, 255 );
416-
mDxfColorPalette[5] = qRgb( 0, 0, 255 );
417-
mDxfColorPalette[6] = qRgb( 255, 0, 255 );
418-
mDxfColorPalette[7] = qRgb( 255, 255, 255 );
419-
mDxfColorPalette[8] = qRgb( 255, 255, 255 );
420-
mDxfColorPalette[9] = qRgb( 255, 255, 255 );
670+
return QColor::fromRgbF( r, g, b ).rgb();
421671
}

‎src/core/qgsdxfexport.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ class QgsDxfExport
8383
double widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer );
8484

8585
//functions for dxf palette
86-
void initColorPalette();
87-
static int closestMatch( QRgb pixel, const QVector<QRgb>& palette );
88-
static int pixel_distance( QRgb p1, QRgb p2 );
86+
static int closestColorMatch( QRgb pixel );
87+
static int color_distance( QRgb p1, int index );
88+
static QRgb createRgbEntry( qreal r, qreal g, qreal b );
89+
90+
static double mDxfColors[][3];
8991

9092
};
9193

0 commit comments

Comments
 (0)
Please sign in to comment.