Statistics
| Branch: | Tag: | Revision:

root / UOLib / UHue.pas @ 119:66352054ce4d

History | View | Annotate | Download (5 kB)

1
(*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License, Version 1.0 only
6
 * (the "License").  You may not use this file except in compliance
7
 * with the License.
8
 *
9
 * You can obtain a copy of the license at
10
 * http://www.opensource.org/licenses/cddl1.php.
11
 * See the License for the specific language governing permissions
12
 * and limitations under the License.
13
 *
14
 * When distributing Covered Code, include this CDDL HEADER in each
15
 * file and include the License file at
16
 * http://www.opensource.org/licenses/cddl1.php.  If applicable,
17
 * add the following below this CDDL HEADER, with the fields enclosed
18
 * by brackets "[]" replaced with your own identifying * information:
19
 *      Portions Copyright [yyyy] [name of copyright owner]
20
 *
21
 * CDDL HEADER END
22
 *
23
 *
24
 *      Portions Copyright 2009 Andreas Schneider
25
 *)
26
unit UHue;
27
28
{$mode objfpc}{$H+}
29
30
interface
31
32
uses
33
  SysUtils, Classes, Graphics, UMulBlock;
34
35
type
36
37
  TColorTable = array[0..31] of Word;
38
39
  { THue }
40
41
  THue = class(TMulBlock)
42
    constructor Create(AData: TStream);
43
    function Clone: THue; override;
44
    function GetSize: Integer; override;
45
    procedure Write(AData: TStream); override;
46
  protected
47
    FColorTable: TColorTable;
48
    FTableStart: Word;
49
    FTableEnd: Word;
50
    FName: string;
51
    procedure SetName(AValue: string);
52
    function GetName: string;
53
  public
54
    property ColorTable: TColorTable read FColorTable write FColorTable;
55
    property TableStart: Word read FTableStart write FTableStart;
56
    property TableEnd: Word read FTableEnd write FTableEnd;
57
    property Name: string read GetName write SetName;
58
  end;
59
60
  THueEntries = array[0..7] of THue;
61
62
  { THueGroup }
63
64
  THueGroup = class(TMulBlock)
65
    constructor Create(AData: TStream);
66
    destructor Destroy; override;
67
    function Clone: THueGroup; override;
68
    function GetSize: Integer; override;
69
    procedure Write(AData: TStream); override;
70
  protected
71
    FHeader: LongWord;
72
    FHueEntries: THueEntries;
73
    function GetHueEntry(AIndex: Integer): THue;
74
    procedure SetHueEntry(AIndex: Integer; AValue: THue);
75
  public
76
    property Header: LongWord read FHeader write FHeader;
77
    property HueEntries[Index: Integer]: THue read GetHueEntry write SetHueEntry;
78
  end;
79
80
implementation
81
82
{ THue }
83
84
function THue.Clone: THue;
85
var
86
  i: Integer;
87
begin
88
  Result := THue.Create(nil);
89
  for i := 0 to 31 do
90
    Result.FColorTable[i] := FColorTable[i];
91
  Result.FTableStart := FTableStart;
92
  Result.FTableEnd := FTableEnd;
93
  Result.FName := FName;
94
end;
95
96
constructor THue.Create(AData: TStream);
97
var
98
  i: Integer;
99
  buffer: TMemoryStream;
100
  color: Word;
101
begin
102
  SetLength(FName, 20);
103
  if AData <> nil then
104
  begin
105
    buffer := TMemoryStream.Create;
106
    buffer.CopyFrom(AData, 88);
107
    buffer.Position := 0;
108
    for i := 0 to 31 do
109
    begin
110
      buffer.Read(color, SizeOf(Word));
111
      FColorTable[i] := color;
112
    end;
113
    buffer.Read(FTableStart, SizeOf(Word));
114
    buffer.Read(FTableEnd, SizeOf(Word));
115
    buffer.Read(PChar(FName)^, 20);
116
    buffer.Free;    
117
  end;
118
end;
119
120
function THue.GetName: string;
121
begin
122
  Result := Trim(FName);
123
end;
124
125
function THue.GetSize: Integer;
126
begin
127
  Result := 88;
128
end;
129
130
procedure THue.SetName(AValue: string);
131
begin
132
  FName := AValue;
133
  SetLength(FName, 20);
134
end;
135
136
procedure THue.Write(AData: TStream);
137
var
138
  i: Integer;
139
  color: Word;
140
begin
141
  SetLength(FName, 20);
142
  for i := 0 to 31 do
143
  begin
144
    color := FColorTable[i];
145
    AData.Write(color, SizeOf(Word));
146
  end;
147
  AData.Write(FTableStart, SizeOf(Word));
148
  AData.Write(FTableEnd, SizeOf(Word));
149
  AData.Write(PChar(FName)^, 20);
150
end;
151
152
{ THueGroup }
153
154
function THueGroup.Clone: THueGroup;
155
var
156
  i: Integer;
157
begin
158
  Result := THueGroup.Create(nil);
159
  Result.FHeader := FHeader;
160
  for i := 0 to 7 do
161
    Result.SetHueEntry(i, FHueEntries[i].Clone);
162
end;
163
164
constructor THueGroup.Create(AData: TStream);
165
var
166
  i: Integer;
167
  buffer: TMemoryStream;
168
begin
169
  if AData <> nil then
170
  begin
171
    buffer := TMemoryStream.Create;
172
    buffer.CopyFrom(AData, 708);
173
    buffer.Position := 0;
174
    buffer.Read(FHeader, SizeOf(LongWord));
175
  end else
176
    buffer := nil;
177
178
  for i := 0 to 7 do
179
    FHueEntries[i] := THue.Create(buffer);
180
181
  buffer.Free;
182
end;
183
184
destructor THueGroup.Destroy;
185
var
186
  i: Integer;
187
begin
188
  for i := 0 to 7 do
189
    FreeAndNil(FHueEntries[i]);
190
  inherited Destroy;
191
end;
192
193
function THueGroup.GetHueEntry(AIndex: Integer): THue;
194
begin
195
  Result := FHueEntries[AIndex];
196
end;
197
198
function THueGroup.GetSize: Integer;
199
begin
200
  Result := 708;
201
end;
202
203
procedure THueGroup.SetHueEntry(AIndex: Integer; AValue: THue);
204
begin
205
  FreeAndNil(FHueEntries[AIndex]);
206
  FHueEntries[AIndex] := AValue;
207
end;
208
209
procedure THueGroup.Write(AData: TStream);
210
var
211
  i: Integer;
212
begin
213
  AData.Write(FHeader, SizeOf(LongWord));
214
  for i := 0 to 7 do
215
    FHueEntries[i].Write(AData);
216
end;
217
218
end.
219