fppalette.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by the Free Pascal development team
  4. TFPPalette implementation.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. { TFPPalette }
  12. constructor TFPPalette.create (ACount : integer);
  13. begin
  14. inherited create;
  15. if aCount > 0 then
  16. getmem (FData, sizeof(TFPColor)*ACount)
  17. else
  18. FData := nil;
  19. FCapacity := ACount;
  20. SetCount (0);
  21. end;
  22. destructor TFPPalette.destroy;
  23. begin
  24. if FCapacity > 0 then
  25. freemem (FData);
  26. inherited;
  27. end;
  28. procedure TFPPalette.Build (Img : TFPCustomImage);
  29. var x,y : integer;
  30. begin
  31. if (Img.Palette <> self) then
  32. begin
  33. Count := 0;
  34. for x := 0 to img.width-1 do
  35. for y := 0 to img.height-1 do
  36. IndexOf(img[x,y]);
  37. end;
  38. end;
  39. procedure TFPPalette.Copy(APalette: TFPPalette);
  40. var
  41. x: integer;
  42. begin
  43. if (APalette <> Self) then
  44. begin
  45. Self.Clear;
  46. for x := 0 to APalette.Count - 1 do
  47. Add(APalette.Color[x])
  48. end;
  49. end;
  50. procedure TFPPalette.Merge (pal : TFPPalette);
  51. var r : integer;
  52. begin
  53. for r := 0 to pal.count-1 do
  54. IndexOf (pal[r]);
  55. end;
  56. procedure TFPPalette.CheckIndex (index:integer);
  57. begin
  58. if (index >= FCount) or (index < 0) then
  59. FPImgError (StrInvalidIndex,[ErrorText[StrPalette],index]);
  60. end;
  61. function TFPPalette.Add (const Value:TFPColor) : integer;
  62. begin
  63. result := FCount;
  64. inc (FCount);
  65. if FCount > FCapacity then
  66. EnlargeData;
  67. FData^[result] := Value;
  68. end;
  69. procedure TFPPalette.SetColor (index:integer; const Value:TFPColor);
  70. begin
  71. if index = FCount then
  72. Add (Value)
  73. else
  74. begin
  75. CheckIndex (index);
  76. FData^[index] := Value;
  77. end;
  78. end;
  79. function TFPPalette.GetColor (index:integer) : TFPColor;
  80. begin
  81. CheckIndex (index);
  82. result := FData^[index];
  83. end;
  84. function TFPPalette.GetCount : integer;
  85. begin
  86. result := FCount;
  87. end;
  88. procedure TFPPalette.EnlargeData;
  89. var old : integer;
  90. NewData : PFPColorArray;
  91. begin
  92. old := FCapacity;
  93. if FCapacity <= 16 then
  94. FCapacity := 32
  95. else if FCapacity <= 128 then
  96. FCapacity := 256
  97. else
  98. // MG: changed to exponential growth
  99. inc (FCapacity, FCapacity);
  100. GetMem (NewData, sizeof(TFPColor)*FCapacity);
  101. if old > 0 then
  102. begin
  103. move (FData^[0], NewData^[0], sizeof(TFPColor)*FCount);
  104. FreeMem (FData);
  105. end;
  106. FData := NewData;
  107. end;
  108. procedure TFPPalette.SetCount (Value:integer);
  109. var
  110. O : integer;
  111. begin
  112. if Value <> FCount then
  113. begin
  114. if Value > FCapacity then
  115. begin
  116. FCapacity := Value+8;
  117. Reallocmem(FData,sizeof(TFPColor)*FCapacity);
  118. end;
  119. for o := FCount to Value-1 do
  120. FData^[o] := colBlack;
  121. FCount := Value;
  122. end;
  123. end;
  124. procedure TFPPalette.SetCapacity (ind : Integer);
  125. var o : Integer;
  126. begin
  127. if ind<count then ind:=count;
  128. if ind<>fcapacity then
  129. begin
  130. fcapacity:=ind;
  131. Reallocmem(FData,sizeof(TFPColor)*FCapacity);
  132. end;
  133. if ind>count then
  134. begin
  135. for o := FCount to ind-1 do
  136. FData^[o] := colBlack;
  137. end;
  138. end;
  139. function TFPPalette.IndexOf (const AColor:TFPColor) : integer;
  140. begin
  141. result := FCount;
  142. repeat
  143. dec (result);
  144. until (result < 0) or (FData^[result]=AColor);
  145. if result < 0 then
  146. result := Add (AColor);
  147. end;
  148. procedure TFPPalette.Clear;
  149. begin
  150. SetCount (0);
  151. end;
  152. { Functions to create standard palettes, by Giulio Bernardi 2005 }
  153. { A simple 1 bit black and white palette }
  154. function CreateBlackAndWhitePalette : TFPPalette;
  155. var fppal : TFPPalette;
  156. Col : TFPColor;
  157. begin
  158. fppal:=TFPPalette.Create(2);
  159. Col.Alpha:=AlphaOpaque;
  160. Col.Red:=$FFFF; Col.Green:=$FFFF; Col.Blue:=$FFFF;
  161. fppal.Color[0]:=Col;
  162. Col.Red:=$0000; Col.Green:=$0000; Col.Blue:=$0000;
  163. fppal.Color[1]:=Col;
  164. Result:=fppal;
  165. end;
  166. { The "standard" netscape 216-color palette (aka: web safe palette) }
  167. function CreateWebSafePalette : TFPPalette;
  168. var Col : TFPColor;
  169. i : integer;
  170. fppal : TFPPalette;
  171. begin
  172. fppal:=TFPPalette.Create(216);
  173. Col.Alpha:=AlphaOpaque;
  174. i:=0;
  175. Col.Red:=$FFFF;
  176. while true do
  177. begin
  178. Col.Green:=$FFFF;
  179. while true do
  180. begin
  181. Col.Blue:=$FFFF;
  182. while true do
  183. begin
  184. fppal.Color[i]:=Col;
  185. if Col.Blue=0 then break;
  186. dec(Col.Blue,$3333);
  187. end;
  188. if Col.Green=0 then break;
  189. dec(Col.Green,$3333);
  190. end;
  191. if Col.Red=0 then break;
  192. dec(Col.Red,$3333);
  193. end;
  194. Result:=fppal;
  195. end;
  196. { A grayscale palette. Not very useful. }
  197. function CreateGrayScalePalette : TFPPalette;
  198. var Col : TFPColor;
  199. i : integer;
  200. fppal : TFPPalette;
  201. begin
  202. fppal:=TFPPalette.Create(256);
  203. Col.Alpha:=AlphaOpaque;
  204. for i:=0 to $FF do
  205. begin
  206. Col.Red:=i;
  207. Col.Red:=(Col.Red shl 8) + Col.Red;
  208. Col.Green:=Col.Red;
  209. Col.Blue:=Col.Red;
  210. fppal.Color[i]:=Col;
  211. end;
  212. Result:=fppal;
  213. end;
  214. { Standard VGA 16 color palette. }
  215. function CreateVGAPalette : TFPPalette;
  216. var fppal : TFPPalette;
  217. begin
  218. fppal:=TFPPalette.Create(16);
  219. fppal.Color[0]:=colBlack;
  220. fppal.Color[1]:=colNavy;
  221. fppal.Color[2]:=colBlue;
  222. fppal.Color[3]:=colMaroon;
  223. fppal.Color[4]:=colPurple;
  224. fppal.Color[5]:=colDkGreen;
  225. fppal.Color[6]:=colRed;
  226. fppal.Color[7]:=colTeal;
  227. fppal.Color[8]:=colFuchsia;
  228. fppal.Color[9]:=colOlive;
  229. fppal.Color[10]:=colGray;
  230. fppal.Color[11]:=colLime;
  231. fppal.Color[12]:=colAqua;
  232. fppal.Color[13]:=colSilver;
  233. fppal.Color[14]:=colYellow;
  234. fppal.Color[15]:=colWhite;
  235. Result:=fppal;
  236. end;