fppalette.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.Merge (pal : TFPPalette);
  40. var r : integer;
  41. begin
  42. for r := 0 to pal.count-1 do
  43. IndexOf (pal[r]);
  44. end;
  45. procedure TFPPalette.CheckIndex (index:integer);
  46. begin
  47. if (index >= FCount) or (index < 0) then
  48. FPImgError (StrInvalidIndex,[ErrorText[StrPalette],index]);
  49. end;
  50. function TFPPalette.Add (const Value:TFPColor) : integer;
  51. begin
  52. result := FCount;
  53. inc (FCount);
  54. if FCount > FCapacity then
  55. EnlargeData;
  56. FData^[result] := Value;
  57. end;
  58. procedure TFPPalette.SetColor (index:integer; const Value:TFPColor);
  59. begin
  60. if index = FCount then
  61. Add (Value)
  62. else
  63. begin
  64. CheckIndex (index);
  65. FData^[index] := Value;
  66. end;
  67. end;
  68. function TFPPalette.GetColor (index:integer) : TFPColor;
  69. begin
  70. CheckIndex (index);
  71. result := FData^[index];
  72. end;
  73. function TFPPalette.GetCount : integer;
  74. begin
  75. result := FCount;
  76. end;
  77. procedure TFPPalette.EnlargeData;
  78. var old : integer;
  79. NewData : PFPColorArray;
  80. begin
  81. old := FCapacity;
  82. if FCapacity <= 16 then
  83. FCapacity := 32
  84. else if FCapacity <= 128 then
  85. FCapacity := 256
  86. else
  87. // MG: changed to exponential growth
  88. inc (FCapacity, FCapacity);
  89. GetMem (NewData, sizeof(TFPColor)*FCapacity);
  90. if old > 0 then
  91. begin
  92. move (FData^[0], NewData^[0], sizeof(TFPColor)*FCount);
  93. FreeMem (FData);
  94. end;
  95. FData := NewData;
  96. end;
  97. procedure TFPPalette.SetCount (Value:integer);
  98. var NewData : PFPColorArray;
  99. O : integer;
  100. begin
  101. if Value <> FCount then
  102. begin
  103. if Value > FCapacity then
  104. begin
  105. O := FCapacity;
  106. FCapacity := Value + 8;
  107. if FCapacity > 0 then
  108. GetMem (NewData, sizeof(TFPColor)*FCapacity)
  109. else
  110. FData := nil;
  111. move (FData^, NewData^, sizeof(TFPColor)*FCount);
  112. if O > 0 then
  113. FreeMem (FData);
  114. FData := NewData;
  115. end;
  116. for o := FCount to Value-1 do
  117. FData^[o] := colBlack;
  118. FCount := Value;
  119. end;
  120. end;
  121. function TFPPalette.IndexOf (const AColor:TFPColor) : integer;
  122. begin
  123. result := FCount;
  124. repeat
  125. dec (result);
  126. until (result < 0) or (FData^[result]=AColor);
  127. if result < 0 then
  128. result := Add (AColor);
  129. end;
  130. procedure TFPPalette.Clear;
  131. begin
  132. SetCount (0);
  133. end;
  134. { Functions to create standard palettes, by Giulio Bernardi 2005 }
  135. { A simple 1 bit black and white palette }
  136. function CreateBlackAndWhitePalette : TFPPalette;
  137. var fppal : TFPPalette;
  138. Col : TFPColor;
  139. begin
  140. fppal:=TFPPalette.Create(2);
  141. Col.Alpha:=AlphaOpaque;
  142. Col.Red:=$FFFF; Col.Green:=$FFFF; Col.Blue:=$FFFF;
  143. fppal.Color[0]:=Col;
  144. Col.Red:=$0000; Col.Green:=$0000; Col.Blue:=$0000;
  145. fppal.Color[1]:=Col;
  146. Result:=fppal;
  147. end;
  148. { The "standard" netscape 216-color palette (aka: web safe palette) }
  149. function CreateWebSafePalette : TFPPalette;
  150. var Col : TFPColor;
  151. i : integer;
  152. fppal : TFPPalette;
  153. begin
  154. fppal:=TFPPalette.Create(216);
  155. Col.Alpha:=AlphaOpaque;
  156. i:=0;
  157. Col.Red:=$FFFF;
  158. while true do
  159. begin
  160. Col.Green:=$FFFF;
  161. while true do
  162. begin
  163. Col.Blue:=$FFFF;
  164. while true do
  165. begin
  166. fppal.Color[i]:=Col;
  167. if Col.Blue=0 then break;
  168. dec(Col.Blue,$3333);
  169. end;
  170. if Col.Green=0 then break;
  171. dec(Col.Green,$3333);
  172. end;
  173. if Col.Red=0 then break;
  174. dec(Col.Red,$3333);
  175. end;
  176. Result:=fppal;
  177. end;
  178. { A grayscale palette. Not very useful. }
  179. function CreateGrayScalePalette : TFPPalette;
  180. var Col : TFPColor;
  181. i : integer;
  182. fppal : TFPPalette;
  183. begin
  184. fppal:=TFPPalette.Create(256);
  185. Col.Alpha:=AlphaOpaque;
  186. for i:=$FF downto 0 do
  187. begin
  188. Col.Red:=i;
  189. Col.Red:=(Col.Red shl 8) + Col.Red;
  190. Col.Green:=Col.Red;
  191. Col.Blue:=Col.Red;
  192. fppal.Color[i]:=Col;
  193. end;
  194. Result:=fppal;
  195. end;
  196. { Standard VGA 16 color palette. }
  197. function CreateVGAPalette : TFPPalette;
  198. var fppal : TFPPalette;
  199. begin
  200. fppal:=TFPPalette.Create(16);
  201. fppal.Color[0]:=colBlack;
  202. fppal.Color[1]:=colNavy;
  203. fppal.Color[2]:=colBlue;
  204. fppal.Color[3]:=colMaroon;
  205. fppal.Color[4]:=colPurple;
  206. fppal.Color[5]:=colDkGreen;
  207. fppal.Color[6]:=colRed;
  208. fppal.Color[7]:=colTeal;
  209. fppal.Color[8]:=colFuchsia;
  210. fppal.Color[9]:=colOlive;
  211. fppal.Color[10]:=colGray;
  212. fppal.Color[11]:=colLime;
  213. fppal.Color[12]:=colAqua;
  214. fppal.Color[13]:=colSilver;
  215. fppal.Color[14]:=colYellow;
  216. fppal.Color[15]:=colWhite;
  217. Result:=fppal;
  218. end;