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
| unit Palette;
interface
uses
Windows, Graphics;
Type
TPalette = Class
Private
NbColor : Integer; // nbre de couleurs de la palette
TabColor : array of TColor; // tableau des couleurs de la palette
NbColorDegrade : Integer; // nbre de couleurs du dégradé
TabColorDegrade : array of TColor; // tableau des couleurs du dégradé
function Gradient(ColorFrom,ColorTo : TColor; NbColor,CurColor: Word): TColor;
Public
constructor Create;
destructor Destroy;Override;
function GetNbColor: Integer;
function GetColor(r: Integer): TColor;
function GetNbColorDegrade: Integer;
function GetColorDegrade(r: Integer): TColor;
procedure SetNbColor(nb: Integer);
procedure SetNbColorDegrade(nb: Integer);
procedure Degrade(nb: Integer);
procedure AddColor(c: TColor);
procedure DeleteColor(c: TColor);
End;
implementation
function TPalette.GetNbColor: Integer;
begin
RESULT := NbColor;
end;
function TPalette.GetColor(r: Integer): TColor;
begin
RESULT := TabColor[r-1];
end;
function TPalette.GetNbColorDegrade: Integer;
begin
RESULT := NbColorDegrade;
end;
function TPAlette.GetColorDegrade(r: Integer): TColor;
begin
RESULT := TabColorDegrade[r-1];
end;
procedure TPalette.SetNbColor(nb: Integer);
begin
NbColor := nb;
end;
procedure TPalette.SetNbColorDegrade(nb: Integer);
begin
NbColorDegrade := nb;
end;
procedure TPalette.AddColor(c: TColor);
begin
SetNbColor(NbColor + 1);
SetLength(TabColor, NbColor);
TabColor[NbColor -1] := c;
SetNbColorDegrade(NbColorDegrade + 1);
SetLength(TabColorDegrade, NbColorDegrade);
TabColorDegrade[NbColorDegrade -1] := c;
end;
procedure TPalette.DeleteColor(c: TColor);
var
i, j: Integer;
begin
for i:=NbColor downto 1 do
begin
if TabColor[i-1]= c then
begin
for j:=i to NbColor-1 do
begin
TabColor[j-1] := TabColor[j];
end;
SetNbColor(NbColor -1);
SetLength(TabColor, NbColor);
end;
end;
// on remet à jour le dégradé
SetNbColorDegrade(NbColorDegrade -1);
Degrade(NbColorDegrade);
end;
procedure TPalette.Degrade(nb: Integer);
var
i, j, k: Integer;
nbinter, toto: Integer;
nbc: Integer;
TabNbColor: array of Integer;
begin
SetNbColorDegrade(nb);
SetLength(TabColorDegrade, GetNbColorDegrade);
nbinter := GetNbColor - 1;
toto := nbinter;
if NbColor=1 then
for i:=1 to nb do TabColorDegrade[i-1] := TabColor[0]
else
begin
nbc := (nb - NbColor) div nbinter;
SetLength(TabNbColor, nbinter);
for i:=1 to NbColor do TabNbColor[i-1] := nbc + 1;
i := nb - nbinter * nbc - NbColor; // nbre de couleurs restants à répartir
Randomize;
while (i <> 0) do
begin
k := Random(nbinter);
TabNbColor[j-1] := TabNbColor[j-1] + 1;
i := i - 1;
end;
// nbc := nb -1;
for i:=1 to nbinter do
begin
nbc := TabNbColor[i-1];
j := 0;
while j < nbc do
begin
if ((i-1)*nbc+j)<=High(TabColorDegrade) then
TabColorDegrade[(i-1) * nbc + j] := Gradient(TabColor[i-1], TabColor[i], nbc, j);
j := j + 1;
end;
end;
end;
end;
function TPalette.Gradient(ColorFrom,ColorTo : TColor; NbColor,CurColor: Word): TColor;
var
RGBFrom: array[0..2] of Byte;
RGBTo: array[0..2] of Byte;
i: Word;
r, v, b: Real;
r2, v2, b2: Real;
decR, decV, decB: Real;
CFrom, CTo: Real;
begin
RGBFrom[0] := GetRValue(ColorToRGB(ColorFrom));
RGBFrom[1] := GetGValue(ColorToRGB(ColorFrom));
RGBFrom[2] := GetBValue(ColorToRGB(ColorFrom));
RGBTo[0] := GetRValue(ColorToRGB(ColorTo));
RGBTo[1] := GetGValue(ColorToRGB(ColorTo));
RGBTo[2] := GetBValue(ColorToRGB(ColorTo));
CFrom := RGB(RGBFrom[0], RGBFrom[1], RGBFrom[2]);
CTo := RGB(RGBTo[0], RGBTo[1], RGBTo[2]);
b := Trunc(CFrom/65536);
v := Trunc((CFrom - b*65536)/256);
r := CFrom - b*65536 - v*256;
b2 := Trunc(CTo/65536);
v2 := Trunc((CTo - b2*65536)/256);
r2 := CTo - b2*65536 -v2*256;
decR := (r2-r)/NbColor;
decV := (v2-v)/NbColor;
decB := (b2-b)/NbColor;
if CurColor = 0 then
begin
r := RGBFrom[0];
v := RGBFrom[1];
b := RGBFrom[2];
end
else
begin
for i:=1 to CurColor do
begin
r := abs(r+decR);
v := abs(v+decV);
b := abs(b+decB);
end;
end;
RESULT := RGB(Trunc(r),Trunc(v),Trunc(b));
end;
constructor TPalette.Create;
begin
inherited Create;
NbColor := 0;
NbColorDegrade := 0;
end;
destructor TPalette.Destroy;
begin
inherited Destroy;
end;
end. |
Partager