1 | unit UColorGray4;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UGGraphics, UFGraphics, Graphics;
|
---|
9 |
|
---|
10 | type
|
---|
11 | { TColorFormatGray4 }
|
---|
12 |
|
---|
13 | TColorFormatGray4 = class(TColorFormat)
|
---|
14 | constructor Create; override;
|
---|
15 | end;
|
---|
16 |
|
---|
17 | TColorGray4 = Byte;
|
---|
18 |
|
---|
19 | { TPixmapGray4 }
|
---|
20 |
|
---|
21 | TPixmapGray4 = class(TGPixmapBit<TColorGray4>)
|
---|
22 | function Gray4ToColor(Value: TColorGray4): TColor;
|
---|
23 | function ColorToGray4(Value: TColor): TColorGray4;
|
---|
24 | end;
|
---|
25 |
|
---|
26 | { TBColorGray4 }
|
---|
27 |
|
---|
28 | TBColorGray4 = class(TBColor)
|
---|
29 | Value: TColorGray4;
|
---|
30 | constructor Create(Color: TColorGray4);
|
---|
31 | procedure SetColorName(ColorName: TColorName); override;
|
---|
32 | procedure SetRandom; override;
|
---|
33 | procedure SetColor(Color: TColor); override;
|
---|
34 | end;
|
---|
35 |
|
---|
36 | { TBPixmapGray4 }
|
---|
37 |
|
---|
38 | TBPixmapGray4 = class(TBPixmap)
|
---|
39 | private
|
---|
40 | FillCallBack: TGetColorPos;
|
---|
41 | function FillGetColor(Position: TPoint): TColorGray4;
|
---|
42 | function NegativeFunc(Position: TPoint): TColorGray4;
|
---|
43 | protected
|
---|
44 | procedure SetSize(AValue: TPoint); override;
|
---|
45 | function GetPixel(X, Y: Integer): IBColor; override;
|
---|
46 | procedure SetPixel(X, Y: Integer; AValue: IBColor); override;
|
---|
47 | public
|
---|
48 | Pixmap: TPixmapGray4;
|
---|
49 | procedure Flip; override;
|
---|
50 | procedure Mirror; override;
|
---|
51 | procedure Negative; override;
|
---|
52 | procedure Fill(Color: IBColor); override;
|
---|
53 | procedure Fill(Func: TGetColorPos); override;
|
---|
54 | procedure Line(P1, P2: TPoint; Color: IBColor); override;
|
---|
55 | procedure PaintToCanvas(Canvas: TCanvas); override;
|
---|
56 | procedure PaintToCanvas(Canvas: TCanvas; Rect: TRect); override;
|
---|
57 | procedure PaintToBitmap(Bitmap: TBitmap; Rect: TRect); override;
|
---|
58 | procedure LoadFromCanvas(Canvas: TCanvas); override;
|
---|
59 | procedure LoadFromBitmap(Bitmap: TBitmap); override;
|
---|
60 | function GetDataSize: Integer; override;
|
---|
61 | constructor Create; override;
|
---|
62 | destructor Destroy; override;
|
---|
63 | end;
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 | implementation
|
---|
68 |
|
---|
69 | { TColorFormatGray4 }
|
---|
70 |
|
---|
71 | constructor TColorFormatGray4.Create;
|
---|
72 | begin
|
---|
73 | inherited;
|
---|
74 | Name := 'Gray 4-bit';
|
---|
75 | BitDepth := 4;
|
---|
76 | BackendColorClass := TBColorGray4;
|
---|
77 | BackendPixmapClass := TBPixmapGray4;
|
---|
78 | AddChannel('Gray', 0, 4);
|
---|
79 | end;
|
---|
80 |
|
---|
81 | { TPixmapGray4 }
|
---|
82 |
|
---|
83 | function TPixmapGray4.Gray4ToColor(Value: TColorGray4): TColor;
|
---|
84 | begin
|
---|
85 | Value := (Value and $f) * (255 div (16 - 1));
|
---|
86 | Result := (Value shl 16) or (Value shl 8) or (Value shl 0);
|
---|
87 | end;
|
---|
88 |
|
---|
89 | function TPixmapGray4.ColorToGray4(Value: TColor): TColorGray4;
|
---|
90 | begin
|
---|
91 | Result := Trunc((((Value shr 16) and $ff) + ((Value shr 8) and $ff) + ((Value shr 0) and $ff)) / $300 * 16);
|
---|
92 | end;
|
---|
93 |
|
---|
94 | { TBColorGray2 }
|
---|
95 |
|
---|
96 | constructor TBColorGray4.Create(Color: TColorGray4);
|
---|
97 | begin
|
---|
98 | Value := Color;
|
---|
99 | end;
|
---|
100 |
|
---|
101 | procedure TBColorGray4.SetColorName(ColorName: TColorName);
|
---|
102 | begin
|
---|
103 | case ColorName of
|
---|
104 | cnBlack: Value := 0;
|
---|
105 | cnGray: Value := 6;
|
---|
106 | cnSilver: Value := 10;
|
---|
107 | cnWhite: Value := 15;
|
---|
108 | else Value := 0;
|
---|
109 | end;
|
---|
110 | end;
|
---|
111 |
|
---|
112 | procedure TBColorGray4.SetRandom;
|
---|
113 | begin
|
---|
114 | Value := Random(16);
|
---|
115 | end;
|
---|
116 |
|
---|
117 | procedure TBColorGray4.SetColor(Color: TColor);
|
---|
118 | begin
|
---|
119 | Value := ((Color shr 0) and $ff + (Color shr 8) and $ff + (Color shr 16) and $ff) div (3 * 16);
|
---|
120 | end;
|
---|
121 |
|
---|
122 | { TBPixmapGray2 }
|
---|
123 |
|
---|
124 | function TBPixmapGray4.FillGetColor(Position: TPoint): TColorGray4;
|
---|
125 | begin
|
---|
126 | Result := (FillCallBack(Position) as TBColorGray4).Value;
|
---|
127 | end;
|
---|
128 |
|
---|
129 | function TBPixmapGray4.NegativeFunc(Position: TPoint): TColorGray4;
|
---|
130 | var
|
---|
131 | C: TColorGray4;
|
---|
132 | begin
|
---|
133 | C := Pixmap.Pixels[Position.X, Position.Y];
|
---|
134 | Result := 16 - (C and $f);
|
---|
135 | end;
|
---|
136 |
|
---|
137 | procedure TBPixmapGray4.SetSize(AValue: TPoint);
|
---|
138 | begin
|
---|
139 | inherited;
|
---|
140 | Pixmap.Size := AValue;
|
---|
141 | end;
|
---|
142 |
|
---|
143 | function TBPixmapGray4.GetPixel(X, Y: Integer): IBColor;
|
---|
144 | begin
|
---|
145 | Result := TBColorGray4.Create(Pixmap.Pixels[X, Y]);
|
---|
146 | end;
|
---|
147 |
|
---|
148 | procedure TBPixmapGray4.SetPixel(X, Y: Integer; AValue: IBColor);
|
---|
149 | begin
|
---|
150 | Pixmap.Pixels[X, Y] := (AValue as TBColorGray4).Value;
|
---|
151 | end;
|
---|
152 |
|
---|
153 | procedure TBPixmapGray4.Flip;
|
---|
154 | begin
|
---|
155 | Pixmap.Flip;
|
---|
156 | end;
|
---|
157 |
|
---|
158 | procedure TBPixmapGray4.Mirror;
|
---|
159 | begin
|
---|
160 | Pixmap.Mirror;
|
---|
161 | end;
|
---|
162 |
|
---|
163 | procedure TBPixmapGray4.Negative;
|
---|
164 | begin
|
---|
165 | Pixmap.Fill(NegativeFunc);
|
---|
166 | end;
|
---|
167 |
|
---|
168 | procedure TBPixmapGray4.Fill(Color: IBColor);
|
---|
169 | begin
|
---|
170 | if Color is TBColorGray4 then
|
---|
171 | Pixmap.Fill((Color as TBColorGray4).Value);
|
---|
172 | end;
|
---|
173 |
|
---|
174 | procedure TBPixmapGray4.Fill(Func: TGetColorPos);
|
---|
175 | begin
|
---|
176 | FillCallBack := Func;
|
---|
177 | Pixmap.Fill(FillGetColor);
|
---|
178 | end;
|
---|
179 |
|
---|
180 | procedure TBPixmapGray4.Line(P1, P2: TPoint; Color: IBColor);
|
---|
181 | begin
|
---|
182 | Pixmap.Canvas.Pen.Color := (Color as TBColorGray4).Value;
|
---|
183 | Pixmap.Canvas.Pen.MoveTo(P1);
|
---|
184 | Pixmap.Canvas.Pen.LineTo(P2);
|
---|
185 | end;
|
---|
186 |
|
---|
187 | procedure TBPixmapGray4.PaintToCanvas(Canvas: TCanvas);
|
---|
188 | begin
|
---|
189 | Pixmap.PaintToCanvas(Canvas, Pixmap.Gray4ToColor);
|
---|
190 | end;
|
---|
191 |
|
---|
192 | procedure TBPixmapGray4.PaintToCanvas(Canvas: TCanvas; Rect: TRect);
|
---|
193 | begin
|
---|
194 | Pixmap.PaintToCanvas(Canvas, Rect, Pixmap.Gray4ToColor);
|
---|
195 | end;
|
---|
196 |
|
---|
197 | procedure TBPixmapGray4.PaintToBitmap(Bitmap: TBitmap; Rect: TRect);
|
---|
198 | begin
|
---|
199 | Pixmap.PaintToBitmap(Bitmap, Rect, Pixmap.Gray4ToColor);
|
---|
200 | end;
|
---|
201 |
|
---|
202 | procedure TBPixmapGray4.LoadFromCanvas(Canvas: TCanvas);
|
---|
203 | begin
|
---|
204 | Pixmap.LoadFromCanvas(Canvas, Pixmap.ColorToGray4);
|
---|
205 | end;
|
---|
206 |
|
---|
207 | procedure TBPixmapGray4.LoadFromBitmap(Bitmap: TBitmap);
|
---|
208 | begin
|
---|
209 | Pixmap.LoadFromBitmap(Bitmap, Pixmap.ColorToGray4);
|
---|
210 | end;
|
---|
211 |
|
---|
212 | function TBPixmapGray4.GetDataSize: Integer;
|
---|
213 | begin
|
---|
214 | Result := Pixmap.GetDataSize;
|
---|
215 | end;
|
---|
216 |
|
---|
217 | constructor TBPixmapGray4.Create;
|
---|
218 | begin
|
---|
219 | Pixmap := TPixmapGray4.Create;
|
---|
220 | Pixmap.BitsPerPixel := 4;
|
---|
221 | end;
|
---|
222 |
|
---|
223 | destructor TBPixmapGray4.Destroy;
|
---|
224 | begin
|
---|
225 | FreeAndNil(Pixmap);
|
---|
226 | inherited;
|
---|
227 | end;
|
---|
228 |
|
---|
229 |
|
---|
230 | end.
|
---|
231 |
|
---|