-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfpspalette.pas
416 lines (366 loc) · 14.1 KB
/
fpspalette.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
{ fpsPalette }
{@@ ----------------------------------------------------------------------------
Palette support for fpspreadsheet file formats
AUTHORS: Werner Pamler, Felipe Monteiro de Carvalho, Reinier Olislagers
LICENSE: See the file COPYING.modifiedLGPL.txt, included in the Lazarus
distribution, for details about the license.
-------------------------------------------------------------------------------}
unit fpsPalette;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fpstypes, fpspreadsheet;
type
{ TsPalette }
TsPalette = class
private
FColors: array of TsColor;
function GetColor(AIndex: Integer): TsColor;
procedure SetColor(AIndex: Integer; AColor: TsColor);
public
constructor Create;
procedure AddBuiltinColors; virtual;
function AddColor(AColor: TsColor; ABigEndian: Boolean = false): Integer;
procedure AddExcelColors;
function AddUniqueColor(AColor: TsColor; ABigEndian: Boolean = false): Integer;
procedure Clear;
procedure CollectFromWorkbook(AWorkbook: TsWorkbook);
function ColorUsedInWorkbook(APaletteIndex: Integer; AWorkbook: TsWorkbook): Boolean;
function FindClosestColorIndex(AColor: TsColor; AMaxPaletteCount: Integer = -1): Integer;
function FindColor(AColor: TsColor; AMaxPaletteCount: Integer = -1): Integer;
function Count: Integer;
procedure Trim(AMaxSize: Integer);
procedure UseColors(const AColors: array of TsColor; ABigEndian: Boolean = false);
property Colors[AIndex: Integer]: TsColor read GetColor write SetColor; default;
end;
procedure MakeLEPalette(var AColors: array of TsColor);
implementation
uses
fpsutils;
{@@ ----------------------------------------------------------------------------
If a palette is coded as big-endian (e.g. by copying the rgb values from
the OpenOffice documentation) the palette values can be converted by means
of this procedure to little-endian which is required by fpspreadsheet.
@param AColors Color array to be converted.
After conversion, its color values are replaced.
-------------------------------------------------------------------------------}
procedure MakeLEPalette(var AColors: array of TsColor);
var
i: Integer;
begin
for i := 0 to High(AColors) do
AColors[i] := LongRGBToExcelPhysical(AColors[i])
end;
{@@ ----------------------------------------------------------------------------
Constructor of the palette: initializes the color array
-------------------------------------------------------------------------------}
constructor TsPalette.Create;
begin
inherited;
SetLength(FColors, 0);
end;
{@@ ----------------------------------------------------------------------------
Adds an rgb color value to the palette and returns the palette index
of the new color.
Existing colors are not checked.
If ABigEndian is TRUE then the rgb values are assumed to be in big endian
order (r = high byte).
By default, rgb is in little-endian order (r = low byte)
-------------------------------------------------------------------------------}
function TsPalette.AddColor(AColor: TsColor; ABigEndian: Boolean = false): Integer;
begin
if ABigEndian then
AColor := LongRGBToExcelPhysical(AColor);
SetLength(FColors, Length(FColors) + 1);
FColors[High(FColors)] := AColor;
Result := High(FColors);
end;
{@@ ----------------------------------------------------------------------------
Adds the built-in colors
-------------------------------------------------------------------------------}
procedure TsPalette.AddBuiltinColors;
begin
AddColor(scBlack); // 0
AddColor(scWhite); // 1
AddColor(scRed); // 2
AddColor(scGreen); // 3
AddColor(scBlue); // 4
AddColor(scYellow); // 5
AddColor(scMagenta); // 6
AddColor(scCyan); // 7
end;
{@@ ----------------------------------------------------------------------------
Adds the standard palette of Excel 8
NOTE: To get the full Excel8 palette call this after AddBuiltinColors
-------------------------------------------------------------------------------}
procedure TsPalette.AddExcelColors;
begin
AddColor($000000, true); // $08: EGA black
AddColor($FFFFFF, true); // $09: EGA white
AddColor($FF0000, true); // $0A: EGA red
AddColor($00FF00, true); // $0B: EGA green
AddColor($0000FF, true); // $0C: EGA blue
AddColor($FFFF00, true); // $0D: EGA yellow
AddColor($FF00FF, true); // $0E: EGA magenta
AddColor($00FFFF, true); // $0F: EGA cyan
AddColor($800000, true); // $10: EGA dark red
AddColor($008000, true); // $11: EGA dark green
AddColor($000080, true); // $12: EGA dark blue
AddColor($808000, true); // $13: EGA olive
AddColor($800080, true); // $14: EGA purple
AddColor($008080, true); // $15: EGA teal
AddColor($C0C0C0, true); // $16: EGA silver
AddColor($808080, true); // $17: EGA gray
AddColor($9999FF, true); // $18:
AddColor($993366, true); // $19:
AddColor($FFFFCC, true); // $1A:
AddColor($CCFFFF, true); // $1B:
AddColor($660066, true); // $1C:
AddColor($FF8080, true); // $1D:
AddColor($0066CC, true); // $1E:
AddColor($CCCCFF, true); // $1F:
AddColor($000080, true); // $20:
AddColor($FF00FF, true); // $21:
AddColor($FFFF00, true); // $22:
AddColor($00FFFF, true); // $23:
AddColor($800080, true); // $24:
AddColor($800000, true); // $25:
AddColor($008080, true); // $26:
AddColor($0000FF, true); // $27:
AddColor($00CCFF, true); // $28:
AddColor($CCFFFF, true); // $29:
AddColor($CCFFCC, true); // $2A:
AddColor($FFFF99, true); // $2B:
AddColor($99CCFF, true); // $2C:
AddColor($FF99CC, true); // $2D:
AddColor($CC99FF, true); // $2E:
AddColor($FFCC99, true); // $2F:
AddColor($3366FF, true); // $30:
AddColor($33CCCC, true); // $31:
AddColor($99CC00, true); // $32:
AddColor($FFCC00, true); // $33:
AddColor($FF9900, true); // $34:
AddColor($FF6600, true); // $35:
AddColor($666699, true); // $36:
AddColor($969696, true); // $37:
AddColor($003366, true); // $38:
AddColor($339966, true); // $39:
AddColor($003300, true); // $3A:
AddColor($333300, true); // $3B:
AddColor($993300, true); // $3C:
AddColor($993366, true); // $3D:
AddColor($333399, true); // $3E:
AddColor($333333, true); // $3F:
end;
{@@ ----------------------------------------------------------------------------
Adds the specified color to the palette if it does not yet exist.
Returns the palette index of the new or existing color
-------------------------------------------------------------------------------}
function TsPalette.AddUniqueColor(AColor: TsColor;
ABigEndian: Boolean = false): Integer;
begin
if ABigEndian then
AColor := LongRGBToExcelPhysical(AColor);
Result := FindColor(AColor);
if Result = -1 then result := AddColor(AColor);
end;
{@@ ----------------------------------------------------------------------------
Clears the palette
-------------------------------------------------------------------------------}
procedure TsPalette.Clear;
begin
SetLength(FColors, 0);
end;
{@@ ----------------------------------------------------------------------------
Collects the colors used in the specified workbook
-------------------------------------------------------------------------------}
procedure TsPalette.CollectFromWorkbook(AWorkbook: TsWorkbook);
var
i: Integer;
sheet: TsWorksheet;
cell: PCell;
fmt: TsCellFormat;
fnt: TsFont;
cb: TsCellBorder;
begin
for i:=0 to AWorkbook.GetWorksheetCount-1 do
begin
sheet := AWorkbook.GetWorksheetByIndex(i);
for cell in sheet.Cells do begin
fmt := sheet.ReadCellFormat(cell);
if (uffBackground in fmt.UsedFormattingFields) then
begin
AddUniqueColor(fmt.Background.BgColor);
AddUniqueColor(fmt.Background.FgColor);
end;
if (uffFont in fmt.UsedFormattingFields) then
begin
fnt := AWorkbook.GetFont(fmt.FontIndex);
AddUniqueColor(fnt.Color);
end;
if (uffBorder in fmt.UsedFormattingFields) then
begin
for cb in TsCellBorder do
if (cb in fmt.Border) then
AddUniqueColor(fmt.BorderStyles[cb].Color);
end;
end;
end;
end;
{@@ ----------------------------------------------------------------------------
Checks whether a given color is used somewhere within the entire workbook
@param APaletteIndex Palette index of the color
@result True if the color is used by at least one cell, false if not.
-------------------------------------------------------------------------------}
function TsPalette.ColorUsedInWorkbook(APaletteIndex: Integer;
AWorkbook: TsWorkbook): Boolean;
var
sheet: TsWorksheet;
cell: PCell;
i: Integer;
fnt: TsFont;
b: TsCellBorder;
fmt: PsCellFormat;
color: TsColor;
begin
color := GetColor(APaletteIndex);
if (color = scNotDefined) or (AWorkbook = nil) then
exit(false);
Result := true;
for i:=0 to AWorkbook.GetWorksheetCount-1 do
begin
sheet := AWorkbook.GetWorksheetByIndex(i);
for cell in sheet.Cells do
begin
fmt := AWorkbook.GetPointerToCellFormat(cell^.FormatIndex);
if (uffBackground in fmt^.UsedFormattingFields) then
begin
if fmt^.Background.BgColor = color then exit;
if fmt^.Background.FgColor = color then exit;
end;
if (uffBorder in fmt^.UsedFormattingFields) then
for b in TsCellBorders do
if (b in fmt^.Border) and (fmt^.BorderStyles[b].Color = color) then
exit;
if (uffFont in fmt^.UsedFormattingFields) then
begin
fnt := AWorkbook.GetFont(fmt^.FontIndex);
if fnt.Color = color then
exit;
end;
end;
end;
Result := false;
end;
{@@ ----------------------------------------------------------------------------
Finds the palette color index which points to a color that is "closest" to a
given color. "Close" means here smallest length of the rgb-difference vector.
@param AColor Rgb color value to be considered
@param AMaxPaletteCount Number of palette entries considered. Example:
BIFF5/BIFF8 can write only 64 colors, i.e
AMaxPaletteCount = 64
@return Palette index of the color closest to AColor
-------------------------------------------------------------------------------}
function TsPalette.FindClosestColorIndex(AColor: TsColor;
AMaxPaletteCount: Integer = -1): Integer;
type
TRGBA = record r,g,b,a: Byte end;
var
rgb: TRGBA;
rgb0: TRGBA absolute AColor;
dist: Double;
minDist: Double;
i: Integer;
n: Integer;
begin
Result := -1;
minDist := 1E108;
n := Length(FColors);
if AMaxPaletteCount > n then n := AMaxPaletteCount;
for i := 0 to n - 1 do
begin
rgb := TRGBA(GetColor(i));
dist := sqr(rgb.r - rgb0.r) + sqr(rgb.g - rgb0.g) + sqr(rgb.b - rgb0.b);
if dist < minDist then
begin
Result := i;
minDist := dist;
end;
end;
end;
{@@ ----------------------------------------------------------------------------
Finds the palette color index which belongs to the specified color.
Returns -1 if the color is not contained in the palette.
@param AColor Rgb color value to be considered
@param AMaxPaletteCount Number of palette entries considered. Example:
BIFF5/BIFF8 can write only 64 colors, i.e
AMaxPaletteCount = 64
@return Palette index of AColor
-------------------------------------------------------------------------------}
function TsPalette.FindColor(AColor: TsColor;
AMaxPaletteCount: Integer = -1): Integer;
var
n: Integer;
begin
n := Length(FColors);
if AMaxPaletteCount > n then n := AMaxPaletteCount;
for Result := 0 to n - 1 do
if GetColor(Result) = AColor then
exit;
Result := -1;
end;
{@@ ----------------------------------------------------------------------------
Reads the rgb color for the given index from the palette.
Can be type-cast to TColor for usage in GUI applications.
@param AIndex Index of the color considered
@return A number containing the rgb components in little-endian notation.
-------------------------------------------------------------------------------}
function TsPalette.GetColor(AIndex: Integer): TsColor;
begin
if (AIndex >= 0) and (AIndex < Length(FColors)) then
Result := FColors[AIndex]
else
Result := scNotDefined;
end;
{@@ ----------------------------------------------------------------------------
Returns the number of palette colors
-------------------------------------------------------------------------------}
function TsPalette.Count: Integer;
begin
Result := Length(FColors);
end;
{@@ ----------------------------------------------------------------------------
Replaces a color value of the palette by a new value.
The color must be given in little-endian notation (ABGR, with A=0).
@param AIndex Palette index of the color to be replaced
@param AColor Number containing the rgb components of the new color
-------------------------------------------------------------------------------}
procedure TsPalette.SetColor(AIndex: Integer; AColor: TsColor);
begin
if (AIndex >= 0) and (AIndex < Length(FColors)) then
FColors[AIndex] := AColor;
end;
{@@ ----------------------------------------------------------------------------
Trims the size of the palette
-------------------------------------------------------------------------------}
procedure TsPalette.Trim(AMaxSize: Integer);
begin
if Length(FColors) > AMaxSize then
SetLength(FColors, AMaxSize);
end;
{@@ ----------------------------------------------------------------------------
Uses the color array to with "APalette" points in the palette.
If ABigEndian is true it is assumed that the input colors are specified in
big-endian notation, i.e. "blue" in the low-value byte.
-------------------------------------------------------------------------------}
procedure TsPalette.UseColors(const AColors: array of TsColor; ABigEndian: Boolean = false);
var
i: Integer;
begin
SetLength(FColors, High(AColors)+1);
if ABigEndian then
for i:=0 to High(AColors) do FColors[i] := LongRGBToExcelPhysical(AColors[i])
else
for i:=0 to High(AColors) do FColors[i] := AColors[i];
end;
end.